Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Partial application

Matei Adriel edited this page May 7, 2020 · 4 revisions

Functions

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.

Partial application

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:

curried add

if you connect it to a number and an output you will get this: curried add connected

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?

The pipe node

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:

add 2 and 3

What is this useful for

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:

half

And here we can see it in action: half-usage

Clone this wiki locally