-
Notifications
You must be signed in to change notification settings - Fork 1
Partial application
In funcional programming every function takes 1 input and has 1 outputs. When we want to do an operation on multiple arguments we can just return functions. For example the function add
has type
Number -> Number -> Number
Basically the add function takes a number and returns another function which takes a number and returns the sum of both numbers.
The fact functions return functions mean we don't need to pass all arguments at a time. For example we can pass 2 to add and then we will get back a function which adds 2 to any input. Then we can reuse that as many times as we want.
On the Add panel in lunarbox each node has an inputs
fields next to it. By default this is set to the maximum number of inputs, but you can change it.
For example if you set the Add function' inputs to 1 and you click the add icon you will get this:
if you connect it to a number and an output you will get this:
You can see the type of the output benig
Number -> Number
Now the question is, how do we actually call the function returned by add?
Lunarbox offers a node called pipe
. This node simply takes a function and an agument and calls the respective function with the given argument.
For example here's an example calling the returned function from add to add the numbers 2 and 3:
Most of the time this is useful when creating a helper function which just calls another function with some of the arguments being constant. For example this is how you can use a 0-inputs divide node function in conjunction with the flip node (which flips the order of the first arguments of a function) to create a function which takes a number and returns its half:
And here we can see it in action: