kruhft / cl-short-lambda

Provides a shorthand way to specifiy lambda functions a'la Arc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

short-lambda - a shorthand for defining lambda functions
--------------------------------------------------------

This module implements the reader macro #[...] which is used to
define short lambda functions that can be passed to functions such
as map, mapcar and maplist.

 The following is an example usage:

	* (map #[ + _ 1 ] '(1 2 3)
	(2 3 4)

Notice the automatic definition of the variable _. This is the
argument that is passed to your function.

The macro parameter can be used to pass the number of arguments
that you would like defined in the resulting function.  Variable
names follow the pattern of: _ (1st), __ (2nd), ___ (3rd), etc.
Notice that this can get a bit unwieldy, so it is not really
reccomended, but available if needed.  Here's an example:

	* (mapcar #3[ + _ __ ___ ] '(1 2 3) '(1 2 3) '(1 2 3))
	(3 6 9)

They can also be nested, like so:

	* (mapcar #2[mapcar #[ + _ 2] (append (list _ _) (list _ __))]
		'(1 2 3) '(1 2 3))
	((3 3 3 3) (4 4 4 4) (5 5 5 5))

Astute readers might notice the similarity to a syntax feature of
Arc.  I just thought it would be interesting to write.  Enjoy.

Bugs
----

- ] characters prior to the end of the #[...] sequence will cause
  the reader to break ie. (#[map nil #[print _] "[abc]"] 0)

Author: Burton Samograd <kruhft@gmail.com>
Date: May 26, 2011

License
-------

cl-short-lambda is distributed under this license:

Copyright (c) 2011, Burton Samograd

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.

    * Neither the names of the copyright owners nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

About

Provides a shorthand way to specifiy lambda functions a'la Arc


Languages

Language:Common Lisp 100.0%