-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the initial commit for the `Identity` documentation for #42
- Loading branch information
1 parent
df484e2
commit 566c922
Showing
1 changed file
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
--- | ||
title: "Identity" | ||
description: "Identity Crock" | ||
layout: "guide" | ||
weight: 60 | ||
--- | ||
|
||
```haskell | ||
Identity a | ||
``` | ||
|
||
`Identity` is one of the most versatile `monads`. It does not have any inherant | ||
behaviour and simply applies the given `function` to it's value. `Identity` is | ||
often used in place where a `monad` or `applicative` is expected. | ||
Identity is also known the "empty" functor and applicative functor. `Identity` | ||
composed with another functor or applicative functor is isomorphic to the | ||
original. | ||
|
||
```javascript | ||
import Identity from 'crocks/Identity' | ||
|
||
Identity(10) | ||
//=> Identity 10 | ||
``` | ||
<article id="topic-implements"> | ||
|
||
## Implements | ||
`Setoid`, `Semigroup`, `Functor`, `Chain`, `Traversable`, `Apply`, `Applicative`, `Monad` | ||
|
||
</article> | ||
|
||
<article id="topic-construction"> | ||
|
||
## Construction | ||
|
||
```haskell | ||
Identity :: a -> Identity a | ||
``` | ||
|
||
|
||
```javascript | ||
import Identity from 'crocks/Identity' | ||
|
||
Identity(42) | ||
//=> Identity 42 | ||
|
||
``` | ||
|
||
</article> | ||
|
||
<article id="topic-constructor"> | ||
|
||
## Constructor Methods | ||
|
||
#### of | ||
|
||
```haskell | ||
Identity.of :: a -> Identity a | ||
``` | ||
|
||
`of` is used to construct an `Identity` with ... | ||
|
||
```javascript | ||
import Identity from 'crocks/Identity' | ||
|
||
Identity.of(42) | ||
//=> Identity 42 | ||
|
||
``` | ||
|
||
</article> | ||
|
||
<article id="topic-instance"> | ||
|
||
## Instance Methods | ||
|
||
#### equals | ||
|
||
```haskell | ||
Identity a ~> b -> Boolean | ||
``` | ||
|
||
Used to compare the underlying values of two `Identity` instances for equality by | ||
value, `equals` takes any given argument and returns `true` if the passed | ||
arguments is a `Identity` with an underlying value equal to the underlying value | ||
of the `Identity` the method is being called on. If the passed argument is not | ||
an `Identity` or the underlying values are not equal, `equals` will return `false`. | ||
|
||
```javascript | ||
import Identity from 'crocks/Identity' | ||
|
||
import equals from 'crocks/pointfree/equals' | ||
|
||
Identity(33) | ||
.equals(Identity(33)) | ||
//=> true | ||
|
||
Identity(33) | ||
.equals(Identity('33')) | ||
//=> false | ||
|
||
// by value, not reference for most types | ||
Identity({ a: 86, b: true }) | ||
.equals(Identity({ a: 86, b: true })) | ||
//=> true | ||
|
||
equals(Identity(95), 95) | ||
//=> false | ||
|
||
equals(Identity([ 2, 3 ]), Identity([ 2, 3 ])) | ||
//=> true | ||
``` | ||
|
||
#### concat | ||
|
||
```haskell | ||
Identity s => Identity s ~> Identity s -> Identity s | ||
``` | ||
|
||
When an underlying value of a given `Identity` is fixed to a `Semigroup`, | ||
`concat` can be used to concat another `Identity` instance with an underlying | ||
`Semigroup` of the same type. Expecting a `Identity` wrapping a `Semigroup` of | ||
the same type, `concat` will give back a new `Identity` instance wrapping the | ||
result of combining the two underlying `Semigroup`s. | ||
|
||
```javascript | ||
import Identity from 'crocks/Identity' | ||
|
||
import Sum from 'crocks/Sum' | ||
|
||
import compose from 'crocks/helpers/compose' | ||
import concat from 'crocks/pointfree/concat' | ||
import flip from 'crocks/combinators/flip' | ||
import map from 'crocks/pointfree/map' | ||
import mapReduce from 'crocks/helpers/mapReduce' | ||
import valueOf from 'crocks/pointfree/valueOf' | ||
|
||
// empty :: Identity Sum | ||
const empty = | ||
Identity(Sum.empty()) | ||
|
||
// sumList :: [ * ] -> Identity Number | ||
const sumList = compose( | ||
map(valueOf), | ||
mapReduce(compose(Identity, Sum), flip(concat), empty) | ||
) | ||
|
||
Identity([ 34 ]) | ||
.concat(Identity([ 92 ])) | ||
//=> Identity [ 34, 92 ] | ||
|
||
sumList([ 3, 4, 5 ]) | ||
//=> Identity 12 | ||
|
||
``` | ||
|
||
#### map | ||
|
||
```haskell | ||
Identity a ~> (a -> b) -> Identity b | ||
``` | ||
|
||
|
||
```javascript | ||
import Identity from 'crocks/Identity' | ||
|
||
const prod = a => b => a * b | ||
const double = prod(2) | ||
|
||
Identity(5) | ||
.map(double) | ||
//=> Identity 10 | ||
``` | ||
|
||
#### ap | ||
|
||
```haskell | ||
Identity (a -> b) ~> Identity a -> Identity b | ||
``` | ||
|
||
`ap` allows for values wrapped in a `Identity` to be applied to functions also | ||
wrapped in a `Identity`. In order to use `ap`, the `Identity` must contain a | ||
function as its value. Under the hood, `ap` unwraps both the function | ||
and the value to be applied and applies the value to the function. Finally it | ||
will wrap the result of that application back into a `Identity`. It is required | ||
that the inner function is curried. | ||
|
||
```javascript | ||
import Identity from 'crocks/Identity' | ||
|
||
const prod = a => b => a * b | ||
const double = prod(2) | ||
|
||
Identity(double) | ||
.ap(5) | ||
//=> Identity 10 | ||
|
||
``` | ||
|
||
#### sequence | ||
|
||
#### traverse | ||
|
||
#### chain | ||
|
||
```haskell | ||
Identity a ~> (a -> Identity b) -> Identity b | ||
``` | ||
|
||
Normally one of the ways `Monad`s like `Identity`... | ||
|
||
```javascript | ||
import Identity from 'crocks/Identity' | ||
import compose from 'crocks/helpers/compose' | ||
|
||
const prod = a => b => a * b | ||
const doubleAsIdentity = compose(Identity, prod(2)) | ||
|
||
Identity(5) | ||
.chain(doubleAsIdentity) | ||
//=> Identity 10 | ||
|
||
``` | ||
|
||
|
||
</article> |