Function composition: Difference between revisions

m
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 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 1,335 ⟶ 1,420:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Function_composition}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Function composition 01.png]]
In '''[https://formulae.org/?example=Function_composition this]''' page you can see the program(s) related to this task and their results.
 
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}}
RPL allows x to be a value or a variable. In this case, the function returns an expression.
« →STR SWAP →STR +
{{works with|Halcyon Calc|4.2.7}}
DUP "»«" POS " " REPL STR→
≪ SWAP EVAL SWAP EVAL ≫
» '<span style="color:blue">FCOMP</span>' STO <span style="color:grey">@ ''( « f » « g » → « f o g » )''</span>
'FCOMP' STO
≪ ALOG ≫ ≪ COS ≫ 0<span style="color:blue">FCOMP</span>
≪ ALOG ≫ ≪ COS ≫ 'x'<span style="color:blue">FCOMP</span> 0 EVAL
≪ 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="ecmascriptwren">var compose = Fn.new { |f, g| Fn.new { |x| f.call(g.call(x)) } }
 
var double = Fn.new { |x| 2 * x }
543

edits