mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-12 13:36:40 +00:00
Merge pull request #154 from jethrolarson/sumType
Rename union type to sum type to complement with product type and cha…
This commit is contained in:
commit
96d3ab1451
1 changed files with 33 additions and 31 deletions
52
readme.md
52
readme.md
|
|
@ -35,8 +35,6 @@ __Table of Contents__
|
||||||
* [Value](#value)
|
* [Value](#value)
|
||||||
* [Constant](#constant)
|
* [Constant](#constant)
|
||||||
* [Functor](#functor)
|
* [Functor](#functor)
|
||||||
* [Preserves identity](#preserves-identity)
|
|
||||||
* [Composable](#composable)
|
|
||||||
* [Pointed Functor](#pointed-functor)
|
* [Pointed Functor](#pointed-functor)
|
||||||
* [Lift](#lift)
|
* [Lift](#lift)
|
||||||
* [Referential Transparency](#referential-transparency)
|
* [Referential Transparency](#referential-transparency)
|
||||||
|
|
@ -56,7 +54,8 @@ __Table of Contents__
|
||||||
* [Foldable](#foldable)
|
* [Foldable](#foldable)
|
||||||
* [Traversable](#traversable)
|
* [Traversable](#traversable)
|
||||||
* [Type Signatures](#type-signatures)
|
* [Type Signatures](#type-signatures)
|
||||||
* [Union type](#union-type)
|
* [Algebraic data type](#algebraic-data-type)
|
||||||
|
* [Sum type](#sum-type)
|
||||||
* [Product type](#product-type)
|
* [Product type](#product-type)
|
||||||
* [Option](#option)
|
* [Option](#option)
|
||||||
* [Functional Programming Libraries in JavaScript](#functional-programming-libraries-in-javascript)
|
* [Functional Programming Libraries in JavaScript](#functional-programming-libraries-in-javascript)
|
||||||
|
|
@ -153,9 +152,9 @@ ie. they allow referencing a scope after the block in which the variables were d
|
||||||
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const addTo = x => y => x + y;
|
const addTo = x => y => x + y
|
||||||
var addToFive = addTo(5);
|
var addToFive = addTo(5)
|
||||||
addToFive(3); //returns 8
|
addToFive(3) // returns 8
|
||||||
```
|
```
|
||||||
The function ```addTo()``` returns a function(internally called ```add()```), lets store it in a variable called ```addToFive``` with a curried call having parameter 5.
|
The function ```addTo()``` returns a function(internally called ```add()```), lets store it in a variable called ```addToFive``` with a curried call having parameter 5.
|
||||||
|
|
||||||
|
|
@ -172,12 +171,12 @@ A closure is a function that encloses its surrounding state by referencing field
|
||||||
|
|
||||||
__Further reading/Sources__
|
__Further reading/Sources__
|
||||||
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
|
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
|
||||||
* [JavaScript Closures highly voted disucussion](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)
|
* [How do JavaScript Closures Work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)
|
||||||
|
|
||||||
## Auto Currying
|
## Auto Currying
|
||||||
Transforming a function that takes multiple arguments into one that if given less than its correct number of arguments returns a function that takes the rest. When the function gets the correct number of arguments it is then evaluated.
|
Transforming a function that takes multiple arguments into one that if given less than its correct number of arguments returns a function that takes the rest. When the function gets the correct number of arguments it is then evaluated.
|
||||||
|
|
||||||
lodash & ramda have a `curry` function that works this way.
|
lodash & Ramda have a `curry` function that works this way.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const add = (x, y) => x + y
|
const add = (x, y) => x + y
|
||||||
|
|
@ -368,7 +367,8 @@ To be a valid category 3 rules must be met:
|
||||||
|
|
||||||
Since these rules govern composition at very abstract level, category theory is great at uncovering new ways of composing things.
|
Since these rules govern composition at very abstract level, category theory is great at uncovering new ways of composing things.
|
||||||
|
|
||||||
### Further reading
|
__Further reading__
|
||||||
|
|
||||||
* [Category Theory for Programmers](https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/)
|
* [Category Theory for Programmers](https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/)
|
||||||
|
|
||||||
## Value
|
## Value
|
||||||
|
|
@ -404,12 +404,12 @@ john.age + five === ({name: 'John', age: 30}).age + (5)
|
||||||
|
|
||||||
An object that implements a `map` function which, while running over each value in the object to produce a new object, adheres to two rules:
|
An object that implements a `map` function which, while running over each value in the object to produce a new object, adheres to two rules:
|
||||||
|
|
||||||
### Preserves identity
|
__Preserves identity__
|
||||||
```
|
```
|
||||||
object.map(x => x) ≍ object
|
object.map(x => x) ≍ object
|
||||||
```
|
```
|
||||||
|
|
||||||
### Composable
|
__Composable__
|
||||||
|
|
||||||
```
|
```
|
||||||
object.map(compose(f, g)) ≍ object.map(g).map(f)
|
object.map(compose(f, g)) ≍ object.map(g).map(f)
|
||||||
|
|
@ -773,27 +773,29 @@ __Further reading__
|
||||||
* [Mostly Adequate Guide](https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch7.html#whats-your-type)
|
* [Mostly Adequate Guide](https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch7.html#whats-your-type)
|
||||||
* [What is Hindley-Milner?](http://stackoverflow.com/a/399392/22425) on Stack Overflow
|
* [What is Hindley-Milner?](http://stackoverflow.com/a/399392/22425) on Stack Overflow
|
||||||
|
|
||||||
## Union type
|
## Algebraic data type
|
||||||
A union type is the combination of two types together into another one.
|
A composite type made from putting other types together. Two common classes of algebraic types are [sum](#sum-type) and [product](#product-type).
|
||||||
|
|
||||||
JS doesn't have static types but let's say we invent a type `NumOrString` which is a sum of `String` and `Number`.
|
### Sum type
|
||||||
|
A Sum type is the combination of two types together into another one. It is called sum because the number of possible values in the result type is the sum of the input types.
|
||||||
The `+` operator in JS works on strings and numbers so we can use this new type to describe its inputs and outputs:
|
|
||||||
|
|
||||||
|
JavaScript doesn't have types like this but we can use `Set`s to pretend:
|
||||||
```js
|
```js
|
||||||
// add :: (NumOrString, NumOrString) -> NumOrString
|
// imagine that rather than sets here we have types that can only have these values
|
||||||
const add = (a, b) => a + b
|
const bools = new Set([true, false])
|
||||||
|
const halfTrue = new Set(['half-true'])
|
||||||
|
|
||||||
add(1, 2) // Returns number 3
|
// The weakLogic type contains the sum of the values from bools and halfTrue
|
||||||
add('Foo', 2) // Returns string "Foo2"
|
const weakLogicValues = new Set([...bools, ...halfTrue])
|
||||||
add('Foo', 'Bar') // Returns string "FooBar"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Union types are also known as algebraic types, tagged unions, or sum types.
|
Sum types are sometimes called union types, discriminated unions, or tagged unions.
|
||||||
|
|
||||||
There's a [couple](https://github.com/paldepind/union-type) [libraries](https://github.com/puffnfresh/daggy) in JS which help with defining and using union types.
|
There's a [couple](https://github.com/paldepind/union-type) [libraries](https://github.com/puffnfresh/daggy) in JS which help with defining and using union types.
|
||||||
|
|
||||||
## Product type
|
Flow includes [union types](https://flow.org/en/docs/types/unions/) and TypeScript has [Enums](https://www.typescriptlang.org/docs/handbook/enums.html) to serve the same role.
|
||||||
|
|
||||||
|
### Product type
|
||||||
|
|
||||||
A **product** type combines types together in a way you're probably more familiar with:
|
A **product** type combines types together in a way you're probably more familiar with:
|
||||||
|
|
||||||
|
|
@ -801,12 +803,12 @@ A **product** type combines types together in a way you're probably more familia
|
||||||
// point :: (Number, Number) -> {x: Number, y: Number}
|
// point :: (Number, Number) -> {x: Number, y: Number}
|
||||||
const point = (x, y) => ({x: x, y: y})
|
const point = (x, y) => ({x: x, y: y})
|
||||||
```
|
```
|
||||||
It's called a product because the total possible values of the data structure is the product of the different values.
|
It's called a product because the total possible values of the data structure is the product of the different values. Many languages have a tuple type which is the simplest formulation of a product type.
|
||||||
|
|
||||||
See also [Set theory](https://en.wikipedia.org/wiki/Set_theory).
|
See also [Set theory](https://en.wikipedia.org/wiki/Set_theory).
|
||||||
|
|
||||||
## Option
|
## Option
|
||||||
Option is a [union type](#union-type) with two cases often called `Some` and `None`.
|
Option is a [sum type](#sum-type) with two cases often called `Some` and `None`.
|
||||||
|
|
||||||
Option is useful for composing functions that might not return a value.
|
Option is useful for composing functions that might not return a value.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue