# Partial application of a function

Hi,

I don’t quite understand this behaviour:

f = { arg a1, a2, a3; (a1+a2+a3).postln } // function with three args

f.value(“one”, “two”, “three”) // works, results in “one two three”

g = { f.value(_, “TWO”, _) } // define a new function with 2 args

g.value(“one”, “three”) // would expect “one TWO three” but get a reference to a function

Two questions:

1. What is going on here?
2. How do I do such partial applications in SC?

Many thanks!

Where is the underscore syntax you are using documented?

Doesn’t work for me in defining a basic function:

‘’’
g = {+}

g.value(1,2)
‘’’

Partial application syntax itself produces a function – it doesn’t need to be enclosed in braces.

``````// the expression _ + 1 produces a function
f = _ + 1  // -> a Function

f.value(2)  // -> 3

// now we have a function
// *containing* the expression that produces a function
f = { _ + 1 };

// f.value evaluates the function-producing expression
// so the result is a function
f.value(2);  // -> a Function

// so your g should be written
g = f.value(_, "TWO", _);
``````

Admittedly the latter is unclear; if you find this to be unreadable (it’s a borderline case for me), then it’s always possible to write using standard arguments.

Btw please use backticks for code examples. Note in your initial post that the quotes have become typographic open/close quotes. If you copy/paste the code from this page into SC, you’ll get syntax errors for the unrecognized quotes.

`````````
g = f.value(_, "TWO", _);
```
``````

hjh

1 Like

http://doc.sccode.org/Reference/Partial-Application.html

Again, do not use partial application with braces: `g = _ + _` should work.

The main intended use of partial application is for `collect` and similar methods. `g = _ + _ ` looks weird; `anArray.select(_ > 0)` does not look so weird.

(Also note the accidental use of forward apostrophes for the code snip, which caused the underscores to become formatting marks – markdown for code is kind of tricky, isn’t it?)

hjh

Ok, many thanks.
That clarifies the matter for me.