Function composition: Difference between revisions

From Rosetta Code
Content added Content deleted
(Javascript)
Line 84: Line 84:
0.5</lang>
0.5</lang>


=={{header|Javascript}}==
=={{header|JavaScript}}==
<lang javascript>
<lang javascript>
function compose(f, g) {
function compose(f, g) {

Revision as of 01:31, 4 March 2009

Task
Function composition
You are encouraged to solve this task according to the task description, using any language you may know.

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, (lets call the argument x), which works like applying function f to the result of applying function g to x.

I.e:

 compose(f, g)(x) == f( g(x) )

Reference: Function composition

ALGOL 68

Translation of: Python
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386

Note: Returning PROC (REAL x)REAL: f1(f2(x)) from a function apparently violates standard ALGOL 68's scoping rules. ALGOL 68G warns about this during parsing, and then rejects during runtime. <lang algol>MODE F = PROC(REAL)REAL; # ALGOL 68 is strong typed #

  1. 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 #

  1. Example use: #

F sin arc sin = compose(sin, arc sin); print((sin arc sin(0.5), (sin O arc sin)(0.5), new line))</lang> Output:

+.500000000000000e +0 +.500000000000000e +0

C++

Note: this is already implemented as __gnu_cxx::compose1() <lang cpp>#include <functional>

  1. include <cmath>
  2. 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;

}</lang>

Common Lisp

<lang lisp>(defun compose (f g) (lambda (x) (funcall f (funcall g x))))</lang> Example use: <lang lisp>>(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</lang>

Haskell

This is already defined as the . (dot) operator in Haskell. <lang haskell>compose f g x = f (g x)</lang> Example use: <lang haskell>Prelude> let compose f g x = f (g x) Prelude> let sin_asin = compose sin asin Prelude> sin_asin 0.5 0.5</lang>

JavaScript

<lang javascript>

function compose(f, g) {
  return function(x) { return f(g(x)) }
}
var id = compose(Math.sin, Math.asin)
print id(0.5)   //  0.5

</lang>

Joy

Composition is the default operation in Joy. The composition of two functions is the concatenation of those functions, in the order in which they are to be applied. <lang joy>

g f

</lang>

OCaml

<lang ocaml>let compose f g x = f (g x)</lang> Example use: <lang ocaml># let compose f g x = f (g x);; val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun>

  1. let sin_asin = compose sin asin;;

val sin_asin : float -> float = <fun>

  1. sin_asin 0.5;;

- : float = 0.5</lang>

Perl

<lang perl>sub compose

  {my ($f, $g) = @_;
   return sub {$f->($g->(@_))};}

use Math::Trig; print compose(sub {sin $_[0]}, \&asin)->(0.5), "\n";</lang>

Python

<lang python>compose = lambda f, g: lambda x: f( g(x) )</lang> Example use: <lang python>>>> compose = lambda f, g: lambda x: f( g(x) ) >>> from math import sin, asin >>> sin_asin = compose(sin, asin) >>> sin_asin(0.5) 0.5 >>> </lang>

Scheme

<lang scheme>(define (compose f g) (lambda (x) (f (g x))))</lang> Example use: <lang scheme>> (define (compose f g) (lambda (x) (f (g x)))) > (define sin_asin (compose sin asin)) > (sin_asin 0.5) 0.5</lang>