From be13a8c11d861522f15c8b9d7cdd84cf3264d90d Mon Sep 17 00:00:00 2001 From: Todd Motto Date: Fri, 27 May 2016 17:46:36 +0100 Subject: [PATCH 1/8] fix(readme): Typo on "evluated" --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 8accb4b..7170729 100644 --- a/readme.md +++ b/readme.md @@ -303,7 +303,7 @@ let rand = function*() { ``` ```js let randIter = rand(); -randIter.next(); // Each exectuion gives a random value, expression is evluated on need. +randIter.next(); // Each exectuion gives a random value, expression is evaluated on need. ``` --- From e85202403a22ba8f1664bcde9cff5f0a27ec8346 Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Sun, 29 May 2016 12:54:46 +0400 Subject: [PATCH 2/8] Point-Free Style: fix js error according to the way it's used, add should by curried --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 7170729..d29ca38 100644 --- a/readme.md +++ b/readme.md @@ -141,7 +141,7 @@ executions with the the same input parameters. ```js // Given let map = fn => list => list.map(fn); -let add = (a, b) => a + b; +let add = a => b => a + b; // Then From 6c314f238e33258613ce3e6a6ed04d373692c14d Mon Sep 17 00:00:00 2001 From: Marius Schulz Date: Sun, 29 May 2016 13:11:01 +0200 Subject: [PATCH 3/8] Curries `add` function in point-free style code The `add` function needs to be curried in order for the code example to work. --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 7170729..d29ca38 100644 --- a/readme.md +++ b/readme.md @@ -141,7 +141,7 @@ executions with the the same input parameters. ```js // Given let map = fn => list => list.map(fn); -let add = (a, b) => a + b; +let add = a => b => a + b; // Then From 973027d6798a66bd9aa108634a97394906d813d3 Mon Sep 17 00:00:00 2001 From: Kosmas Chatzimichalis Date: Tue, 31 May 2016 20:38:02 +0200 Subject: [PATCH 4/8] small typo either this repository or this repo --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index d29ca38..3bfea63 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # Functional Programming Jargon -> The whole idea of this repos is to try and define jargon from combinatorics and category theory jargon that are used in functional programming in a easier fashion. +> The whole idea of this repo is to try and define jargon from combinatorics and category theory jargon that are used in functional programming in a easier fashion. *Let's try and define these with examples, this is a WIP—please feel free to send PR ;)* From 6ddf6bb3e1e3f19b99fb4f6c3d0baa86b4e44580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 31 May 2016 21:06:30 +0200 Subject: [PATCH 5/8] In-depth proofreading and correcting --- readme.md | 127 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 50 deletions(-) diff --git a/readme.md b/readme.md index d29ca38..86ed04d 100644 --- a/readme.md +++ b/readme.md @@ -13,10 +13,11 @@ const sum = (a, b) => a + b; const arity = sum.length; -console.log(arity); -// => 2 +console.log(arity); // 2 + // The arity of sum is 2 ``` + --- ## Higher-Order Functions (HOF) @@ -25,13 +26,13 @@ console.log(arity); ```js const filter = (pred, xs) => { - const result = []; - for (var idx = 0; idx < xs.length; idx += 1) { - if (pred(xs[idx])) { - result.push(xs[idx]); + const result = []; + for (var idx = 0; idx < xs.length; idx++) { + if (pred(xs[idx])) { + result.push(xs[idx]); + } } - } - return result; + return result; }; ``` @@ -40,7 +41,7 @@ const is = type => x => Object(x) instanceof type; ``` ```js -filter(is(Number), [0, '1', 2, null]); //=> [0, 2] +filter(is(Number), [0, '1', 2, null]); // [0, 2] ``` ## Partial Application @@ -55,7 +56,7 @@ let sum = (a, b) => a + b; let partial = sum.bind(null, 40); // Invoking it with `b` -partial(2); //=> 42 +partial(2); // 42 ``` --- @@ -71,6 +72,7 @@ let curriedSum = (a) => (b) => a + b; curriedSum(40)(2) // 42. ``` + --- ## Composition @@ -78,13 +80,12 @@ curriedSum(40)(2) // 42. > A function which combines two values of a given type (usually also some kind of functions) into a third value of the same type. The most straightforward type of composition is called "normal function composition". -It allows you to combines functions that accept and return a single value. +It allows you to combine functions that accept and return a single value. ```js const compose = (f, g) => a => f(g(a)) // Definition -const floorAndToString = compose((val)=> val.toString(), Math.floor) //Usage +const floorAndToString = compose((val) => val.toString(), Math.floor) // Usage floorAndToString(121.212121) // "121" - ``` --- @@ -97,9 +98,9 @@ input values, without any side effects. ```js let greet = "yo"; -greet.toUpperCase(); // YO; +greet.toUpperCase(); // "YO" -greet // yo; +greet // "yo" ``` As opposed to: @@ -121,16 +122,21 @@ numbers // [] ```js console.log("IO is a side effect!"); ``` + --- ## Idempotency > A function is said to be idempotent if it has no side-effects on multiple -executions with the the same input parameters. +executions with the same input parameters. -`f(f(x)) = f(x)` +```js +f(f(x)) = f(x) +``` -`Math.abs(Math.abs(10))` +```js +Math.abs(Math.abs(10)) +``` --- @@ -168,11 +174,11 @@ Points-free function definitions look just like normal assignments without `func ## Categories -> Objects with associated functions that adhere certain rules. E.g. [monoid](#monoid) +> Objects with associated functions that adhere to certain rules. E.g. [Monoid](#monoid) --- -## Value +## Value > Any complex or primitive value that is used in the computation, including functions. Values in functional programming are assumed to be immutable. @@ -181,36 +187,40 @@ Points-free function definitions look just like normal assignments without `func Object.freeze({name: 'John', age: 30}) // The `freeze` function enforces immutability. (a) => a ``` + Note that value-containing structures such as [Functor](#functor), [Monad](#monad) etc. are themselves values. This means, among other things, that they can be nested within each other. --- -## Constant +## Constant > An immutable reference to a value. Not to be confused with `Variable` - a reference to a value which can at any point be updated to point to a different value. + ```js const five = 5 const john = {name: 'John', age: 30} ``` + Constants are referentially transparent. That is, they can be replaced with the values that they represent without affecting the result. + In other words with the above two constants the expression: ```js john.age + five === ({name: 'John', age: 30}).age + (5) - ``` + Should always return `true`. --- ## Functor -> An object with a `map` function that adhere to certains rules. `Map` runs a function on values in an object and returns a new object. +> An object with a `map` function that adheres to certains rules. `map` runs a function on values in an object and returns a new object. -Simplest functor in javascript is an `Array` +Simplest functor in javascript is an `Array`: ```js -[2,3,4].map( n => n * 2 ); // [4,6,8] +[2, 3, 4].map(n => n * 2); // [4, 6, 8] ``` Let `func` be an object implementing a `map` function, and `f`, `g` be arbitrary functions, then `func` is said to be a functor if the map function adheres to the following rules: @@ -226,11 +236,13 @@ func.map(x => f(g(x))) == func.map(g).map(f) ``` We can now see that `Array` is a functor because it adheres to the functor rules! + ```js [1, 2, 3].map(x => x); // = [1, 2, 3] ``` and + ```js let f = x => x + 1; let g = x => x * 2; @@ -238,16 +250,18 @@ let g = x => x * 2; [1, 2, 3].map(x => f(g(x))); // = [3, 5, 7] [1, 2, 3].map(g).map(f); // = [3, 5, 7] ``` + --- ## Pointed Functor -> A functor with an `of` method. `Of` puts _any_ single value into a functor. +> A functor with an `of` method. `of` puts _any_ single value into a functor. + +Array implementation: -Array Implementation: ```js - Array.prototype.of = (v) => [v]; - - [].of(1) // [1] +Array.prototype.of = (v) => [v]; + +[].of(1) // [1] ``` --- @@ -259,11 +273,13 @@ Array Implementation: Map is the same as a lift over a one-argument function: ```js -lift(n => n * 2)([2,3,4]); // [4,6,8] +lift(n => n * 2)([2, 3, 4]); // [4, 6, 8] ``` + Unlike map lift can be used to combine values from multiple arrays: -``` -lift((a, b) => a * b)([1, 2], [3]); // [3, 6] + +```js +lift((a, b) => a * b)([1, 2], [3]); // [3, 6] ``` --- @@ -296,15 +312,17 @@ referentially transparent. ```js let rand = function*() { - while(1<2) { + while (1 < 2) { yield Math.random(); } } ``` + ```js let randIter = rand(); -randIter.next(); // Each exectuion gives a random value, expression is evaluated on need. +randIter.next(); // Each execution gives a random value, expression is evaluated on need. ``` + --- ## Monoid @@ -342,23 +360,23 @@ The identity value is empty array `[]` ```js [1, 2].concat([]); // [1, 2] ``` -Functions also form a monoid with the normal functional compositon as an operation and the function which returns its input `(a) => a` +Functions also form a monoid with the normal functional composition as an operation and the function which returns its input `(a) => a` --- ## Monad -> A monad is an object with [`of`](#pointed-functor) and `chain` functions. `Chain` is like [map](#functor) except it unnests the resulting nested object. +> A monad is an object with [`of`](#pointed-functor) and `chain` functions. `chain` is like [`map`](#functor) except it un-nests the resulting nested object. ```js -['cat,dog','fish,bird'].chain(a => a.split(',')) // ['cat','dog','fish','bird'] +['cat,dog', 'fish,bird'].chain(a => a.split(',')) // ['cat', 'dog', 'fish', 'bird'] //Contrast to map -['cat,dog','fish,bird'].map(a => a.split(',')) // [['cat','dog'], ['fish','bird']] +['cat,dog', 'fish,bird'].map(a => a.split(',')) // [['cat', 'dog'], ['fish', 'bird']] ``` -You may also see `of` and `chain` referred to as `return` and `bind` (not be confused with the JS keyword/function...) in languages which provide Monad-like constructs as part of their standard library (e.g. Haskell, F#), on [Wikipedia](https://en.wikipedia.org/wiki/Monad_%28functional_programming%29) and in other literature. It's also important to note that `return` and `bind` are not part of the [Fantasy Land spec](https://github.com/fantasyland/fantasy-land) and are mentioned here only for the sake of people interested in learning more about Monads. +You may also see `of` and `chain` referred to as `return` and `bind` (not to be confused with the JS keyword/function...) in languages which provide monad-like constructs as part of their standard library (e.g. Haskell, F#), on [Wikipedia](https://en.wikipedia.org/wiki/Monad_%28functional_programming%29) and in other literature. It's also important to note that `return` and `bind` are not part of the [Fantasy Land spec](https://github.com/fantasyland/fantasy-land) and are mentioned here only for the sake of people interested in learning more about monads. --- @@ -375,22 +393,25 @@ let CoIdentity = v => ({ ``` Extract takes a value out of a functor. + ```js CoIdentity(1).extract() // 1 ``` -Extend runs a function on the comonad. The function should return the same type as the Comonad. +Extend runs a function on the comonad. The function should return the same type as the comonad. + ```js CoIdentity(1).extend(co => co.extract() + 1) // CoIdentity(2) ``` + --- ## Applicative Functor -> An applicative functor is an object with an `ap` function. `Ap` applies a function in the object to a value in another object of the same type. +> An applicative functor is an object with an `ap` function. `ap` applies a function in the object to a value in another object of the same type. ```js -[(a)=> a + 1].ap([1]) // [2] +[(a) => a + 1].ap([1]) // [2] ``` --- @@ -403,9 +424,10 @@ CoIdentity(1).extend(co => co.extract() + 1) // CoIdentity(2) ## Isomorphism -> A pair of transformations between 2 types of objects that is structural in nature and no data is lost. +> A pair of transformations between 2 types of objects that is structural in nature and no data is lost. For example, 2D coordinates could be stored as an array `[2,3]` or object `{x: 2, y: 3}`. + ```js // Providing functions to convert in both directions makes them isomorphic. const pairToCoords = (pair) => ({x: pair[0], y: pair[1]}) @@ -423,7 +445,8 @@ pairToCoords(coordsToPair({x: 1, y: 2})) // {x: 1, y: 2} > An object that has an `equals` function which can be used to compare other objects of the same type. -Make array a setoid. +Make array a setoid: + ```js Array.prototype.equals = arr => { var len = this.length @@ -446,7 +469,7 @@ Array.prototype.equals = arr => { ## Semigroup -An object that has a `concat` function that combines it with another object of the same type. +> An object that has a `concat` function that combines it with another object of the same type. ```js [1].concat([2]) // [1, 2] @@ -456,7 +479,7 @@ An object that has a `concat` function that combines it with another object of t ## Foldable -> An object that has a reduce function that can transform that object into some other type. +> An object that has a `reduce` function that can transform that object into some other type. ```js let sum = list => list.reduce((acc, val) => acc + val, 0); @@ -468,11 +491,13 @@ sum([1, 2, 3]) // 6 ## Traversable --- + ## Type Signatures > Often functions will include comments that indicate the types of their arguments and return types. -There's quite a bit variance across the community but they often follow the following patterns: +There's quite a bit of variance across the community but they often follow the following patterns: + ```js // functionName :: firstArgType -> secondArgType -> returnType @@ -489,8 +514,10 @@ If a function accepts another function as an argument it is wrapped in parenthes // call :: (a -> b) -> a -> b let call = f => x => f(x) ``` -The letters `a`, `b`, `c`, `d` are used to signify that the argument can be of any type. For this map it takes a function that transforms a value of some type `a` into another type `b`, an array of values of type `a`, and returns an array of values of type `b`. + +The letters `a`, `b`, `c`, `d` are used to signify that the argument can be of any type. For this `map` it takes a function that transforms a value of some type `a` into another type `b`, an array of values of type `a`, and returns an array of values of type `b`. + ```js // map :: (a -> b) -> [a] -> [b] -let map = f => list => list.map(f) +let map = f => list => list.map(f) ``` From 181c17fa5c863c09fcd20a3f8ebd00b715b9d95f Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Wed, 1 Jun 2016 05:45:11 +0530 Subject: [PATCH 6/8] =?UTF-8?q?Added=20a=20link=20to=20all=20the=20contrib?= =?UTF-8?q?utors=20=F0=9F=99=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks to: @jethrolarson @ethagnawl @therealklanni @CrossEye @jhartikainen @davidchambers @skippednote @mariusschulz @lovasoa @raine @sjsyrek @toddmotto @matthieubulte @stoeffel --- readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.md b/readme.md index 3bfea63..d690146 100644 --- a/readme.md +++ b/readme.md @@ -494,3 +494,6 @@ The letters `a`, `b`, `c`, `d` are used to signify that the argument can be of a // map :: (a -> b) -> [a] -> [b] let map = f => list => list.map(f) ``` +--- + +__P.S:__ Without the wonderful [contributions](https://github.com/hemanth/functional-programming-jargon/graphs/contributors) this repo would be meaningless! From 9ac6df6cba7004500d26af7bde414db3e2fa88a5 Mon Sep 17 00:00:00 2001 From: Rootul Patel Date: Tue, 31 May 2016 22:55:19 -0400 Subject: [PATCH 7/8] Fix typo: combines to combine --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index d690146..de7dee9 100644 --- a/readme.md +++ b/readme.md @@ -78,7 +78,7 @@ curriedSum(40)(2) // 42. > A function which combines two values of a given type (usually also some kind of functions) into a third value of the same type. The most straightforward type of composition is called "normal function composition". -It allows you to combines functions that accept and return a single value. +It allows you to combine functions that accept and return a single value. ```js const compose = (f, g) => a => f(g(a)) // Definition From 8b75429462d0efa0ea4dafe2f775faf0828cc49c Mon Sep 17 00:00:00 2001 From: Stoeffel Date: Wed, 1 Jun 2016 11:35:39 +0200 Subject: [PATCH 8/8] adds toc * create toc `npm run toc` --- package.json | 23 +++++++++++++++++++++++ readme.md | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..e77e457 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "functional-programming-jargons", + "version": "1.0.0", + "description": "Jargon from the functional programming world in simple terms!", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "toc": "roadmarks" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/hemanth/functional-programming-jargon.git" + }, + "author": "hemanth", + "license": "MIT", + "bugs": { + "url": "https://github.com/hemanth/functional-programming-jargon/issues" + }, + "homepage": "https://github.com/hemanth/functional-programming-jargon#readme", + "devDependencies": { + "roadmarks": "^1.6.3" + } +} diff --git a/readme.md b/readme.md index 4d86f38..e01de2a 100644 --- a/readme.md +++ b/readme.md @@ -4,6 +4,42 @@ *Let's try and define these with examples, this is a WIP—please feel free to send PR ;)* + + +* [Arity](#arity) +* [Higher-Order Functions (HOF)](#higher-order-functions-hof) +* [Partial Application](#partial-application) +* [Currying](#currying) +* [Composition](#composition) +* [Purity](#purity) +* [Side effects](#side-effects) +* [Idempotency](#idempotency) +* [Point-Free Style](#point-free-style) +* [Contracts](#contracts) +* [Guarded Functions](#guarded-functions) +* [Categories](#categories) +* [Value](#value) +* [Constant](#constant) +* [Functor](#functor) +* [Pointed Functor](#pointed-functor) +* [Lift](#lift) +* [Referential Transparency](#referential-transparency) +* [Equational Reasoning](#equational-reasoning) +* [Lazy evaluation](#lazy-evaluation) +* [Monoid](#monoid) +* [Monad](#monad) +* [Comonad](#comonad) +* [Applicative Functor](#applicative-functor) +* [Morphism](#morphism) +* [Isomorphism](#isomorphism) +* [Setoid](#setoid) +* [Semigroup](#semigroup) +* [Foldable](#foldable) +* [Traversable](#traversable) +* [Type Signatures](#type-signatures) + + + ## Arity @@ -523,4 +559,4 @@ let map = f => list => list.map(f) ``` --- -__P.S:__ Without the wonderful [contributions](https://github.com/hemanth/functional-programming-jargon/graphs/contributors) this repo would be meaningless! +__P.S:__ Without the wonderful [contributions](https://github.com/hemanth/functional-programming-jargon/graphs/contributors) this repo would be meaningless!