User:Coderjoe/Sandbox2

From Rosetta Code

Groovy

Solution: <lang groovy>def compose = { f, g -> { x -> f(g(x)) } }</lang>

Test program: <lang groovy>def cube = { it * it * it } def cubeRoot = { it ** (1/3) }

funcList = [ Math.&sin, Math.&cos, cube ] inverseList = [ Math.&asin, Math.&acos, cubeRoot ]

println [funcList, inverseList].transpose().collect { compose(it[0],it[1]) }.collect{ it(0.5) } println [inverseList, funcList].transpose().collect { compose(it[0],it[1]) }.collect{ it(0.5) }</lang>

Output:

[0.5, 0.4999999999999999, 0.5000000000346574]
[0.5, 0.4999999999999999, 0.5000000000346574]

Haskell

<lang haskell>Prelude> let cube x = x ^ 3 Prelude> let croot x = x ** (1/3) Prelude> let compose f g = \x -> f (g x) -- this is already implemented in Haskell as the "." operator Prelude> -- we could have written "let compose f g x = f (g x)" but we show this for clarity Prelude> let funclist = [sin, cos, cube] Prelude> let funclisti = [asin, acos, croot] Prelude> zipWith (\f inversef -> (compose inversef f) 0.5) funclist funclisti [0.5,0.4999999999999999,0.5]</lang>

Icon and Unicon

The Unicon solution can be modified to work in Icon. See Function_composition#Icon_and_Unicon. <lang Unicon>link compose procedure main(arglist)

   fun := [sin,cos,cube]
   inv := [asin,acos,cuberoot]
   x := 0.5
   every i := 1 to *inv do 
      write("f(",x,") := ", compose(inv[i],fun[i])(x))

end

procedure cube(x) return x*x*x end

procedure cuberoot(x) return x ^ (1./3) end</lang> Please refer to See Function_composition#Icon_and_Unicon for 'compose'.

Sample Output:

f(0.5) := 0.5
f(0.5) := 0.4999999999999999
f(0.5) := 0.5

J

J has some subtleties which are not addressed in this specification (J functions have grammatical character and their gerundial form may be placed in data structures where the spec sort of implies that there be no such distinction).

However, here are the basics which were requested: <lang j> sin=: 1&o.

  cos=:  2&o.
 cube=: ^&3

square=: *:

 unqo=: `:6
 unqcol=: `:0
 quot=: 1 :'{.u`
 A=: sin`cos`cube`square
 B=: monad def'y unqo inv quot'"0 A
 BA=. A dyad def'x unqo@(y unqo) quot'"0 B</lang>

<lang> A unqcol 0.5 0.479426 0.877583 0.125 0.25

  BA unqcol 0.5

0.5 0.5 0.5 0.5</lang>

Java

Java doesn't technically have first-class functions. Java can simulate first-class functions to a certain extent, with anonymous classes and generic function interface.

<lang java> public static Function<Double, Double> compose( final Function<Double, Double> f, final Function<Double, Double> g) { return new Function<Double, Double>() { @Override public Double apply(Double x) { return f.apply(g.apply(x)); } }; }

@SuppressWarnings("unchecked") public static void main(String[] args) { ArrayList<Function<Double, Double>> functions = Lists.newArrayList( new Function<Double, Double>() { @Override public Double apply(Double x) { return Math.cos(x); } }, new Function<Double, Double>() { @Override public Double apply(Double x) { return Math.tan(x); } }, new Function<Double, Double>() { @Override public Double apply(Double x) { return x * x; } }); ArrayList<Function<Double, Double>> inverse = Lists.newArrayList( new Function<Double, Double>() { @Override public Double apply(Double x) { return Math.acos(x); } }, new Function<Double, Double>() { @Override public Double apply(Double x) { return Math.atan(x); } }, new Function<Double, Double>() { @Override public Double apply(Double x) { return Math.sqrt(x); } }); for (int i = 0; i < functions.size(); i++) { System.out.println(compose(functions.get(i), inverse.get(i)).apply(0.5)); } } </lang>

JavaScript

assuming the print function is provided by the environment, like a stand-alone shell. In browsers, use alert(), document.write() or similar

<lang javascript>var compose = function (f, g) {

   return function (x) { 
       return f(g(x)); 
   }; 

};

var fn = [Math.sin, Math.cos, function (x) { return Math.pow(x, 3); }]; var inv = [Math.asin, Math.acos, function (x) { return Math.pow(x, 1/3); }];

(function () {

   for (var i = 0; i < 3; i++) {
       var f = compose(inv[i], fn[i]);
       print(f(0.5));    // 0.5
   }

})();

</lang>

Lua

<lang lua> function compose(f,g) return function(...) return f(g(...)) end end

fn = {math.sin, math.cos, function(x) return x^3 end} inv = {math.asin, math.acos, function(x) return x^(1/3) end}

for i, v in ipairs(fn)

 local f = compose(v, inv[i])
 print(f(0.5))     --> 0.5

end</lang>

Mathematica

The built-in function Composition can do composition, a custom function that does the same would be compose[f_,g_]:=f[g[#]]&. However the latter only works with 2 arguments, Composition works with any number of arguments. <lang Mathematica>funcs = {Sin, Cos, #^3 &}; funcsi = {ArcSin, ArcCos, #^(1/3) &}; compositefuncs = Composition @@@ Transpose[{funcs, funcsi}]; Table[i[0.666], {i, compositefuncs}]</lang> gives back: <lang Mathematica>{0.666, 0.666, 0.666}</lang> Note that I implemented cube and cube-root as pure functions. This shows that Mathematica is fully able to handle functions as variables, functions can return functions, and functions can be given as an argument. Composition can be done in more than 1 way: <lang Mathematica>Composition[f,g,h][x] f@g@h@x x//h//g//f</lang> all give back: <lang Mathematica>f[g[h[x]]]</lang>