Branch: Tag:

2003-11-20

2003-11-20 16:01:06 by Marcus Comstedt <marcus@mc.pp.se>

Added Y and curry.

Rev: lib/modules/Function.pmod:1.11

39:   {    return f(@args, @extra);   } +  +  + //! The dreaded fixpoint combinator "Y". + //! + //! The Y combinator is useful when writing recursive lambdas. It + //! converts a lambda that expects a self-reference as its first argument + //! into one which can be called without this argument. + //! + //! @example + //! This example creates a lambda that computes the faculty function. + //! @code + //! Function.Y(lambda(function f, int n) { return n>1? n*f(n-1) : 1; }) + //! @endcode + function Y(function f) + { +  return lambda(function p) { +  return lambda(mixed ... args) { +  return f(p(p), @args); +  }; +  } (lambda(function p) { +  return lambda(mixed ... args) { +  return f(p(p), @args); +  }; +  }); + } +  +  + //! Partially evaluate a function call. + //! + //! This function allows N parameters to be given to a function taking + //! M parameters (N<=M), yielding a new function taking M-N parameters. + //! + //! What is actually returned from this function is a function taking N + //! parameters, and returning a function taking M-N parameters. + //! + //! @example + //! This example creates a function adding 7 to its argument. + //! @code + //! Function.curry(`+)(7) + //! @endcode + function curry(function f) + { +  return lambda(mixed ... args1) { +  return lambda(mixed ... args2) { +  return f(@args1, @args2); +  }; +  }; + }