operators & operations

  • => (chuck)
  • + - * / (arithmetic)
  • % (modulo)
  • && || == != >= <=” (logic)
  • & | ^ (bitwise)
  • ++ – (inc / dec)
  • ! + - new (unary)

You may have seen many of the operators in other programming languages (C/Java). Some others are native to ChucK. We start with the family of ChucK operators.

=> (the ChucK operator)

The ChucK operator (=>) is a massively(大量地) overloaded operator that, depending on the types involved(包含), performs various actions(执行各种操作). It denotes(表示) **action**, can be chained(链接), and imposes(强制执行) and clarifies order(理清顺序) (always goes *from left to right*). The ChucK operator is the means by which work is done in ChucK. Furthermore(此外), the ChucK operator is not a single operator, but a family of operators.

=> (foundational(基础的) ChucK operator)

We start with the standard, plain-vanilla(普通的,无修饰的) ChucK operator (=>). It is left-associative (all ChucK operators are), which allows us to specify(指定) any ordered flow of data/tasks/modules (such as unit generator connection) from left to right, as in written (English) text. What => does depends on the context(环境). It always depends on the type of the entity(实体) on the left (the chucker(投手)) and the one on the right (the chuckee(接球手)), and it sometimes also depends on the nature of the entity (such as whether it is a variable(变量的) or not).

Some examples:
(用了c的高亮,至少好看点)

// a unit generator patch - the signal flow is apparent
// (in this case, => connects two unit generators)
SinOsc b => Gain g => BiQuad f => dac;// add 4 to foo, chuck result to new 'int' variable 'bar'
// (in this case, => assigns a value to a variable (int)
4 + foo => int bar;// chuck values to a function == function call
// (same as Math.rand2f( 30, 1000))
( 30, 1000 ) => Math.rand2f;

There are many other well-defined(定义明确的) uses of the ChucK operator, depending on the context.

@=> (explicit(明确的) assignment(分配) ChucK operator)

In ChucK, there is no stardard assignment operator (=), found in many other programming languages. Assignment is carried out using ChucK operators. In the previous examples, we have used => for assignment:

// assign 4 to variable foo
4 => int foo;// assign 1.5 to variable bar
1.5 => float bar;// assign duration of 100 millisecond to duh
100::ms => dur duh;// assign the time "5 second from now" to later
5::second + now => time later;

The @=> explicit assignment ChucK operator behaves exactly the same for the above types (int, float, dur, time). However, the difference is that @=> can also be used for reference assignments(分配) of objects (see objects and classes) whereas(然而) => only does assignment on primitive(原始的) types (int, float, dur, time). The behavior(行为) of => on objects is completely context-dependent(上下文相关的).

// using @=> is same as => for primitive types
4 @=> int foo;// assign 1.5 to variable bar
1.5 @=> float bar;// (only @=> can perform reference assignment on objects)// reference assign moe to larry
// (such that both moe and larry reference the same object)
Object moe @=> Object @ larry;// array initialization
[ 1, 2 ] @=> int ar[];// using new
new Object @=> moe;

In its own screwed-up(糟透了的) way, this is kind of nice because there is no confusion(混淆) between assignment (@=> or =>) and equality (==). In fact, the following is not a valid ChucK statement(声明):

// not a valid ChucK statement!
int foo = 4;

+=> -=> *=> /=> etc. (arithmetic(算数) ChucK operators)

These operators are used with variables(变量) (using ‘int’ and ‘float’) to perform one operation with assignment(执行一个有分配的操作).

// add 4 to foo and assign result to foo
foo + 4 => foo;// add 4 to foo and assign result to foo
4 +=> foo;// subtract 10 from foo and assign result to foo
// remember this is (foo-10), not (10-foo)
10 -=> foo;// 2 times foo assign result to foo
2 *=> foo;// divide 4 into foo and assign result to foo
// again remember this is (foo/4), not (4/foo)
4 /=> foo;

It is important to note the relationship between the value and variable when using -=> and /=>, since these operations are not commutative(交换的).

