Function composition: Difference between revisions
m
→{{header|Amazing Hopper}}
m (Update Lang example: Fix spelling of Lang) |
|||
(12 intermediate revisions by 9 users not shown) | |||
Line 183:
F sin arc sin = compose(sin, arc sin);
print((sin arc sin(0.5), (sin O arc sin)(0.5), new line))</syntaxhighlight>
=={{header|Amazing Hopper}}==
VERSION 1:
<syntaxhighlight lang="c">
#defn Compose(_FX_,_FY_) _FX_,_FY_
main:
0.5,Compose(sin,arcsin)
"\n", print
{0}return
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/compose1.hop
0.500000
</pre>
VERSION 2:
<syntaxhighlight lang="c">
#define-a «(_X_) _X_ )
#define Compose(_FX_,_FY_) _FC_(_FX_,_FY_,
#define _FC_(_X_,_Y_,*) *,_X_,_Y_
main:
Compose(sin,arcsin)«( 0.5)
"\n", print
{0}return
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/compose2.hop
0.500000
</pre>
VERSION 3:
<syntaxhighlight lang="c">
#define «(_X_) _X_ )
#define Compose(_FX_,_FY_) _FC_(_FX_,_FY_,
#define _FC_(_X_,_Y_,*) *,_X_,_Y_
main:
Compose(sin,arcsin)«( 0.5, mul by '2' )
"\n", print
{0}return
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/compose2.hop
1.000000
</pre>
<p>The power of macro-substitution, by Hopper!</p>
=={{header|AntLang}}==
Line 550 ⟶ 603:
{{out}} on Android phone:
<syntaxhighlight lang="bori">0.500000000</syntaxhighlight>
=={{header|Binary Lambda Calculus}}==
In lambda calculus, the compose functions happens to coincide with multiplication on Church numerals, namely <code>compose = \f \g \x. f (g x)</code> which in BLC is
<pre>00 00 00 01 1110 01 110 10</pre>
=={{header|BQN}}==
Line 610 ⟶ 669:
b = compose(->double ->add1)
p b 1 #should print 4</syntaxhighlight>
=={{header|Bruijn}}==
Composition operators as defined in <code>std/Combinator</code>:
<syntaxhighlight lang="bruijn">
:import std/Number .
# 1x composition, bluebird combinator
…∘… [[[2 (1 0)]]]
:test (((inc ∘ (mul (+2))) (+3)) =? (+7)) ([[1]])
# 2x composition, blackbird combinator
…∘∘… [[[[3 (2 1 0)]]]]
:test (((inc ∘∘ mul) (+2) (+3)) =? (+7)) ([[1]])
# 3x composition, bunting combinator
…∘∘∘… [[[[[4 (3 2 1 0)]]]]]
:test (((inc ∘∘∘ (add ∘∘ mul)) (+1) (+2) (+4)) =? (+7)) ([[1]])
# reverse composition, queer bird combinator
…→… [[[1 (2 0)]]]
:test ((((mul (+2)) → inc) (+3)) =? (+7)) ([[1]])
</syntaxhighlight>
=={{header|C}}==
Line 1,044 ⟶ 1,129:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
Line 1,055 ⟶ 1,140:
public program()
{
var fg := (x => x + 1).compose::(x => x * x);
console.printLine(fg(3))
Line 1,335 ⟶ 1,420:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Function_composition}}
'''Solution'''
[[File:Fōrmulæ - Function composition 01.png]]
The compose function returns a lambda expression, containing the actual composition of its arguments, and hence it can be called applied with its argument(s):
'''Test cases'''
[[File:Fōrmulæ - Function composition 02.png]]
[[File:Fōrmulæ - Function composition 03.png]]
Arguments of the functions to compose can be the same symbol, they are not "scrambled":
[[File:Fōrmulæ - Function composition 04.png]]
[[File:Fōrmulæ - Function composition 05.png]]
Because a function in Fōrmulæ is just a lambda expression, a lambda expression can be directly provided.
[[File:Fōrmulæ - Function composition 06.png]]
[[File:Fōrmulæ - Function composition 03.png]]
[[File:Fōrmulæ - Function composition 07.png]]
[[File:Fōrmulæ - Function composition 05.png]]
Since the composition function returns a lambda expression, it is not required to be applied:
[[File:Fōrmulæ - Function composition 08.png]]
[[File:Fōrmulæ - Function composition 09.png]]
[[File:Fōrmulæ - Function composition 10.png]]
[[File:Fōrmulæ - Function composition 11.png]]
=={{header|GAP}}==
Line 1,803 ⟶ 1,922:
# In Lang this task can be achieved with the concat operator
fn.println(parser.op((fp.g ||| fp.f)($x))) # Prints 21 [Internal execution: fp.f(fp.g($x))]
# Creating a user-defined function doing the same thing is a bit more difficult
Line 1,811 ⟶ 1,930:
}
fn.println(fp.compose(fp.f, fp.g)($x)) # Prints also 21
</syntaxhighlight>
Line 2,835 ⟶ 2,954:
=={{header|RPL}}==
{{works with|HP|48G}}
« →STR SWAP →STR +
DUP "»«" POS " " REPL STR→
» '<span style="color:blue">FCOMP</span>' STO <span style="color:grey">@ ''( « f » « g » → « f o g » )''</span>
≪ ALOG ≫ ≪ COS ≫
≪ ALOG ≫ ≪ COS ≫
≪ ALOG ≫ ≪ COS ≫ <span style="color:blue">FCOMP</span> 'x' EVAL
{{out}}
<pre>
3: ≪ COS ALOG ≫
2: 10
1: 'ALOG(COS(x))'
</pre>
=={{header|Ruby}}==
This <tt>compose</tt> method gets passed two Method objects or Proc objects
Line 3,351 ⟶ 3,473:
=={{header|Wren}}==
<syntaxhighlight lang="
var double = Fn.new { |x| 2 * x }
|