Update of /cvsroot/nice/Nice/stdlib/nice/functional
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28672/stdlib/nice/functional
Added Files:
higher-order.nice
Log Message:
Some useful odds and ends
--- NEW FILE: higher-order.nice ---
/****************************************************************************
* N I C E *
* A high-level object-oriented research language *
* (c) Daniel Bonniot 2003 *
* *
* This package is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
* *
* As a special exception, the copyright holders of this library give you *
* permission to link this library with independent modules to produce an *
* executable, regardless of the license terms of these independent *
* modules, and to copy and distribute the resulting executable under *
* terms of your choice. *
****************************************************************************/
/**
* Higher-order functions - that is, functions which take other
* functions as arguments, or which return functions.
*
* @author Bryn Keller <xo...@xo...>
*/
package nice.functional;
/**
* Returns a function which returns the opposite of what
* the original function would return.
*/
<T> T->boolean not(T->boolean func) = T t => !func(t);
/**
* Returns a function which always returns the same result,
* no matter what argument it's called with.
*/
<T,U> U->T always(T constant) = U u => constant;
/**
* Does nothing, simply returns the object it's passed.
*/
<T> T id(T thing) = thing;
//Tests
void _testNot() {
let f = int i => i > 1;
assert f(2) == true && not(f)(2) == false;
}
void _testAlways() {
let f = always(2);
assert f(1) == 2 && f(3) == 2;
}
void _testID() {
let o = new Object();
assert id(o) == o;
}
|