Skip to content

dreampiggy/functional.js

Repository files navigation

Functional.js

npm travis License Star

Functional syntax sugar and more, for JavaScript

logo

Why Functional?

1 .Speed up your develop!

2. Build a high-level logic!

3. For fun, isn't it?

Install

Of course. Install by npm with one line

npm install functionaljs

Build

Because ECMAScript 6 Standard has released, so we all use this to build. But not all runtime support the standard.

So, if you use Node.js v5.0.0 or higher, you can use pure *-ec6.js to run. Otherwise use the compiled js(without -ec6) by Babel

You can install Babel through npm and use node-babel instead of node to run example.

sudo npm install -g babel-cli

We recommend to use Sublime Text with Babel plugin. (Which support EC6 and JSX syntax better) and here is a build-system for Sublime in Other/Babel.sublime-build. One short-key Command + Shift + B to run.

Async

What is async Wikipedia About future and promise

  • Async foreach call
var ForEach = require('functionaljs').ForEach;
var fs = require('fs');
var arr = ['/etc/hosts', '/etc/paths', '/etc/donthavethisfile'];

var eachFunc = function (currentValue, index, array) {
	var done = this.async();
	fs.readFile(currentValue, {encoding:'utf-8'}, function (err,result) {
		if (err) {
			console.error('file: ' + currentValue + ' not found');
			done(false); //false to early abort;
		}
		else {
			console.log('file: ' + currentValue + ' load');
			console.log(result);
			done(true); //true to set async have done.
		};
	})
}

ForEach(arr, eachFunc, function () {
	console.log('foreach end');
})

Promise

What is promise? Standard 中文说明

  • Promise

Just use Node.js built-in Promise(from 0.12), or use Promise for ancient version Node.js

  • Promisify

Convert callback function to promise object

var promisify = require('functionaljs').Promisify;
var fs = require('fs');
var readFile = promisify(fs.readFile);
readFile('/etc/hosts', {encoding:'utf-8' ,flag:'r'}).then(function (contents) {
	console.log('promise:\n' + contents);
}).catch(function(error) {
	console.error(error);
});

Monad

What is monad? Wikipedia 中文说明

var Monad = require('functionaljs').Monad;
//Use
var monad = new Monad;
var monad = new Monad(10);

monad.unit(20);
monad.unit(new Monad(30));

monad.bind(function (value) { return value });
var result = monad.bind(function (value) {
	return value.extract() * 2;
})

console.log(result.extract());//60

Optional

What is optional? Wikipedia Optional in Swift

var Optional = require('functionaljs').Optional;
//Use
var op = new Optional(1);

op.present(function (value) {
	console.log(value);//1
});

var result = op.map(function (value) {
	return ++value;
}).map(function (value) {
	return value *= 2;
}).filter(function (value) {
	return value === 4 ? true : false;
}).flatMap(function (value) {
	return new Optional(value);
});

if (result.empty()) {
	result.or(0);
}
console.log(result.get());//4

Curry

What is curry? Wikipedia 中文说明

Attention. Use Function.curry call may be conflict with your custom function curry prototype(if you do so). Please set it to undefined if you don't want

var curry = require('functionaljs').Curry;
//Use by invoking
var multiply = function (a, b) {
	return a * b;
}
var double = curry(multiply, function (args) {
	args.push(2);	//which means multiply(2, x)
	return args;
});

//Use by function prototype
var square = multiply.curry(function (args) {
	args.push(args[0]);	//which means multiply(x, x)
	return args;
})
console.log(multiply(3, 4));//12
console.log(double(3));//6
console.log(square(4));//16

Combinator

What is Y-combinator? Wikipedia 知乎来源

Attention: for strict languages(which means function call by value) such as JavaScript, using original Y-combinator will cause stack overflow because calling f(x(x)) means the compiler(interpreter) will try to generate accuracy defination with infinity stack alloc. So we use Z-combinator to actually implement Y-combinator. For more, see: Z-combinator

  • Y-combinator
/*
We can build a `pure` lambda(anonymous function) with Y-combinator.
See `y-test.js` and learn from step by step what `Y-combinator` is
*/

var Y = require('functionaljs').Y;
var fibonacci = Y(function(f) {
	return function(n) {
		return n == 0 ? 1 : n * f(n - 1);
	}
})
console.log(fibonacci(5))

Lazy

What is lazy? Wikipedia 中文说明

Thanks to @pkrumins

var Lazy = require('functionaljs').Lazy;

//Lazy range
Lazy.range(10).filter(function (e) {
		return e % 2 == 0
	})
	.take(5)
	.map(function (e) {
		return e * 2;
	})
	.forEach(function (e) {
		console.log(e);
	});
//0 4 8 12 16

Retroactive(Data Structure)

What is retroactive? Wikipedia Paper Reference

At now just implement retoractive queue

var retroactive = require('functionaljs').Retroactive;

//partial construct with false or null, fully with true

var partial = new retroactive(false);
var fully = new retroactive(true);

//next use property to generate new object

var partialQueue = new partial.queue();

function partialQueueTest(queue) {
	queue.insert(queue.push(1), 0);
	queue.insert(queue.push(2), 1);
	queue.query();	//[1,2]
	queue.insert(queue.pop(), 2);
	queue.query();	//[2]
	queue.delete(2);
	queue.query();	//[1,2]
	queue.delete(0);
	queue.query();	//[2]
}

License MIT

About

Async & Promise & Monad & Optional & Curry & DS, etc

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published