D Programming Language 2.0

- Comments
- D
- Search
- Home

Last update Sun Oct 11 21:34:25 2009

# std.functional

Functions that manipulate other functions.

License:
Boost License 1.0.

Authors:
Andrei Alexandrescu

Copyright Andrei Alexandrescu 2008 - 2009. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at ) http:
//www.boost.org/LICENSE_1_0.txt

template unaryFun(alias funbody,bool byRef = false,string parmName = "a")
  Transforms a string representing an expression into a unary function. The string must use symbol name a as the parameter.

  Example:

      alias unaryFun!("(a & 1) == 0") isEven;
      assert(isEven(2) && !isEven(1));

template binaryFun(alias funbody,string parm1Name = "a",string parm2Name = "b")
  Transforms a string representing an expression into a Boolean binary predicate. The string must use symbol names a and b as the compared elements.

  Example:

      alias binaryFun!("a < b") less;
      assert(less(1, 2) && !less(2, 1));
      alias binaryFun!("a > b") greater;
      assert(!greater("1", "2") && greater("2", "1"));

template not(alias pred)
  Negates predicate pred.

  Example:

      string a = "   Hello, world!";
      assert(find!(not!isspace)(a) == "Hello, world!");

template curry(alias fun,alias arg)
  Curries fun by tying its first argument to a particular value.

  Example:

      int fun(int a, int b) { return a + b; }
      alias curry!(fun, 5) fun5;
      assert(fun5(6) == 11);

  Note that in most cases you'd use an alias instead of a value assignment. Using an alias allows you to curry template functions without committing to a particular type of the function.

template adjoin(F...)
  Takes multiple functions and adjoins them together. The result is a std.typecons.Tuple with one element per passed-in function. Upon invocation, the returned tuple is the adjoined results of all functions.

  Example:

      static bool f1(int a) { return a != 0; }
      static int f2(int a) { return a / 2; }
      auto x = adjoin!(f1, f2)(5);
      assert(is(typeof(x) == Tuple!(bool, int)));
      assert(x._0 == true && x.field[1] == 2);

template compose(fun...)
  Composes passed-in functions fun[0], fun[1], ... returning a function f(x) that in turn returns fun[0](fun[1](...(x))).... Each function can be a regular functions, a delegate, or a string.

  Example:

      // First split a string in whitespace-separated tokens and then
      // convert each token into an integer
      assert(compose!(map!(to!(int)), split)("1 2 3") == [1, 2, 3]);

template pipe(fun...)
  Pipes functions in sequence. Offers the same functionality as compose, but with functions specified in reverse order. This may lead to more readable code in some situation because the order of execution is the same as lexical order.

  Example:

      // Read an entire text file, split the resulting string in
      // whitespace-separated tokens, and then convert each token into an
      // integer
      int[] a = pipe!(readText, split, map!(to!(int)))("file.txt");

Copyright Andrei Alexandrescu 2008 - 2009. | Page generated by Ddoc.