User Resources
› What is Dylan? › Learning Dylan › Downloads › Documentation › Wiki › Community › Dylan Competes › Supported Platforms › Screenshots › Current Limitations › Our Goals
Developer Resources
› Repository Access › Browse Repository › Bug Tracker › CVSZilla Search › Buildbot › Current Projects › Dev Tools › Submit News

[ All Fragments ]

Closures

Dylan allows you to create functions at runtime. The following example demonstrates disjoin, which can be used to combine two or more functions into one. Each function should take a single argument and return either true or false. The created function will take a single argument and return true if any of the original functions returned true.

Also note the use of a standard C header in this example.

define interface
  #include "ctype.h",
    import: {"isalpha" => is-alphabetic?,
	     "isdigit" => is-numeric?},
    map: {"int" => <boolean>};
end interface;

define constant is-alphanumeric? = disjoin(is-alphabetic?, is-numeric?);

It's easy to create functions at runtime. The following example uses method to create a closure, which is a combination of code and stored data.

define function make-linear-mapper
    (times :: <integer>, plus :: <integer>)
 => (mapper :: <function>)
  method (x)
    times * x + plus;
  end method;
end function;

define constant times-two-plus-one = make-linear-mapper(2, 1);

times-two-plus-one(5);
// Returns 11.