Compose Function

compose f g x = f (g x)

Isn't this just the benifits of native support for functional composition and currying [see: CurryingSchonfinkelling] or is it really the name a HigherOrderFunction?

No it is not the same as currying. Composing f and g returns a function that takes one argument and applies f to the result of applying g to its argument. In Haskell the ComposeFunction? is denoted by '.', so (f . g) is the same as (lambda x (f (g x))). Currying would give (lambda x (f g x)) which is completely different.

Cool! I think I get it now! It's seems kind of like the std::binder1st, std::binder2nd, and std::compose classes in STL. Where you can, for example, partially specify the arguments of a binary function to make it evaluate as a unary function?

Partially specifying the arguments of a binary function is called partial application, which you get ForFree if all your functions are curried. This is not directly related to ComposeFunction.

Great, that makes sense. However, it seems like composition, at least the way we use it in CeePlusPlus, is not as useful without having these binders for partial application. For example:

   std::find(
      seq.begin(), seq.end(),
      std::compose2(
          std::and(),
          std::bind2nd( std::greater(), 0 ),
          std::bind2nd( std::less(), 11 ) );

This composes a binary and operation with two unary operations like so that executing:

  and( x ) 

is really executing:

  f = bind_second_parameter_of( greater(), 0 );
  g = bind_second_parameter_of( less(), 0 );

o = and( f( x ), g( x ) );

Which, in this example is used to find the first x in the sequence that is in the range of [1,11). Is this similar in intent? In BlocksInJava I provide classes like BinderFirst, BinderSecond, UnaryCompose, and BinaryCompose to do the same thing as this CeePlusPlus example.

I'm not sure. For one, this compose2 takes 3 arguments. It seems to be equivalent to the following Haskell:

  compose2 f g h x = f (g x) (h x)

Okay, this is the same...well, at least as close to the same as CeePlusPlus can get with classes, templates, and function objects!!

In SchemeLanguage:

  (define (compose f g)
    (lambda (x)
      (f (g x))))

Or, if you want to take advantage of Scheme's multiple values:

  (define (compose f g)
    (lambda (x)
      (call-with-values (lambda () (g x)) f)))

In this case, F takes as many arguments as G returns. Even better would be to return an n-ary function:

  (define (compose f g)
    (lambda args
      (call-with-values (lambda () (apply g args)) f)))

And, better yet, to define COMPOSE itself to be n-ary:

  (define (compose f . rest)
    (if (null? rest)
      f
      (let ((g (apply compose rest)))
        (lambda args
          (call-with-values (lambda () (apply g args)) f)))))

--Riastradh


In PythonLanguage:

  def compose(f, g):
    return lambda x, f=f, g=g: f(g(x))


In CsharpLanguage:

  public static Converter<T1, T3> Compose<T1, T2, T3>(Converter<T2, T3> f, Converter<T1, T2> g) {
    return delegate(T1 x) { return f(g(x)); };
  }


In the StandardTemplateLibrary:

  std::compose1( std::ptr_fun( f ), std::ptr_fun( g ) );


In BlocksInJava:

  UnaryFunction
  compose1( UnaryFunction f, UnaryFunction g ) {
      return new UnaryCompose( f, g );
  }

For example given the following FunctorObjects:

  UnaryFunction f =
     new UnaryFunction() {
         public Object eval( Object x ) {
             return x.toString() + "f"; } };

UnaryFunction g = new UnaryFunction() { public Object eval( Object x ) { return x.toString() + "g"; } };

You can do the following:

  o = compose1( f, g );
  o.eval( x ); 

This creates the string:

  "xgf"


JavaScript:

 function compose(f,g) {
   return function(x) { return f(g(x)) }
 }

function square(x) { return x*x } function halve(x) { return x/2 } assert( compose( halve, square ) (4) == 8 )

See http://ling.ucsd.edu/~barker/Iota/ where JavaScript and composition are used to simulate the combinative languages Iota and Jot.


JoyLanguage:

Functional composition is expressed in Joy by simple concatenation. In a sense, it is the default operator.


In OcamlLanguage:

  let compose f g x = f (g x)


HaskellLanguage has a pre-defined operator (.) for composition.

 Prelude> ( (`div` 2) . (^2) ) 4
 8

It could have been defined like this:

  (f . g) x = f (g x)


SmlLanguage has a pre-defined operator (o) for composition.

 - ( (fn x => x div 2) o (fn x => x * x) ) 4;
 val it = 8 : int

It could have been defined like this:

  fun op o (f, g) x = f (g x)


TeXnicard:
 @S Compose
 @. ( code code -- code )
 rB[x]+rB[x]++
You can also just use the + operator although that won't work in many cases, such as if one or both operands are numbers or if the first code is a string that ends with a number in decimal notation.


See also CommonHigherOrderFunctions FunctionalComposition InverseFunctionalComposition

CategoryFunctionalProgramming CategoryInManyProgrammingLanguages

EditText of this page (last edited May 16, 2011) or FindPage with title or text search