Your Object may be a Function! MaxMSP functional programming learning

First comes a poem:

There is a cycle, a rhythm to the universe.

Today one program will be popular, tomorrow another.

Today 500 bugs will be fixed, tomorrow another 500 will appear.

To understand life is to know that the rhythm exists.

To understand Zen is to live outside this rhythm,

detached from the everyday concerns of life.

Only then can the mind be free.

— — from The Tao of Programming

The theory of the Object-Oriented Programming (OOP) holds that “Everything is object”.

Object-oriented is probably the most used programming design pattern for school and enterprise products.
Once brainwashed, it is difficult to get it back. Such as: the object is always right.

In recent years I began to learn and use Functional Reactive Programming (FRP).
FRP combines Reactive Programming (RP) and Functional Programming (FP).

Reactive Programming (RP) : Focus on handling asynchronous data streams. It Observes the data streams and then responds to.

For example, cells in Excel can contain formulas like “=B1+C1”, and the values of these cells could change depending on the values of other cells (B1 and C1).

Reactive Programming processes stream, observes stream, and responds to the stream quickly.

I have understood this point, when I was learning Jeet Kune Do(JKD) as a child :

Be water, my friend.

Functional Programming (FP): more likes doing math operations.

“Write the arithmetic process into a series of nested function calls.”(from 函数式编程初探 — 阮一峰的网络日志

Note that the input parameter x of the function f(x) above, can also be another function.

Although not unfamiliar with Functional Reactive Programming (FRP), and I have also been used to launch several Apps with ReactCocoa Objective-C, and excitedly make use of signal, map, subscribe, etc.

However, switching from “Everything is Object” to “Everything is Function” is still a bit puzzling.

In particular, after merging the concept of OOP and FP, once I thought that “In fact, your object is a function”, there is a mess in my brain.

So, let’s forget the theory, and just start with the example:

This pic is from a very hot FRP tutorial:
The introduction to Reactive Programming you’ve been missing
https://gist.github.com/staltz/868e7e9bc2a7b8c1f754

The black arrow line in the figure represents stream, data stream.

“A gray rectangle is a function that converts a stream into another stream. We buffer all click streams in an array every 250ms. This is what buffer (stream.throttle(250ms)) does.

Thus, we get a stream containing multiple arrays, and then call the map() function to map each array to an integer (the length of the array).

Then we call filter(x >= 2) to filter out arrays of length 1.
In summary, we only need 3 operations to get the stream we want. Finally, we call subscribe() to listen and respond to what we want to do. ”

from 可能是最好的 Rx 初学者教程 — 知乎专栏

To implement the conversion of this data stream, written in RxJS, only need 4 line codes:

var multiClickStream = clickStream
.buffer(function() { return clickStream.throttle(250); })
.map(function(list) { return list.length; })
.filter(function(x) { return x >= 2; });

After getting the multiClickStream, subscribe it, then you can observe the value in the data stream:

multiClickStream.subscribe(function (numclicks) {
console.log(numclicks);
)};

The above-mentioned functional operations on stream, after removing the line breaks, it can be:
stream.buffer().map().filter()
More silky smooth.

If you change “.” to “-“:
stream — buffer() — map() — filter()
It looks like programming with MaxMSP / VVVV / Pure Data, is it?

Take MaxMSP as an example:

The figure above is the Max version of that RxJS code, with the same function.

Once you click, it will not be output at last of the stream. The last print log still shows a previous 3 combo click.

If you make 4 combos click, you will pass the filter and get the result:

It can be seen that the map and filter of the green part in the above picture are not very “functional” in the details, or it is more like using an imperative program to tell the machine how to execute instructions rather than writing function elegantly.

The Center for New Music and Audio Technologies (CNMAT), a research center at the University of Berkeley, designed and developed a Max’s library, ODOT.

ODOT is based on OSC , just that familiar Open Sound Control.

First of all, the point is the OSC data encapsulation format, name-value structure.

Secondly, OSC data can be easily flowed from one module to another within the program. Even from hardware Arduino module, through the network, to the software modules such as Max, Processing, C, and Java.

Use ODOT to refactor the map and filter modules above, as shown in the following figure:

O.pack encapsulates incoming stream data into OSC format.

The following two sentences are very intuitive functions:

/data = length(/data) ; //fetch lengtho.if /data >= 2 ; //filter the data more than 2

The source code (max patch) here.

ODOT can also implement more functional features.

Higher-order function:

Recursive:

In addition, using the OSC data structure, ODOT can actually implement Object-Oriented style.
Can refer to the following papers.

ODOT download
Downloads | CNMAT

Reference papers

《Composability for Musical Gesture Signal Processing using new OSC-based Object and Functional Programming Extensions to Max/MSP》

《DYNAMIC, INSTANCE-BASED, OBJECT-ORIENTED PROGRAMMING IN MAX/MSP USING OPEN SOUND CONTROL MESSAGE DELEGATION》

Thanks for reading.

Aftertaste the essence of the opening verse:
Today 500 bugs will be fixed, tomorrow another 500 will appear.

Find more creative stuff on:

Technical artist, computational art, generative art, interactive media, ex game/App/web developer. floatbug.com