// mod foo by T and assign result to foo
T %=> foo;// bitwise(按位) AND 0xff and bar and assign result to bar
0xff &=> bar;// bitwise OR 0xff and bar and assign result to bar
0xff |=> bar;

+ - * / (arithmetic)

// divide (and assign)
16 / 4 => int four;// multiply
2 * 2 => four;// add
3 + 1 => four;// subtract
93 - 89 => four;

cast(转换)

ChucK implicitly(隐式地) casts int values to float when float is expected, but not the other around. The latter could result in a loss of information and requires an explicit(明确的) cast.

// adding float and int produces a float
9.1 + 2 => float result;// however, going from float to int requires cast
4.8 $ int => int foo;  // foo == 4
//这里的$是什么意思?// this function expects two floats
Math.rand2f( 30.0, 1000.0 );// this is ok because of implicit cast
Math.rand2f( 30, 1000 );

% (modulo(取模))

The modulo operator % computes the remainder(余数) after integer(整数), floating point, duration, and time/duration division.

// 7 mod 4 (should yield(产生) 3)
7 % 4 => int result;// 7.3 mod 3.2 floating point mod (should yield .9)
7.3 % 3.2 => float resultf;// duration mod
5::second % 2::second => dur foo;// time/duration mod
now % 5::second => dur bar;

the latter (time/duration mod) is one of many ways to dynamically(动态地) synchronize(同步) timing in shreds. the examples otf_01.ck through otf_07.ck (see under examples) make use of this to on-the-fly(运行时) synchronize its various parts, no matter when each shred is added to the virtual(虚拟的) machine:

// define period (agreed upon by several shreds)
.5::second => dur T;// compute the remainder(余数) of the current period ...
// and advance time by that amount
T - (now % T) => now;// when we reach this point, we are synchronized to T period boundary// the rest of the code
// ...

This is one of many ways to compute and reason about(思考) time in ChucK. The appropriate(适当的) solution(解决方案)(s) in each case depends on the intended(打算的) functionality(功能).

&& || == != > >= < <= (logic)(逻辑)

Logical operators - each of these need two operands(操作数). The result is an integer value of 0 or 1.

  • && : and
  • || : or
  • == : equals
  • != : does not equal
  • > : greater than
  • >= : greater than or equal to
  • < : less than
  • <= : less than or equal to
// test some universal truths(普遍的真理)
if( 1 <= 4 && true )<<<"horray">>>;

>> << & | ^ (bitwise)(按位)

These are used on int values at the bit level, often for bit masking.

>> : shift bits right(右移位) ( 8 >> 1 = 4 )
<< : shift bits left ( 8 << 1 = 16 )
& : bitwise AND
| : bitwise OR
^ : bitwise XOR 

++ – (inc / dec)

Values may be incremented or decremented by appending(附加) the ++ or -- operators.

4 => int foo;
foo++;
foo--;

! + - new (unary(一元的))

These operators come before(位于…之前) one operand(操作数).

// logical invert
if( !true == false )<<<"yes">>>;// negative
-1 => int foo;// instantiate object
new object @=> object @ bar;

