Function composition: Difference between revisions

m
(→‎{{header|RPL}}: actually composes 2 functions - previous version only evaluated them one after the other)
 
(8 intermediate revisions by 6 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 46.x :
<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 3,388 ⟶ 3,473:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var compose = Fn.new { |f, g| Fn.new { |x| f.call(g.call(x)) } }
 
var double = Fn.new { |x| 2 * x }
543

edits