Let's consider an example where we wish to construct a linear function of x called f, with the slope and y-intercept of the graph being optional arguments of f. We use the ==> operator to attach the default values to our function, coded in a special way.
i1 : opts = {Slope => 1, Intercept => 1} |
i2 : f = opts ==> o -> x -> x * o.Slope + o.Intercept |
i3 : f 5 |
i4 : f(5, Slope => 100) |
i5 : f(5, Slope => 100, Intercept => 1000) |
In the example the function body is the code x * opts.Slope + opts.Intercept. When it is evaluated, a hash table is assigned to opts; its keys are the names of the optional arguments, and the values are the corresponding current values, obtained either from the default values specified in the definition of f, or from the options specified at the time f is called.
In the example above, the inner function has just one argument, x, but handling multiple arguments is just as easy. Here is an example with two arguments.
i6 : f = {a => 1000} ==> o -> (x,y) -> x * o.a + y; |
i7 : f(3,7) |
i8 : f(5,11,a=>10^20) |