ChucK初步(5)相关推荐

  1. TensorRT 7.2.1开发初步

    TensorRT 7.2.1开发初步 TensorRT 7.2.1开发人员指南演示了如何使用C ++和Python API来实现最常见的深度学习层.它显示了如何采用深度学习框架构建现有模型,并使用该模 ...

  2. SOC,System on-a-Chip技术初步

    SOC,System on-a-Chip技术初步 S O C(拼作S-O-C)是一种集成电路,它包含了电子系统在单个芯片上所需的所有电路和组件.它可以与传统的计算机系统形成对比,后者由许多不同的组件组 ...

  3. 《OpenCV3编程入门》学习笔记3 HighGUI图形用户界面初步

    第3章 HighGUI图形用户界面初步 3.1 图像的载入.显示和输出到文件 1.OpenCV命名空间2种访问方法 (1)代码开头加:usingnamespace cv; (2)每个类或函数前加:cv ...

  4. 初步判断内存泄漏方法

    有时候,内存泄漏不明显,或者怀疑系统有内存泄漏,我们可以通过下面介绍的方法初步确认系统是否存在内存泄漏. 首先在Java命令行中增加-verbose:gc参数, 然后重新启动java进程. 当系统运行 ...

  5. android蓝牙4.0(BLE)开发之ibeacon初步

    一个april beacon里携带的信息如下 ? 1 <code class=" hljs ">0201061AFF4C0002159069BDB88C11416BAC ...

  6. 游戏AI之初步介绍(0)

    目录 游戏AI是什么? 游戏AI和理论AI 智能的假象 (更新)游戏AI和机器学习 介绍一些游戏AI 4X游戏AI <求生之路>系列 角色扮演/沙盒游戏中的NPC 游戏AI 需要学些什么? ...

  7. 【转】ibatis的简介与初步搭建应用

    [转]ibatis的简介与初步搭建应用 一.ibatis的简介 ibatis是什么东西就不介绍了,自己去找谷老师. 这里讲下自己的使用体会.之前自己学过Hibernate,是看尚学堂的视频教学的,看完 ...

  8. 初步了解:使用JavaScript进行表达式(De Do Do Do,De Da Da Da)

    by Donavon West 由Donavon West 初步了解:使用JavaScript进行表达式(De Do Do Do,De Da Da Da) (A first look: do expr ...

  9. 存储过程和存储函数初步

    2019独角兽企业重金招聘Python工程师标准>>> 存储过程和函数初步 简单的来说,存储过程就是一条或者多条 SQL 语句的集合,可视为批处理文件,但是其作用不仅限于批处理. # ...

  10. 【spring框架】spring整合hibernate初步

    spring与hibernate做整合的时候,首先我们要获得sessionFactory. 我们一般只需要操作一个sessionFactory,也就是一个"单例",这一点很适合交给 ...

最新文章

  1. 就业丨2018年国内就业薪资高的5大编程语言排行
  2. 吴军:既能得诺贝尔奖,又能生产高科技产品,美国的科研机制是如何运行的?...
  3. pup 流程控制语句(下)
  4. 0宽字符加密_前端AES加密方式分析,及其python实现
  5. linux备忘录-vi和vim
  6. 频繁项集挖掘之Aprior和FPGrowth算法
  7. 工作中的javascript代码收集及整理
  8. SQL 2005: @@identity 的妙用
  9. 2020亚太内容分发大会 阿里云荣获“边缘计算领航企业”奖
  10. des加密的c语言程序,C++中四种加密算法之DES源代码
  11. asp.net mvc源码分析-Action篇 Action的执行
  12. 把excel每一行中的数据输出为一个txt文档的VBA函数
  13. 洛谷 1855——榨取kkksc03
  14. Circular Sequence
  15. Github上最热门的Java开源项目
  16. word硬回车是怎么产生的_在word中怎样删除软硬回车?
  17. Learn Git Branching 笔记
  18. java-php-python-springboo垃圾分类网站计算机毕业设计
  19. 计算机算法在生物信息学中的应用,引力场算法及其在生物信息学中的应用
  20. JS实现简单的网页文本转语音阅读

热门文章

  1. Java实现 LeetCode 365 水壶问题
  2. MXNet对含隐藏状态的循环神经网络(RNN)的实现
  3. activiti画流程图、部署、启动、审批、驳回
  4. 7年弹指一挥间:iOS演进史
  5. oracle报错imp报错00008,Oracle imp导入数据时报IMP-00032与IMP-00008的解决方法
  6. 从入门到放弃:微信小程序入门个人指南Day 4
  7. 周杰伦录音室专辑名字整理,时间倒数
  8. Flink整合kafka并基于事件源生成时间戳以及水印
  9. Excel图标美化技巧
  10. 大道至简:软件工程实践者的思想(读后感想)