Nice/stdlib/nice/functional higher-order.nice,NONE,1.1

Bryn Keller <[email protected]>
Newsgroups gmane.comp.lang.nice.cvs
Message-ID <[email protected]>
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 <[email protected]>
 */
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;
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.