Reification of a mathematical function as an object. Allows function arithmetics and function composition.
To install Functon, go to the Playground (Ctrl+OW) in your Pharo image and execute the following Metacello script (select it and press Do-it button or Ctrl+D):
Metacello new
baseline: 'Function';
repository: 'github://olekscode/Function/src';
load.
Functions can be defined using block closures:
f := [ :x | x sin ] asFunction.
g := [ :x | x cos ] asFunction.
h := [ :x | x + 1 ] asFunction.
alpha := Float pi / 2.
In the following example, f + g
is a new function (f+g)(x) = sin(x) + cos(x)
:
(f + g) value: alpha. "1.0"
Composition of two functions f(x)
and h(x)
is a new function (f@h)(x) = f(h(x))
. In our example, (f@h)(x) = sin(x+1)
and (h@f)(x) = sin(x) + 1
:
(f @ h) value: alpha. "0.54"
(h @ f) value: alpha. "2.0"