slambda 0.8.5

Short Lambda expressions in a tiny library

Released 2016-03-17.

To install, run:

haxelib install slambda 0.8.5

See using Haxelib in Haxelib documentation for more information.

Maintainerciscoheat
Websitehttps://github.com/ciscoheat/slambda
Current version0.8.5
StatisticsInstalled 1140 times
LicenseMIT
Tags lambda

README.md

Slambda

Pronounced "slam-da", stands for Short Lambda. A tiny library that enables lambda expressions for Haxe in a simple way.

Can be used with the common => syntax, but also in a shorter format where _ is assumed to be the first parameter. If more than one argument, use _1 and _2, etc.

Examples

// This line is all you need to enable the library:
using Slambda;

class Main {
	static function main() {
		var a : Dynamic;
		
		// Short version, parameter name is set to "_"
		a = [1, 2, 3].filter.fn(_ > 1);
		trace(a); // [2, 3]

		// Normal version, arrow syntax:
		a = [1, 2, 3].filter.fn(x => x > 1);
		trace(a); // [2, 3]

		// For multiple arguments, use _1, _2, etc.
		a = [1, 1, 1].mapi.fn(_1 + _2);
		trace(a); // [1,2,3]

		// Multiple arguments can also be used with arrow syntax, 
		// but must then use brackets:
		a = [1, 1, 1].mapi.fn([i, a] => i + a);
		trace(a); // [1,2,3]

		// When used as a static extension, add rest arguments
		// for functions like fold:
		a = [1, 1, 1].fold.fn(_1 + _2, 10);
		trace(a); // 13

		// Chainable
		a = [1, 2, 3].filter.fn(_ > 1).filter.fn(y => y > 2);
		trace(a); // [3]

		// Works inside (single-quoted) strings too
		a = [1].map.fn('<b>$_</b>');
		trace(a); // ["<b>1</b>"]
	}
}

If you import Slambda.fn, you can use it without the static extension:

import Slambda.fn;

// Only Lambda this time for the sake of the example.
using Lambda;

class Main {
	static function main() {
		var a = [1, 2, 3].filter(fn(_ > 1));
	}
}

Installation

haxelib install slambda, then use it in a hxml file with -lib slambda.