<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hello John, Casper<div class=""><br class=""></div><div class="">For better or worse, LiCK was inspired by functional-ish language features in Java (the colt library and commons-functor specifically) well before JDK8 lambdas or other JVM languages such as Scala. &nbsp;The downside as Casper points out is lots of little classes.</div><div class=""><br class=""></div><div class="">John's original example might look like</div><div class=""><br class=""></div><div class=""><div class="">IntArrayList notes;</div><div class="">notes.size(10);</div><div class="">notes.assign(42);</div><div class="">&lt;&lt;&lt;notes.get(0)&gt;&gt;&gt;;</div><div class=""><br class=""></div><div class="">class Increment extends IntFunction {</div><div class="">&nbsp; fun int evaluate(int arg) {</div><div class="">&nbsp; &nbsp; return arg + 1;</div><div class="">&nbsp; }</div><div class="">}</div><div class=""><br class=""></div><div class="">Increment increment;</div><div class="">notes.transform(increment);</div><div class="">&lt;&lt;&lt;notes.get(0)&gt;&gt;&gt;;</div></div><div class=""><br class=""></div><div class=""><div class="">...</div><div class="">[chuck](VM): sporking incoming shred: 1 (increment.ck)...</div><div class="">42 :(int)</div><div class="">43 :(int)</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Hope this helps!</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp;michael</div><div class=""><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class="">On Aug 26, 2020, at 6:22 AM, Casper Schipper &lt;<a href="mailto:casper.schipper@gmail.com" class="">casper.schipper@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hello John,<div class=""><br class=""></div><div class="">It is not really possible in normal Chuck syntax: functions are not values in Chuck and there is no function type.</div><div class=""><br class=""></div><div class="">However, you can simulate functions as values, by creating classes that create objects that behave like a function would.</div><div class="">The library LicK provides an implementation of that idea:</div><div class=""><a href="https://github.com/heuermh/lick/tree/master/lick/fn" class="">https://github.com/heuermh/lick/tree/master/lick/fn</a></div><div class=""><br class=""></div><div class="">The approach is best explained with an example I think.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class=""><span style="color: #0000ff" class="">// this is the Function class from LicK for functions that take one float argument.</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class=""><span style="color: #0000ff" class="">class</span> FloatFunction</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">{</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #d48010" class="">0</span>.<span style="color: #d48010" class="">0</span> =&gt; <span style="color: #0000ff" class="">float</span> default;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #0000ff" class="">fun</span> <span style="color: #0000ff" class="">float</span> evaluate(<span style="color: #0000ff" class="">float</span> arg0) // evaluate is abstract method applying the function to an argument.&nbsp;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff" class="">return</span> default;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">}</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class="">// You extend it to make some function, for example, to make an addition Function object:</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class=""><span style="color: #0000ff" class="">class</span> Adder <span style="color: #0000ff" class="">extends</span> FloatFunction</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">{</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #0000ff" class="">float</span> value;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #0000ff" class="">fun</span> <span style="color: #0000ff" class="">void</span> init(<span style="color: #0000ff" class="">float</span> arg) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; &nbsp; &nbsp; arg =&gt; value;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #0000ff" class="">fun</span> <span style="color: #0000ff" class="">float</span> evaluate(<span style="color: #0000ff" class="">float</span> arg0) { // here we override the evaluate method from the base class.</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff" class="">return</span> value + arg0;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">}</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class="">// this is just a helper, to create a Adder object:</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class=""><span style="color: #0000ff" class="">fun</span> Adder mkAdder(<span style="color: #0000ff" class="">float</span> arg) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; Adder a;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; a.init(arg);</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #0000ff" class="">return</span> a;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">}</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; color: rgb(96, 144, 16);" class="">// Now it is possible to implement map, where you can take a function object as an argument:</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class=""><span style="color: #0000ff" class="">fun</span> <span style="color: #0000ff" class="">float</span> [] map (FloatFunction f, <span style="color: #0000ff" class="">float</span> lst[]) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #0000ff" class="">float</span> result[lst.cap()];</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #0000ff" class="">for</span> (<span style="color: #0000ff" class="">int</span> i;i&lt;lst.cap();i++) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; &nbsp; &nbsp; f.evaluate(lst[i]) =&gt; result[i];</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #0000ff" class="">return</span> result;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">}</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class=""><span style="color: #0000ff" class="">fun</span> <span style="color: #0000ff" class="">void</span> printArray(<span style="color: #0000ff" class="">float</span> arr[]) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; <span style="color: #0000ff" class="">for</span> (<span style="color: #0000ff" class="">int</span> i;i&lt;arr.cap();i++) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;&lt;arr[i]&gt;&gt;&gt;;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">}</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class="">// so now your transposing mapping would look something like this:</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">[<span style="color: #d48010" class="">10</span>.,<span style="color: #d48010" class="">11</span>.,<span style="color: #d48010" class="">12</span>.,<span style="color: #d48010" class="">13</span>] @=&gt; <span style="color: #0000ff" class="">float</span> test[];</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco; min-height: 17px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">map(mkAdder(<span style="color: #d48010" class="">10</span>.<span style="color: #d48010" class="">0</span>),test) =&gt; printArray;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Monaco;" class="">map(mkAdder(<span style="color: #d48010" class="">20</span>.<span style="color: #d48010" class="">0</span>),test) =&gt; printArray;</div></div><div class=""><br class=""></div><div class="">A limitation that remains with this, since there is no curying etc.., you need a lot of classes, for each function type one. So creating and defining functions like this is not very lightweight (you almost always have to extend some class :-/). No surprise I guess: this is just programming a functional programming language using an object oriented one. My personal way of getting around that limitation was to build a translator in python that takes my own (scheme-like) syntax and parses it into chuck code* to automate all that boilerplate. But that is still quite a lot work! If you are really into doing functional stuff, it may also be worthwhile to consider use a “proper" functional language (ocaml/haskell) and communicate with chuck through OSC? It depends a bit on what level you would like to use the functions (controlling parameters or audio processing itself). I probably would have done something like this now if I didn’t have a whole bunch of tools written in chuck already :-P.&nbsp;</div><div class=""><br class=""></div><div class="">Hope this helps,</div><div class="">Casper</div><div class=""><br class=""></div><div class="">* <a href="https://github.com/casperschipper/ChucK-Tools" class="">https://github.com/casperschipper/ChucK-Tools</a>&nbsp;&amp; <a href="https://github.com/casperschipper/cisp" class="">https://github.com/casperschipper/cisp</a></div><div class="">There is some documentation in the cisp repo (sorry not very complete, this is kind of a personal toolbox).</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On 25 Aug 2020, at 22:51, John Crane &lt;<a href="mailto:john.crane@gmail.com" class="">john.crane@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">Hi all,&nbsp;</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif">Chuck newbie here. Is it possible to pass a function as an argument? I'd like to be able to&nbsp; transform&nbsp;things in a functional programming manner.&nbsp; As a trivial example, if I had an array of midi notes and wanted to raise them all one interval.&nbsp;</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif">&nbsp; &nbsp; fun int increment(int note) {&nbsp;</div><div class="gmail_default" style="font-family:georgia,serif">&nbsp; &nbsp; &nbsp; &nbsp; note + 1;&nbsp;</div><div class="gmail_default" style="font-family:georgia,serif">&nbsp; &nbsp; }</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif">&nbsp; &nbsp; // use map to apply a function to all elements of an array</div><div class="gmail_default" style="font-family:georgia,serif">&nbsp; &nbsp; map ( array_of_notes, increment);</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class=""><div class="gmail_default" style="font-family:georgia,serif">thanks in advance,</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif">John</div></div></div>
_______________________________________________<br class="">chuck-users mailing list<br class=""><a href="mailto:chuck-users@lists.cs.princeton.edu" class="">chuck-users@lists.cs.princeton.edu</a><br class=""><a href="https://lists.cs.princeton.edu/mailman/listinfo/chuck-users" class="">https://lists.cs.princeton.edu/mailman/listinfo/chuck-users</a><br class=""></div></blockquote></div><br class=""></div></div></div>_______________________________________________<br class="">chuck-users mailing list<br class=""><a href="mailto:chuck-users@lists.cs.princeton.edu" class="">chuck-users@lists.cs.princeton.edu</a><br class="">https://lists.cs.princeton.edu/mailman/listinfo/chuck-users<br class=""></div></blockquote></div><br class=""></div></body></html>