Function composition: Difference between revisions

→‎{{header|Common Lisp}}: create a crystal implementation
(→‎{{header|Common Lisp}}: create a crystal implementation)
Line 719:
1
CL-USER> </pre>
 
=={{header|Crystal}}==
Crystal requires using closures for function composition. Since the only type the compiler can't infer for <code>compose</code> is the type of <code>x</code>, the type of the first argument to <code>f</code> has to be specified as the generic type <code>T</code>.
<lang ruby>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</lang>
The types for <code>f</code>'s output, <code>g</code>'s input and output, and the result of <code>compose</code> can all be inferred, but could be specified verbosely with <code>def compose(f : Proc(T, U), g : Proc(U, V)) : Proc(T, V) forall T, U, V</code>
 
=={{header|D}}==
Anonymous user