GithubHelp home page GithubHelp logo

aryankaushik / functional-programming Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samarpanda/functional-programming

1.0 2.0 0.0 190 KB

Functional programming concepts --> Functions are nouns. A goal of functional programming is to write functions that take in a lot of arguments, but only give them one argument at a time. This process is called currying.

License: MIT License

JavaScript 83.65% HTML 13.57% CSS 2.78%
functional-programming js

functional-programming's Introduction

Functional Programming Concepts

Curry

Functions are nouns. A goal of functional programming is to write functions that take in a lot of arguments, but only give them one argument at a time. This process is called currying.

Thunkify = 1 step curry Separate work from control flow using functional programming

/**
 * Curry function
 */
var curry = function(fn){
	return function(){
		if(fn.length > arguments.length){
			var slice = Array.prototype.slice;
			var args = slice.apply(arguments);
			return function(){
				return fn.apply(null, args.concat(slice.apply(arguments)));
			};
		}else
		  return fn.apply(null, arguments);
	};
}

/**
 * Subcurry Function
 */
function sub_curry (fn) {
	var args = Array.prototype.slice.call(arguments, 1);
	function f () {
		return fn.apply(this, args.concat(toArray(arguments)));
	}
	return f;
}

/**
 * To Array
 */
function toArray (args) {
	return [].slice.call(args);
}

Compose

/**
 * Compose function
 */
function compose(g, f){
	return function(x){
		return g(f(x));
	}
}

/**
 * Reverse String
 */
function reverse(s){
	return s.split('').reverse().join('');
}

/**
 * Capitalize first Letter
 */
function properNoun(s){
	return s.charAt(0).toUpperCase() + s.slice(1);
}

//Example of compose functionality
var revCap = compose(reverse, properNoun);
console.log(revCap("samar"));//"ramaS"

Try an example demo jsbin

Category Theory

  • compose
  • identity

Category Laws

  • left identity: compose(id, f) == f
  • right identity: compose(f, id) == f
  • associativity: compose(compose(f, g), h) == compose(f,compose(g,h))

Some conventions

Objects:

var _Container = function(val){
	this.val = val;
}

var Container = function(x){
	return new _Container(x);
}

Container(3);
//=> _Container{val:3}

var _Container.prototype.map = function(f){
	return Container(f(this.val));
}

Container("ram").map(capitalize);
//=> Container("Ram");

Functor

An object or data structure you can map over.

functions: map

  1. maybe

Monards

What is Higher-Order Functions?

Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions. ...more

Benifits of Functional programming:

  • Nulls, Callbacks, Errors, Side effects

functional-programming's People

Contributors

bitdeli-chef avatar samarpanda avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.