Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Function-composition/00-META.yaml
Normal file
3
Task/Function-composition/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Function_composition
|
||||
note: Higher-order functions
|
||||
16
Task/Function-composition/00-TASK.txt
Normal file
16
Task/Function-composition/00-TASK.txt
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
;Task:
|
||||
Create a function, <span style="font-family:serif">compose</span>, whose two arguments <span style="font-family:serif">''f''</span> and <span style="font-family:serif">''g''</span>, are both functions with one argument.
|
||||
|
||||
|
||||
The result of <span style="font-family:serif">compose</span> is to be a function of one argument, (lets call the argument <span style="font-family:serif">''x''</span>), which works like applying function <span style="font-family:serif"> ''f'' </span> to the result of applying function <span style="font-family:serif"> ''g'' </span> to <span style="font-family:serif"> ''x''</span>.
|
||||
|
||||
|
||||
;Example:
|
||||
<span style="font-family:serif">compose(''f'', ''g'') (''x'') = ''f''(''g''(''x''))</span>
|
||||
|
||||
|
||||
Reference: [[wp:Function composition (computer science)|Function composition]]
|
||||
|
||||
Hint: In some languages, implementing <span style="font-family:serif">compose</span> correctly requires creating a [[wp:Closure (computer science)|closure]].
|
||||
<br><br>
|
||||
|
||||
3
Task/Function-composition/11l/function-composition.11l
Normal file
3
Task/Function-composition/11l/function-composition.11l
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
V compose = (f, g) -> (x -> @f(@g(x)))
|
||||
V sin_asin = compose(x -> sin(x), x -> asin(x))
|
||||
print(sin_asin(0.5))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
MODE F = PROC(REAL)REAL; # ALGOL 68 is strong typed #
|
||||
|
||||
# As a procedure for real to real functions #
|
||||
PROC compose = (F f, g)F: (REAL x)REAL: f(g(x));
|
||||
|
||||
OP (F,F)F O = compose; # or an OPerator that can be overloaded #
|
||||
|
||||
# Example use: #
|
||||
F sin arc sin = compose(sin, arc sin);
|
||||
print((sin arc sin(0.5), (sin O arc sin)(0.5), new line))
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
MODE F = PROC(REAL)REAL; # ALGOL 68 is strong typed #
|
||||
|
||||
# As a procedure for real to real functions #
|
||||
PROC compose = (F f, g)F: ((F f2, g2, REAL x)REAL: f2(g2(x)))(f, g, ); # Curry #
|
||||
|
||||
PRIO O = 7;
|
||||
OP (F,F)F O = compose; # or an OPerator that can be overloaded #
|
||||
|
||||
# Example use: #
|
||||
F sin arc sin = compose(sin, arc sin);
|
||||
print((sin arc sin(0.5), (sin O arc sin)(0.5), new line))
|
||||
4
Task/Function-composition/ANT/function-composition.ant
Normal file
4
Task/Function-composition/ANT/function-composition.ant
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/Apply g to exactly one argument
|
||||
compose1: {f: x; g: y; {f[g[x]]}}
|
||||
/Extra: apply to multiple arguments
|
||||
compose: {f: x; g: y; {f[g apply args]}}
|
||||
46
Task/Function-composition/ATS/function-composition-1.ats
Normal file
46
Task/Function-composition/ATS/function-composition-1.ats
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
(*
|
||||
The task:
|
||||
|
||||
Create a function, compose, whose two arguments f and g, are
|
||||
both functions with one argument.
|
||||
|
||||
The result of compose is to be a function of one argument,
|
||||
(let's call the argument x), which works like applying function
|
||||
f to the result of applying function g to x.
|
||||
|
||||
In ATS, we have to choose whether to use non-linear closures
|
||||
(cloref) or linear closures (cloptr). In the latter case, we also
|
||||
have to choose between closures allocated with malloc (or similar)
|
||||
and closures allocated on the stack.
|
||||
|
||||
For simplicity, we will use non-linear closures and assume there is
|
||||
a garbage collector, or that the memory allocated for the closures
|
||||
can be allowed to leak. (This is often the case in a program that
|
||||
does not run continuously.)
|
||||
*)
|
||||
|
||||
#include "share/atspre_staload.hats"
|
||||
|
||||
(* The following is actually a *template function*, rather than a
|
||||
function proper. It is expanded during template processing. *)
|
||||
|
||||
fn {t1, t2, t3 : t@ype}
|
||||
compose (f : t2 -<cloref1> t3,
|
||||
g : t1 -<cloref1> t2) : t1 -<cloref1> t3 =
|
||||
lam x => f (g (x))
|
||||
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
val one_hundred = 100.0
|
||||
val char_zero = '0'
|
||||
val f = (lam y =<cloref1> add_double_int (one_hundred, y))
|
||||
val g = (lam x =<cloref1> char2i x - char2i char_zero)
|
||||
val z = compose (f, g) ('5')
|
||||
val fg = compose (f, g)
|
||||
val w = fg ('7')
|
||||
in
|
||||
println! (z : double);
|
||||
println! (w : double)
|
||||
end
|
||||
28
Task/Function-composition/ATS/function-composition-2.ats
Normal file
28
Task/Function-composition/ATS/function-composition-2.ats
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
fn {t1, t2, t3 : t@ype}
|
||||
compose (f : t2 -<cloref1> t3,
|
||||
g : t1 -<cloref1> t2) : t1 -<cloref1> t3 =
|
||||
lam x => f (g (x))
|
||||
|
||||
fn
|
||||
compose_char2int2double
|
||||
(f : int -<cloref1> double,
|
||||
g : char -<cloref1> int) :
|
||||
char -<cloref1> double =
|
||||
compose<char, int, double> (f, g)
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
val one_hundred = 100.0
|
||||
val char_zero = '0'
|
||||
val f = (lam y =<cloref1> add_double_int (one_hundred, y))
|
||||
val g = (lam x =<cloref1> char2i x - char2i char_zero)
|
||||
val z = compose_char2int2double (f, g) ('5')
|
||||
val fg = compose_char2int2double (f, g)
|
||||
val w = fg ('7')
|
||||
in
|
||||
println! (z : double);
|
||||
println! (w : double)
|
||||
end
|
||||
29
Task/Function-composition/ATS/function-composition-3.ats
Normal file
29
Task/Function-composition/ATS/function-composition-3.ats
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
fn {t1, t2, t3 : t@ype}
|
||||
compose (f : t2 -<cloref1> t3,
|
||||
g : t1 -<cloref1> t2) :<cloref1>
|
||||
t1 -<cloref1> t3 =
|
||||
lam x => f (g (x))
|
||||
|
||||
fn
|
||||
compose_char2int2double
|
||||
(f : int -<cloref1> double,
|
||||
g : char -<cloref1> int) :<cloref1>
|
||||
char -<cloref1> double =
|
||||
compose<char, int, double> (f, g)
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
val one_hundred = 100.0
|
||||
val char_zero = '0'
|
||||
val f = (lam y =<cloref1> add_double_int (one_hundred, y))
|
||||
val g = (lam x =<cloref1> char2i x - char2i char_zero)
|
||||
val z = compose_char2int2double (f, g) ('5')
|
||||
val fg = compose_char2int2double (f, g)
|
||||
val w = fg ('7')
|
||||
in
|
||||
println! (z : double);
|
||||
println! (w : double)
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
function compose(f:Function, g:Function):Function {
|
||||
return function(x:Object) {return f(g(x));};
|
||||
}
|
||||
function test() {
|
||||
trace(compose(Math.atan, Math.tan)(0.5));
|
||||
}
|
||||
13
Task/Function-composition/Ada/function-composition-1.ada
Normal file
13
Task/Function-composition/Ada/function-composition-1.ada
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
generic
|
||||
type Argument is private;
|
||||
package Functions is
|
||||
type Primitive_Operation is not null
|
||||
access function (Value : Argument) return Argument;
|
||||
type Func (<>) is private;
|
||||
function "*" (Left : Func; Right : Argument) return Argument;
|
||||
function "*" (Left : Func; Right : Primitive_Operation) return Func;
|
||||
function "*" (Left, Right : Primitive_Operation) return Func;
|
||||
function "*" (Left, Right : Func) return Func;
|
||||
private
|
||||
type Func is array (Positive range <>) of Primitive_Operation;
|
||||
end Functions;
|
||||
25
Task/Function-composition/Ada/function-composition-2.ada
Normal file
25
Task/Function-composition/Ada/function-composition-2.ada
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package body Functions is
|
||||
function "*" (Left : Func; Right : Argument) return Argument is
|
||||
Result : Argument := Right;
|
||||
begin
|
||||
for I in reverse Left'Range loop
|
||||
Result := Left (I) (Result);
|
||||
end loop;
|
||||
return Result;
|
||||
end "*";
|
||||
|
||||
function "*" (Left, Right : Func) return Func is
|
||||
begin
|
||||
return Left & Right;
|
||||
end "*";
|
||||
|
||||
function "*" (Left : Func; Right : Primitive_Operation) return Func is
|
||||
begin
|
||||
return Left & (1 => Right);
|
||||
end "*";
|
||||
|
||||
function "*" (Left, Right : Primitive_Operation) return Func is
|
||||
begin
|
||||
return (Left, Right);
|
||||
end "*";
|
||||
end Functions;
|
||||
12
Task/Function-composition/Ada/function-composition-3.ada
Normal file
12
Task/Function-composition/Ada/function-composition-3.ada
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Functions;
|
||||
|
||||
procedure Test_Compose is
|
||||
package Float_Functions is new Functions (Float);
|
||||
use Float_Functions;
|
||||
|
||||
Sin_Arcsin : Func := Sin'Access * Arcsin'Access;
|
||||
begin
|
||||
Put_Line (Float'Image (Sin_Arcsin * 0.5));
|
||||
end Test_Compose;
|
||||
5
Task/Function-composition/Agda/function-composition.agda
Normal file
5
Task/Function-composition/Agda/function-composition.agda
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
compose : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c}
|
||||
→ (B → C)
|
||||
→ (A → B)
|
||||
→ A → C
|
||||
compose f g x = f (g x)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import math
|
||||
|
||||
function compose (f, g) {
|
||||
return function (x) { return f(g(x)) }
|
||||
}
|
||||
|
||||
var func = compose(Math.sin, Math.asin)
|
||||
println (func(0.5)) // 0.5
|
||||
26
Task/Function-composition/Aime/function-composition.aime
Normal file
26
Task/Function-composition/Aime/function-composition.aime
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
compose_i(,,)
|
||||
{
|
||||
($0)(($1)($2));
|
||||
}
|
||||
|
||||
compose(,)
|
||||
{
|
||||
compose_i.apply($0, $1);
|
||||
}
|
||||
|
||||
double(real a)
|
||||
{
|
||||
2 * a;
|
||||
}
|
||||
|
||||
square(real a)
|
||||
{
|
||||
a * a;
|
||||
}
|
||||
|
||||
main(void)
|
||||
{
|
||||
o_(compose(square, double)(40), "\n");
|
||||
|
||||
0;
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
-- Compose two functions where each function is
|
||||
-- a script object with a call(x) handler.
|
||||
on compose(f, g)
|
||||
script
|
||||
on call(x)
|
||||
f's call(g's call(x))
|
||||
end call
|
||||
end script
|
||||
end compose
|
||||
|
||||
script sqrt
|
||||
on call(x)
|
||||
x ^ 0.5
|
||||
end call
|
||||
end script
|
||||
|
||||
script twice
|
||||
on call(x)
|
||||
2 * x
|
||||
end call
|
||||
end script
|
||||
|
||||
compose(sqrt, twice)'s call(32)
|
||||
-- Result: 8.0
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
------------ COMPOSITION OF A LIST OF FUNCTIONS ----------
|
||||
|
||||
-- compose :: [(a -> a)] -> (a -> a)
|
||||
on compose(fs)
|
||||
script
|
||||
on |λ|(x)
|
||||
script go
|
||||
on |λ|(a, f)
|
||||
mReturn(f)'s |λ|(a)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldr(go, x, fs)
|
||||
end |λ|
|
||||
end script
|
||||
end compose
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
on root(x)
|
||||
x ^ 0.5
|
||||
end root
|
||||
|
||||
on succ(x)
|
||||
x + 1
|
||||
end succ
|
||||
|
||||
on half(x)
|
||||
x / 2
|
||||
end half
|
||||
|
||||
on run
|
||||
tell compose({half, succ, root})
|
||||
|
||||
|λ|(5)
|
||||
|
||||
end tell
|
||||
--> 1.61803398875
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS -------------------
|
||||
|
||||
-- foldr :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldr(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from lng to 1 by -1
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldr
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
10 F$ = "SIN"
|
||||
20 DEF FN A(P) = ATN(P/SQR(-P*P+1))
|
||||
30 G$ = "FN A"
|
||||
40 GOSUB 100"COMPOSE
|
||||
50 SA$ = E$
|
||||
|
||||
60 X = .5 : E$ = SA$
|
||||
70 GOSUB 200"EXEC
|
||||
80 PRINT R
|
||||
90 END
|
||||
|
||||
100 E$ = F$ + "(" + G$ + "(X))" : RETURN : REMCOMPOSE F$ G$
|
||||
|
||||
200 D$ = CHR$(4) : FI$ = "TEMPORARY.EX" : M$ = CHR$(13)
|
||||
210 PRINT D$"OPEN"FI$M$D$"CLOSE"FI$M$D$"DELETE"FI$
|
||||
220 PRINT D$"OPEN"FI$M$D$"WRITE"FI$
|
||||
230 PRINT "CALL-998:CALL-958:R="E$":CONT"
|
||||
240 PRINT D$"CLOSE"FI$M$D$"EXEC"FI$:CALL-998:END:RETURN
|
||||
43
Task/Function-composition/Argile/function-composition.argile
Normal file
43
Task/Function-composition/Argile/function-composition.argile
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use std, math
|
||||
|
||||
let my_asin = new Function (.:<any,real x>:. -> real {asin x})
|
||||
let my__sin = new Function (.:<any,real x>:. -> real { sin x})
|
||||
let sinasin = my__sin o my_asin
|
||||
print sin asin 0.5
|
||||
print *my__sin 0.0
|
||||
print *sinasin 0.5
|
||||
~my_asin
|
||||
~my__sin
|
||||
~sinasin
|
||||
|
||||
=: <Function f> o <Function g> := -> Function {compose f g}
|
||||
|
||||
.:compose <Function f, Function g>:. -> Function
|
||||
use array
|
||||
let d = (new array of 2 Function)
|
||||
(d[0]) = f ; (d[1]) = g
|
||||
let c = new Function (.:<array of Function fg, real x>:. -> real {
|
||||
*fg[0]( *fg[1](x) )
|
||||
}) (d)
|
||||
c.del = .:<any>:.{free any}
|
||||
c
|
||||
|
||||
class Function
|
||||
function(any)(real)->(real) func
|
||||
any data
|
||||
function(any) del
|
||||
|
||||
=: * <Function f> <real x> := -> real
|
||||
Cgen "(*("(f.func)"))("(f.data)", "(x)")"
|
||||
|
||||
.: del Function <Function f> :.
|
||||
unless f.del is nil
|
||||
call f.del with f.data
|
||||
free f
|
||||
=: ~ <Function f> := {del Function f}
|
||||
|
||||
.: new Function <function(any)(real)-\>real func> (<any data>):. -> Function
|
||||
let f = new Function
|
||||
f.func = func
|
||||
f.data = data
|
||||
f
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
compose: function [f,g] ->
|
||||
return function [x].import:[f,g][
|
||||
call f @[call g @[x]]
|
||||
]
|
||||
|
||||
splitupper: compose 'split 'upper
|
||||
|
||||
print call 'splitupper ["done"]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
MsgBox % compose("sin","cos",1.5)
|
||||
|
||||
compose(f,g,x) { ; function composition
|
||||
Return %f%(%g%(x))
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
REM Create some functions for testing:
|
||||
DEF FNsqr(a) = SQR(a)
|
||||
DEF FNabs(a) = ABS(a)
|
||||
|
||||
REM Create the function composition:
|
||||
SqrAbs = FNcompose(FNsqr(), FNabs())
|
||||
|
||||
REM Test calling the composition:
|
||||
x = -2 : PRINT ; x, FN(SqrAbs)(x)
|
||||
END
|
||||
|
||||
DEF FNcompose(RETURN f%, RETURN g%)
|
||||
LOCAL f$, p% : DIM p% 7 : p%!0 = f% : p%!4 = g%
|
||||
f$ = "(x)=" + CHR$&A4 + "(&" + STR$~p% + ")(" + \
|
||||
\ CHR$&A4 + "(&" + STR$~(p%+4) + ")(x))"
|
||||
DIM p% LEN(f$) + 4 : $(p%+4) = f$ : !p% = p%+4
|
||||
= p%
|
||||
1
Task/Function-composition/BQN/function-composition-1.bqn
Normal file
1
Task/Function-composition/BQN/function-composition-1.bqn
Normal file
|
|
@ -0,0 +1 @@
|
|||
_compose_ ← ∘
|
||||
1
Task/Function-composition/BQN/function-composition-2.bqn
Normal file
1
Task/Function-composition/BQN/function-composition-2.bqn
Normal file
|
|
@ -0,0 +1 @@
|
|||
_compose_ ← {𝔽𝔾𝕩}
|
||||
1
Task/Function-composition/BQN/function-composition-3.bqn
Normal file
1
Task/Function-composition/BQN/function-composition-3.bqn
Normal file
|
|
@ -0,0 +1 @@
|
|||
Compose ← {𝕏∘𝕎}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
double sin (double v) { return Math.sin(v); }
|
||||
double asin (double v) { return Math.asin(v); }
|
||||
Var compose (Func f, Func g, double d) { return f(g(d)); }
|
||||
|
||||
void button1_onClick (Widget widget)
|
||||
{
|
||||
double d = compose(sin, asin, 0.5);
|
||||
label1.setText(d.toString(9));
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
0.500000000
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
( ( compose
|
||||
= f g
|
||||
. !arg:(?f.?g)&'(.($f)$(($g)$!arg))
|
||||
)
|
||||
& compose
|
||||
$ ( (=.flt$(!arg,2))
|
||||
. compose$((=.!arg*5/9).(=.!arg+-32))
|
||||
)
|
||||
: (=?FahrenheitToCelsius)
|
||||
& ( FahrenheitToCelsiusExample
|
||||
= deg
|
||||
. chu$(x2d$b0):?deg
|
||||
& out
|
||||
$ ( str
|
||||
$ (!arg " " !deg "F in " !deg "C = " FahrenheitToCelsius$!arg)
|
||||
)
|
||||
)
|
||||
& FahrenheitToCelsiusExample$0
|
||||
& FahrenheitToCelsiusExample$100
|
||||
)
|
||||
7
Task/Function-composition/Brat/function-composition.brat
Normal file
7
Task/Function-composition/Brat/function-composition.brat
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
compose = { f, g | { x | f g x } }
|
||||
|
||||
#Test
|
||||
add1 = { x | x + 1 }
|
||||
double = { x | x * 2 }
|
||||
b = compose(->double ->add1)
|
||||
p b 1 #should print 4
|
||||
35
Task/Function-composition/C++/function-composition-1.cpp
Normal file
35
Task/Function-composition/C++/function-composition-1.cpp
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include <functional>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
// functor class to be returned by compose function
|
||||
template <class Fun1, class Fun2>
|
||||
class compose_functor :
|
||||
public std::unary_function<typename Fun2::argument_type,
|
||||
typename Fun1::result_type>
|
||||
{
|
||||
protected:
|
||||
Fun1 f;
|
||||
Fun2 g;
|
||||
|
||||
public:
|
||||
compose_functor(const Fun1& _f, const Fun2& _g)
|
||||
: f(_f), g(_g) { }
|
||||
|
||||
typename Fun1::result_type
|
||||
operator()(const typename Fun2::argument_type& x) const
|
||||
{ return f(g(x)); }
|
||||
};
|
||||
|
||||
// we wrap it in a function so the compiler infers the template arguments
|
||||
// whereas if we used the class directly we would have to specify them explicitly
|
||||
template <class Fun1, class Fun2>
|
||||
inline compose_functor<Fun1, Fun2>
|
||||
compose(const Fun1& f, const Fun2& g)
|
||||
{ return compose_functor<Fun1,Fun2>(f, g); }
|
||||
|
||||
int main() {
|
||||
std::cout << compose(std::ptr_fun(::sin), std::ptr_fun(::asin))(0.5) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
14
Task/Function-composition/C++/function-composition-2.cpp
Normal file
14
Task/Function-composition/C++/function-composition-2.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <cmath>
|
||||
|
||||
template <typename A, typename B, typename C>
|
||||
std::function<C(A)> compose(std::function<C(B)> f, std::function<B(A)> g) {
|
||||
return [f,g](A x) { return f(g(x)); };
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::function<double(double)> f = sin;
|
||||
std::function<double(double)> g = asin;
|
||||
std::cout << compose(f, g)(0.5) << std::endl;
|
||||
}
|
||||
11
Task/Function-composition/C++/function-composition-3.cpp
Normal file
11
Task/Function-composition/C++/function-composition-3.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
template <class F, class G>
|
||||
decltype(auto) compose(F&& f, G&& g) {
|
||||
return [=](auto x) { return f(g(x)); };
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << compose(sin, asin)(0.5) << "\n";
|
||||
}
|
||||
7
Task/Function-composition/C++/function-composition-4.cpp
Normal file
7
Task/Function-composition/C++/function-composition-4.cpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <ext/functional>
|
||||
|
||||
int main() {
|
||||
std::cout << __gnu_cxx::compose1(std::ptr_fun(::sin), std::ptr_fun(::asin))(0.5) << std::endl;
|
||||
}
|
||||
10
Task/Function-composition/C++/function-composition-5.cpp
Normal file
10
Task/Function-composition/C++/function-composition-5.cpp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
auto compose(auto f, auto g) {
|
||||
return [=](auto x) { return f(g(x)); };
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << compose(sin, asin)(0.5) << "\n";
|
||||
}
|
||||
18
Task/Function-composition/C-sharp/function-composition.cs
Normal file
18
Task/Function-composition/C-sharp/function-composition.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Func<int, int> outfunc = Composer<int, int, int>.Compose(functA, functB);
|
||||
Console.WriteLine(outfunc(5)); //Prints 100
|
||||
}
|
||||
static int functA(int i) { return i * 10; }
|
||||
static int functB(int i) { return i + 5; }
|
||||
class Composer<A, B, C>
|
||||
{
|
||||
public static Func<C, A> Compose(Func<B, A> a, Func<C, B> b)
|
||||
{
|
||||
return delegate(C i) { return a(b(i)); };
|
||||
}
|
||||
}
|
||||
}
|
||||
64
Task/Function-composition/C/function-composition.c
Normal file
64
Task/Function-composition/C/function-composition.c
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
/* generic interface for functors from double to double */
|
||||
typedef struct double_to_double {
|
||||
double (*fn)(struct double_to_double *, double);
|
||||
} double_to_double;
|
||||
|
||||
#define CALL(f, x) f->fn(f, x)
|
||||
|
||||
|
||||
/* functor returned by compose */
|
||||
typedef struct compose_functor {
|
||||
double (*fn)(struct compose_functor *, double);
|
||||
double_to_double *f;
|
||||
double_to_double *g;
|
||||
} compose_functor;
|
||||
/* function to be used in "fn" in preceding functor */
|
||||
double compose_call(compose_functor *this, double x) {
|
||||
return CALL(this->f, CALL(this->g, x));
|
||||
}
|
||||
/* returns functor that is the composition of functors
|
||||
f & g. caller is responsible for deallocating memory */
|
||||
double_to_double *compose(double_to_double *f,
|
||||
double_to_double *g) {
|
||||
compose_functor *result = malloc(sizeof(compose_functor));
|
||||
result->fn = &compose_call;
|
||||
result->f = f;
|
||||
result->g = g;
|
||||
return (double_to_double *)result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include <math.h>
|
||||
|
||||
/* we can make functors for sin and asin by using
|
||||
the following as "fn" in a functor */
|
||||
double sin_call(double_to_double *this, double x) {
|
||||
return sin(x);
|
||||
}
|
||||
double asin_call(double_to_double *this, double x) {
|
||||
return asin(x);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
double_to_double *my_sin = malloc(sizeof(double_to_double));
|
||||
my_sin->fn = &sin_call;
|
||||
double_to_double *my_asin = malloc(sizeof(double_to_double));
|
||||
my_asin->fn = &asin_call;
|
||||
|
||||
double_to_double *sin_asin = compose(my_sin, my_asin);
|
||||
|
||||
printf("%f\n", CALL(sin_asin, 0.5)); /* prints "0.500000" */
|
||||
|
||||
free(sin_asin);
|
||||
free(my_sin);
|
||||
free(my_asin);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(defn compose [f g]
|
||||
(fn [x]
|
||||
(f (g x))))
|
||||
|
||||
; Example
|
||||
(def inc2 (compose inc inc))
|
||||
(println (inc2 5)) ; prints 7
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
compose = ( f, g ) -> ( x ) -> f g x
|
||||
|
||||
# Example
|
||||
add2 = ( x ) -> x + 2
|
||||
mul2 = ( x ) -> x * 2
|
||||
|
||||
mulFirst = compose add2, mul2
|
||||
addFirst = compose mul2, add2
|
||||
multiple = compose mul2, compose add2, mul2
|
||||
|
||||
console.log "add2 2 #=> #{ add2 2 }"
|
||||
console.log "mul2 2 #=> #{ mul2 2 }"
|
||||
console.log "mulFirst 2 #=> #{ mulFirst 2 }"
|
||||
console.log "addFirst 2 #=> #{ addFirst 2 }"
|
||||
console.log "multiple 2 #=> #{ multiple 2 }"
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
Function::of = (f) -> (args...) => @ f args...
|
||||
|
||||
# Example
|
||||
add2 = (x) -> x + 2
|
||||
mul2 = (x) -> x * 2
|
||||
|
||||
mulFirst = add2.of mul2
|
||||
addFirst = mul2.of add2
|
||||
multiple = mul2.of add2.of mul2
|
||||
|
||||
console.log "add2 2 #=> #{ add2 2 }"
|
||||
console.log "mul2 2 #=> #{ mul2 2 }"
|
||||
console.log "mulFirst 2 #=> #{ mulFirst 2 }"
|
||||
console.log "addFirst 2 #=> #{ addFirst 2 }"
|
||||
console.log "multiple 2 #=> #{ multiple 2 }"
|
||||
|
|
@ -0,0 +1 @@
|
|||
(defun compose (f g) (lambda (x) (funcall f (funcall g x))))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>(defun compose (f g) (lambda (x) (funcall f (funcall g x))))
|
||||
COMPOSE
|
||||
>(let ((sin-asin (compose #'sin #'asin)))
|
||||
(funcall sin-asin 0.5))
|
||||
0.5
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(defun compose (f g)
|
||||
(eval `(lambda (x) (funcall ',f (funcall ',g x)))))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
CL-USER> (defmacro compose (fn-name &rest args)
|
||||
(labels ((rec1 (args)
|
||||
(if (= (length args) 1)
|
||||
`(funcall ,@args x)
|
||||
`(funcall ,(first args) ,(rec1 (rest args))))))
|
||||
`(defun ,fn-name (x) ,(rec1 args))))
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
require "math"
|
||||
|
||||
def compose(f : Proc(T, _), g : Proc(_, _)) forall T
|
||||
return ->(x : T) { f.call(g.call(x)) }
|
||||
end
|
||||
|
||||
compose(->Math.sin(Float64), ->Math.asin(Float64)).call(0.5) #=> 0.5
|
||||
11
Task/Function-composition/D/function-composition.d
Normal file
11
Task/Function-composition/D/function-composition.d
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import std.stdio;
|
||||
|
||||
T delegate(S) compose(T, U, S)(in T delegate(U) f,
|
||||
in U delegate(S) g) {
|
||||
return s => f(g(s));
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(compose((int x) => x + 15, (int x) => x ^^ 2)(10));
|
||||
writeln(compose((int x) => x ^^ 2, (int x) => x + 15)(10));
|
||||
}
|
||||
37
Task/Function-composition/Delphi/function-composition.delphi
Normal file
37
Task/Function-composition/Delphi/function-composition.delphi
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
program AnonCompose;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
type
|
||||
TFunc = reference to function(Value: Integer): Integer;
|
||||
// Alternative: TFunc = TFunc<Integer,Integer>;
|
||||
|
||||
function Compose(F, G: TFunc): TFunc;
|
||||
begin
|
||||
Result:= function(Value: Integer): Integer
|
||||
begin
|
||||
Result:= F(G(Value));
|
||||
end
|
||||
end;
|
||||
|
||||
var
|
||||
Func1, Func2, Func3: TFunc;
|
||||
|
||||
begin
|
||||
Func1:=
|
||||
function(Value: Integer): Integer
|
||||
begin
|
||||
Result:= Value * 2;
|
||||
end;
|
||||
|
||||
Func2:=
|
||||
function(Value: Integer): Integer
|
||||
begin
|
||||
Result:= Value * 3;
|
||||
end;
|
||||
|
||||
Func3:= Compose(Func1, Func2);
|
||||
|
||||
Writeln(Func3(6)); // 36 = 6 * 3 * 2
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
set_namespace(rosettacode);
|
||||
|
||||
begin_funct(compose)_arg(f, g);
|
||||
[]_ret(x)_calc([f]([g]([x])));
|
||||
end_funct[];
|
||||
|
||||
me_msg()_funct(compose)_arg(f)_sin()_arg(g)_asin()_var(x)_value(0.5); // result: 0.5
|
||||
|
||||
reset_namespace[];
|
||||
13
Task/Function-composition/Diego/function-composition-2.diego
Normal file
13
Task/Function-composition/Diego/function-composition-2.diego
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
set_namespace(rosettacode);
|
||||
|
||||
with_funct(f)_arg({int}, x)_ret()_calc([x] * [x]);
|
||||
with_funct(g)_arg({int}, x)_ret()_calc([x] + 2);
|
||||
|
||||
begin_funct(compose)_arg(f, g);
|
||||
[]_ret(x)_calc([f]([g]([x])));
|
||||
end_funct[];
|
||||
|
||||
me_msg()_funct(compose)_arg(f)_funct(f)_arg(g)_funct(g)_var(x)_v(10); // result: 144
|
||||
// or me_msg()_funct(compose)_arg({f}, f)_arg({g}, g)_var(x)_v(10);
|
||||
|
||||
reset_ns[];
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
define method compose(f,g)
|
||||
method(x) f(g(x)) end
|
||||
end;
|
||||
3
Task/Function-composition/E/function-composition.e
Normal file
3
Task/Function-composition/E/function-composition.e
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def compose(f, g) {
|
||||
return fn x { return f(g(x)) }
|
||||
}
|
||||
16
Task/Function-composition/EchoLisp/function-composition-1.l
Normal file
16
Task/Function-composition/EchoLisp/function-composition-1.l
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
;; By decreasing order of performance
|
||||
;; 1) user definition : lambda and closure
|
||||
|
||||
(define (ucompose f g ) (lambda (x) ( f ( g x))))
|
||||
(ucompose sin cos)
|
||||
→ (🔒 λ (_x) (f (g _x)))
|
||||
|
||||
;; 2) built-in compose : lambda
|
||||
|
||||
(compose sin cos)
|
||||
→ (λ (_#:g1002) (#apply-compose (#list #cos #sin) _#:g1002))
|
||||
|
||||
;; 3) compiled composition
|
||||
|
||||
(define (sincos x) (sin (cos x)))
|
||||
sincos → (λ (_x) (⭕️ #sin (#cos _x)))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
((ucompose sin cos) 3) → -0.8360218615377305
|
||||
((compose sin cos) 3) → -0.8360218615377305
|
||||
(sincos 3) → -0.8360218615377305
|
||||
1
Task/Function-composition/Ela/function-composition-1.ela
Normal file
1
Task/Function-composition/Ela/function-composition-1.ela
Normal file
|
|
@ -0,0 +1 @@
|
|||
let compose f g x = f (g x)
|
||||
1
Task/Function-composition/Ela/function-composition-2.ela
Normal file
1
Task/Function-composition/Ela/function-composition-2.ela
Normal file
|
|
@ -0,0 +1 @@
|
|||
compose f g x = f (g x)
|
||||
14
Task/Function-composition/Elena/function-composition.elena
Normal file
14
Task/Function-composition/Elena/function-composition.elena
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import extensions;
|
||||
|
||||
extension op : Func1
|
||||
{
|
||||
compose(Func1 f)
|
||||
= (x => self(f(x)));
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
var fg := (x => x + 1).compose:(x => x * x);
|
||||
|
||||
console.printLine(fg(3))
|
||||
}
|
||||
11
Task/Function-composition/Elixir/function-composition.elixir
Normal file
11
Task/Function-composition/Elixir/function-composition.elixir
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
defmodule RC do
|
||||
def compose(f, g), do: fn(x) -> f.(g.(x)) end
|
||||
|
||||
def multicompose(fs), do: List.foldl(fs, fn(x) -> x end, &compose/2)
|
||||
end
|
||||
|
||||
sin_asin = RC.compose(&:math.sin/1, &:math.asin/1)
|
||||
IO.puts sin_asin.(0.5)
|
||||
|
||||
IO.puts RC.multicompose([&:math.sin/1, &:math.asin/1, fn x->1/x end]).(0.5)
|
||||
IO.puts RC.multicompose([&(&1*&1), &(1/&1), &(&1*&1)]).(0.5)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
;; lexical-binding: t
|
||||
(defun compose (f g)
|
||||
(lambda (x)
|
||||
(funcall f (funcall g x))))
|
||||
|
||||
(let ((func (compose '1+ '1+)))
|
||||
(funcall func 5)) ;=> 7
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(defun compose (f g)
|
||||
`(lambda (x) (,f (,g x))))
|
||||
|
||||
(let ((func (compose '1+ '1+)))
|
||||
(funcall func 5)) ;=> 7
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(defmacro compose (f g)
|
||||
`(lambda (x) (,f (,g x))))
|
||||
|
||||
(let ((func (compose 1+ 1+)))
|
||||
(funcall func 5)) ;=> 7
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
-module(fn).
|
||||
-export([compose/2, multicompose/1]).
|
||||
|
||||
compose(F,G) -> fun(X) -> F(G(X)) end.
|
||||
|
||||
multicompose(Fs) ->
|
||||
lists:foldl(fun compose/2, fun(X) -> X end, Fs).
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
1> (fn:compose(fun math:sin/1, fun math:asin/1))(0.5).
|
||||
0.5
|
||||
2> Sin_asin_plus1 = fn:multicompose([fun math:sin/1, fun math:asin/1, fun(X) -> X + 1 end]).
|
||||
#Fun<tests.0.59446746>
|
||||
82> Sin_asin_plus1(0.5).
|
||||
1.5
|
||||
9
Task/Function-composition/Es/function-composition-1.es
Normal file
9
Task/Function-composition/Es/function-composition-1.es
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fn compose f g {
|
||||
result @ {$g | $f}
|
||||
}
|
||||
|
||||
fn downvowel {tr AEIOU aeiou}
|
||||
fn upcase {tr a-z A-Z}
|
||||
fn-c = <={compose $fn-downvowel $fn-upcase}
|
||||
echo 'Cozy lummox gives smart squid who asks for job pen.' | c
|
||||
# => CoZY LuMMoX GiVeS SMaRT SQuiD WHo aSKS FoR JoB PeN.
|
||||
9
Task/Function-composition/Es/function-composition-2.es
Normal file
9
Task/Function-composition/Es/function-composition-2.es
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fn compose f g {
|
||||
result @ x {result <={$f <={$g $x}}}
|
||||
}
|
||||
|
||||
fn downvowel x {result `` '' {tr AEIOU aeiou <<< $x}}
|
||||
fn upcase x {result `` '' {tr a-z A-Z <<< $x}}
|
||||
fn-c = <={compose $fn-downvowel $fn-upcase}
|
||||
echo <={c 'Cozy lummox gives smart squid who asks for job pen.'}
|
||||
# => CoZY LuMMoX GiVeS SMaRT SQuiD WHo aSKS FoR JoB PeN.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
> let compose f g x = f (g x);;
|
||||
|
||||
val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
> let sin_asin = compose sin asin;;
|
||||
|
||||
val sin_asin : (float -> float)
|
||||
|
||||
> sin_asin 0.5;;
|
||||
val it : float = 0.5
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
( scratchpad ) [ 2 * ] [ 1 + ] compose .
|
||||
[ 2 * 1 + ]
|
||||
( scratchpad ) 4 [ 2 * ] [ 1 + ] compose call .
|
||||
9
|
||||
15
Task/Function-composition/Fantom/function-composition.fantom
Normal file
15
Task/Function-composition/Fantom/function-composition.fantom
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class Compose
|
||||
{
|
||||
static |Obj -> Obj| compose (|Obj -> Obj| fn1, |Obj -> Obj| fn2)
|
||||
{
|
||||
return |Obj x -> Obj| { fn2 (fn1 (x)) }
|
||||
}
|
||||
|
||||
public static Void main ()
|
||||
{
|
||||
double := |Int x -> Int| { 2 * x }
|
||||
|Int -> Int| quad := compose(double, double)
|
||||
echo ("Double 3 = ${double(3)}")
|
||||
echo ("Quadruple 3 = ${quad (3)}")
|
||||
}
|
||||
}
|
||||
9
Task/Function-composition/Forth/function-composition.fth
Normal file
9
Task/Function-composition/Forth/function-composition.fth
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
: compose ( xt1 xt2 -- xt3 )
|
||||
>r >r :noname
|
||||
r> compile,
|
||||
r> compile,
|
||||
postpone ;
|
||||
;
|
||||
|
||||
' 2* ' 1+ compose ( xt )
|
||||
3 swap execute . \ 7
|
||||
79
Task/Function-composition/Fortran/function-composition.f
Normal file
79
Task/Function-composition/Fortran/function-composition.f
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
module functions_module
|
||||
implicit none
|
||||
private ! all by default
|
||||
public :: f,g
|
||||
|
||||
contains
|
||||
|
||||
pure function f(x)
|
||||
implicit none
|
||||
real, intent(in) :: x
|
||||
real :: f
|
||||
f = sin(x)
|
||||
end function f
|
||||
|
||||
pure function g(x)
|
||||
implicit none
|
||||
real, intent(in) :: x
|
||||
real :: g
|
||||
g = cos(x)
|
||||
end function g
|
||||
|
||||
end module functions_module
|
||||
|
||||
module compose_module
|
||||
implicit none
|
||||
private ! all by default
|
||||
public :: compose
|
||||
|
||||
interface
|
||||
pure function f(x)
|
||||
implicit none
|
||||
real, intent(in) :: x
|
||||
real :: f
|
||||
end function f
|
||||
|
||||
pure function g(x)
|
||||
implicit none
|
||||
real, intent(in) :: x
|
||||
real :: g
|
||||
end function g
|
||||
end interface
|
||||
|
||||
contains
|
||||
|
||||
impure function compose(x, fi, gi)
|
||||
implicit none
|
||||
real, intent(in) :: x
|
||||
procedure(f), optional :: fi
|
||||
procedure(g), optional :: gi
|
||||
real :: compose
|
||||
|
||||
procedure (f), pointer, save :: fpi => null()
|
||||
procedure (g), pointer, save :: gpi => null()
|
||||
|
||||
if(present(fi) .and. present(gi))then
|
||||
fpi => fi
|
||||
gpi => gi
|
||||
compose = 0
|
||||
return
|
||||
endif
|
||||
|
||||
if(.not. associated(fpi)) error stop "fpi"
|
||||
if(.not. associated(gpi)) error stop "gpi"
|
||||
|
||||
compose = fpi(gpi(x))
|
||||
|
||||
contains
|
||||
|
||||
end function compose
|
||||
|
||||
end module compose_module
|
||||
|
||||
program test_compose
|
||||
use functions_module
|
||||
use compose_module
|
||||
implicit none
|
||||
write(*,*) "prepare compose:", compose(0.0, f,g)
|
||||
write(*,*) "run compose:", compose(0.5)
|
||||
end program test_compose
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
compose[\A, B, C\](f:A->B, g:B->C, i:Any): A->C = do
|
||||
f(g(i))
|
||||
end
|
||||
|
||||
composed(i:RR64): RR64 = compose(sin, cos, i)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
alt_compose(f:Number->RR64, g:Number->RR64, i:RR64): ()->RR64 = do
|
||||
f(g(i))
|
||||
end
|
||||
|
||||
alt_composed(i:RR64): RR64 = compose(sin, cos, i)
|
||||
|
|
@ -0,0 +1 @@
|
|||
opr_composed(i:Number): Number->RR64 = (sin COMPOSE cos)(i)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
function compose( f as function(as integer) as integer,_
|
||||
g as function(as integer) as integer,_
|
||||
n as integer ) as integer
|
||||
return f(g(n))
|
||||
end function
|
||||
7
Task/Function-composition/FunL/function-composition.funl
Normal file
7
Task/Function-composition/FunL/function-composition.funl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import math.{sin, asin}
|
||||
|
||||
def compose( f, g ) = x -> f( g(x) )
|
||||
|
||||
sin_asin = compose( sin, asin )
|
||||
|
||||
println( sin_asin(0.5) )
|
||||
7
Task/Function-composition/GAP/function-composition.gap
Normal file
7
Task/Function-composition/GAP/function-composition.gap
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Composition := function(f, g)
|
||||
return x -> f(g(x));
|
||||
end;
|
||||
|
||||
h := Composition(x -> x+1, x -> x*x);
|
||||
h(5);
|
||||
# 26
|
||||
11
Task/Function-composition/Go/function-composition-1.go
Normal file
11
Task/Function-composition/Go/function-composition-1.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// Go doesn't have generics, but sometimes a type definition helps
|
||||
// readability and maintainability. This example is written to
|
||||
// the following function type, which uses float64.
|
||||
type ffType func(float64) float64
|
||||
|
||||
// compose function requested by task
|
||||
func compose(f, g ffType) ffType {
|
||||
return func(x float64) float64 {
|
||||
return f(g(x))
|
||||
}
|
||||
}
|
||||
17
Task/Function-composition/Go/function-composition-2.go
Normal file
17
Task/Function-composition/Go/function-composition-2.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package main
|
||||
|
||||
import "math"
|
||||
import "fmt"
|
||||
|
||||
type ffType func(float64) float64
|
||||
|
||||
func compose(f, g ffType) ffType {
|
||||
return func(x float64) float64 {
|
||||
return f(g(x))
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
sin_asin := compose(math.Sin, math.Asin)
|
||||
fmt.Println(sin_asin(.5))
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
final times2 = { it * 2 }
|
||||
final plus1 = { it + 1 }
|
||||
|
||||
final plus1_then_times2 = times2 << plus1
|
||||
final times2_then_plus1 = times2 >> plus1
|
||||
|
||||
assert plus1_then_times2(3) == 8
|
||||
assert times2_then_plus1(3) == 7
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Prelude> let sin_asin = sin . asin
|
||||
Prelude> sin_asin 0.5
|
||||
0.49999999999999994
|
||||
|
|
@ -0,0 +1 @@
|
|||
(sin . asin) 0.5
|
||||
|
|
@ -0,0 +1 @@
|
|||
sin . asin $ 0.5
|
||||
|
|
@ -0,0 +1 @@
|
|||
compose f g x = f (g x)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Prelude> let compose f g x = f (g x)
|
||||
Prelude> let sin_asin = compose sin asin
|
||||
Prelude> sin_asin 0.5
|
||||
0.5
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
composeList :: [a -> a] -> a -> a
|
||||
composeList = flip (foldr id)
|
||||
|
||||
main :: IO ()
|
||||
main = print $ composeList [(/ 2), succ, sqrt] 5
|
||||
3
Task/Function-composition/Hy/function-composition.hy
Normal file
3
Task/Function-composition/Hy/function-composition.hy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(defn compose [f g]
|
||||
(fn [x]
|
||||
(f (g x))))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
x @ f # use this syntax in Icon instead of the Unicon f(x) to call co-expressions
|
||||
every push(fL := [],!rfL) # use this instead of reverse(fL) as the Icon reverse applies only to strings
|
||||
31
Task/Function-composition/Icon/function-composition-2.icon
Normal file
31
Task/Function-composition/Icon/function-composition-2.icon
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
procedure main(arglist)
|
||||
h := compose(sqrt,abs)
|
||||
k := compose(integer,"sqrt",ord)
|
||||
m := compose("-",k)
|
||||
every write(i := -2 to 2, " h=(sqrt,abs)-> ", h(i))
|
||||
every write(c := !"1@Q", " k=(integer,\"sqrt\",ord)-> ", k(c))
|
||||
write(c := "1"," m=(\"-\",k) -> ",m(c))
|
||||
end
|
||||
|
||||
invocable all # permit string invocations
|
||||
|
||||
procedure compose(fL[]) #: compose(f1,f2,...) returns the functional composition of f1,f2,... as a co-expression
|
||||
local x,f,saveSource
|
||||
|
||||
every case type(x := !fL) of {
|
||||
"procedure"|"co-expression": &null # procedures and co-expressions are fine
|
||||
"string" : if not proc(x,1) then runnerr(123,fL) # as are invocable strings (unary operators, and procedures)
|
||||
default: runerr(123,fL)
|
||||
}
|
||||
|
||||
fL := reverse(fL) # reverse and isolate from mutable side-effects
|
||||
cf := create { saveSource := &source # don't forget where we came from
|
||||
repeat {
|
||||
x := (x@saveSource)[1] # return result and resume here
|
||||
saveSource := &source # ...
|
||||
every f := !fL do x := f(x) # apply the list of 'functions'
|
||||
}
|
||||
}
|
||||
return (@cf, cf) # 'prime' the co-expr before returning it
|
||||
|
||||
end
|
||||
1
Task/Function-composition/J/function-composition-1.j
Normal file
1
Task/Function-composition/J/function-composition-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
compose =: @
|
||||
1
Task/Function-composition/J/function-composition-2.j
Normal file
1
Task/Function-composition/J/function-composition-2.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
f compose g
|
||||
3
Task/Function-composition/J/function-composition-3.j
Normal file
3
Task/Function-composition/J/function-composition-3.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
f=: >.@(1&o.)@%:
|
||||
g=: 1&+@|@(2&o.)
|
||||
h=: f@g
|
||||
2
Task/Function-composition/J/function-composition-4.j
Normal file
2
Task/Function-composition/J/function-composition-4.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(f, g, h) 1p1
|
||||
1 2 1
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(defn fahrenheit->celsius [deg-f]
|
||||
(/ (* (- deg-f 32) 5) 9))
|
||||
|
||||
(defn celsius->kelvin [deg-c]
|
||||
(+ deg-c 273.15))
|
||||
|
||||
(def fahrenheit->kelvin (comp celsius->kelvin fahrenheit->celsius))
|
||||
|
||||
(fahrenheit->kelvin 72) // 295.372
|
||||
33
Task/Function-composition/Java/function-composition-1.java
Normal file
33
Task/Function-composition/Java/function-composition-1.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
public class Compose {
|
||||
|
||||
// Java doesn't have function type so we define an interface
|
||||
// of function objects instead
|
||||
public interface Fun<A,B> {
|
||||
B call(A x);
|
||||
}
|
||||
|
||||
public static <A,B,C> Fun<A,C> compose(final Fun<B,C> f, final Fun<A,B> g) {
|
||||
return new Fun<A,C>() {
|
||||
public C call(A x) {
|
||||
return f.call(g.call(x));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Fun<Double,Double> sin = new Fun<Double,Double>() {
|
||||
public Double call(Double x) {
|
||||
return Math.sin(x);
|
||||
}
|
||||
};
|
||||
Fun<Double,Double> asin = new Fun<Double,Double>() {
|
||||
public Double call(Double x) {
|
||||
return Math.asin(x);
|
||||
}
|
||||
};
|
||||
|
||||
Fun<Double,Double> sin_asin = compose(sin, asin);
|
||||
|
||||
System.out.println(sin_asin.call(0.5)); // prints "0.5"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import java.util.function.Function;
|
||||
|
||||
public class Compose {
|
||||
public static void main(String[] args) {
|
||||
Function<Double,Double> sin_asin = ((Function<Double,Double>)Math::sin).compose(Math::asin);
|
||||
|
||||
System.out.println(sin_asin.apply(0.5)); // prints "0.5"
|
||||
}
|
||||
}
|
||||
13
Task/Function-composition/Java/function-composition-3.java
Normal file
13
Task/Function-composition/Java/function-composition-3.java
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import java.util.function.Function;
|
||||
|
||||
public class Compose {
|
||||
public static <A,B,C> Function<A,C> compose(Function<B,C> f, Function<A,B> g) {
|
||||
return x -> f.apply(g.apply(x));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Function<Double,Double> sin_asin = compose(Math::sin, Math::asin);
|
||||
|
||||
System.out.println(sin_asin.apply(0.5)); // prints "0.5"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
function compose(f, g) {
|
||||
return function(x) {
|
||||
return f(g(x));
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
var id = compose(Math.sin, Math.asin);
|
||||
console.log(id(0.5)); // 0.5
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
|
||||
// iterativeComposed :: [f] -> f
|
||||
function iterativeComposed(fs) {
|
||||
|
||||
return function (x) {
|
||||
var i = fs.length,
|
||||
e = x;
|
||||
|
||||
while (i--) e = fs[i](e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
// foldComposed :: [f] -> f
|
||||
function foldComposed(fs) {
|
||||
|
||||
return function (x) {
|
||||
return fs
|
||||
.reduceRight(function (a, f) {
|
||||
return f(a);
|
||||
}, x);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
var sqrt = Math.sqrt,
|
||||
|
||||
succ = function (x) {
|
||||
return x + 1;
|
||||
},
|
||||
|
||||
half = function (x) {
|
||||
return x / 2;
|
||||
};
|
||||
|
||||
|
||||
// Testing two different multiple composition ([f] -> f) functions
|
||||
|
||||
return [iterativeComposed, foldComposed]
|
||||
.map(function (compose) {
|
||||
|
||||
// both functions compose from right to left
|
||||
return compose([half, succ, sqrt])(5);
|
||||
|
||||
});
|
||||
})();
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
function compose(f, g) {
|
||||
return x => f(g(x));
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue