Skip to content

Commit

Permalink
add of functions when missing
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed May 31, 2019
1 parent 533d561 commit 5a70b12
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 10 deletions.
11 changes: 11 additions & 0 deletions docs/modules/Array.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Adapted from https://github.com/purescript/purescript-arrays
- [lookup (function)](#lookup-function)
- [makeBy (function)](#makeby-function)
- [modifyAt (function)](#modifyat-function)
- [of (function)](#of-function)
- [range (function)](#range-function)
- [replicate (function)](#replicate-function)
- [reverse (function)](#reverse-function)
Expand Down Expand Up @@ -911,6 +912,16 @@ assert.deepStrictEqual(modifyAt(1, double)([]), none)

Added in v2.0.0

# of (function)

**Signature**

```ts
export const of = <A>(a: A): Array<A> => ...
```

Added in v2.0.0

# range (function)

Create an array containing a range of integers, including both endpoints
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/IO.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ computation() // returns { name: 'Aristotle', age: 60 }
- [io (constant)](#io-constant)
- [getMonoid (function)](#getmonoid-function)
- [getSemigroup (function)](#getsemigroup-function)
- [of (function)](#of-function)

---

Expand Down Expand Up @@ -169,3 +170,13 @@ export function getSemigroup<A>(S: Semigroup<A>): Semigroup<IO<A>> { ... }
```

Added in v2.0.0

# of (function)

**Signature**

```ts
export const of = <A>(a: A): IO<A> => () => ...
```

Added in v2.0.0
11 changes: 11 additions & 0 deletions docs/modules/NonEmptyArray.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Data structure which represents non-empty arrays
- [getEq (constant)](#geteq-constant)
- [getShow (constant)](#getshow-constant)
- [nonEmptyArray (constant)](#nonemptyarray-constant)
- [of (constant)](#of-constant)
- [reverse (constant)](#reverse-constant)
- [snoc (constant)](#snoc-constant)
- [filter (function)](#filter-function)
Expand Down Expand Up @@ -148,6 +149,16 @@ export const nonEmptyArray: Monad1<URI> &

Added in v2.0.0

# of (constant)

**Signature**

```ts
export const of: <A>(a: A) => NonEmptyArray<A> = ...
```

Added in v2.0.0

# reverse (constant)

**Signature**
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/Reader.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ parent: Modules
- [URI (constant)](#uri-constant)
- [ask (constant)](#ask-constant)
- [asks (constant)](#asks-constant)
- [of (constant)](#of-constant)
- [reader (constant)](#reader-constant)
- [getMonoid (function)](#getmonoid-function)
- [getSemigroup (function)](#getsemigroup-function)
Expand Down Expand Up @@ -76,6 +77,16 @@ export const asks: <R, A>(f: (r: R) => A) => Reader<R, A> = ...

Added in v2.0.0

# of (constant)

**Signature**

```ts
export const of: <A>(a: A) => Reader<unknown, A> = ...
```

Added in v2.0.0

# reader (constant)

**Signature**
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/State.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parent: Modules
- [get (constant)](#get-constant)
- [gets (constant)](#gets-constant)
- [modify (constant)](#modify-constant)
- [of (constant)](#of-constant)
- [put (constant)](#put-constant)
- [state (constant)](#state-constant)

Expand Down Expand Up @@ -113,6 +114,16 @@ export const modify: <S>(f: (s: S) => S) => State<S, void> = ...

Added in v2.0.0

# of (constant)

**Signature**

```ts
export const of: <S, A>(a: A) => State<S, A> = ...
```

Added in v2.0.0

# put (constant)

Set the state
Expand Down
11 changes: 11 additions & 0 deletions docs/modules/Task.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ If you want to represent an asynchronous computation that may fail, please see `
- [getRaceMonoid (function)](#getracemonoid-function)
- [getSemigroup (function)](#getsemigroup-function)
- [never (function)](#never-function)
- [of (function)](#of-function)

---

Expand Down Expand Up @@ -140,3 +141,13 @@ export const never: Task<never> = () => new Promise(_ => ...
```
Added in v2.0.0
# of (function)
**Signature**
```ts
export function of<A>(a: A): Task<A> { ... }
```
Added in v2.0.0
11 changes: 11 additions & 0 deletions docs/modules/Tree.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Forest<A> = Array<Tree<A>>
- [getEq (function)](#geteq-function)
- [getShow (function)](#getshow-function)
- [make (function)](#make-function)
- [of (function)](#of-function)
- [unfoldForest (function)](#unfoldforest-function)
- [unfoldForestM (function)](#unfoldforestm-function)
- [unfoldTree (function)](#unfoldtree-function)
Expand Down Expand Up @@ -169,6 +170,16 @@ export function make<A>(value: A, forest: Forest<A> = empty): Tree<A> { ... }

Added in v2.0.0

# of (function)

**Signature**

```ts
export function of<A>(a: A): Tree<A> { ... }
```

Added in v2.0.0

# unfoldForest (function)

Build a tree from a seed value
Expand Down
7 changes: 6 additions & 1 deletion src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,11 @@ export function difference<A>(E: Eq<A>): (xs: Array<A>, ys: Array<A>) => Array<A

const identity = <A>(a: A): A => a

/**
* @since 2.0.0
*/
export const of = <A>(a: A): Array<A> => [a]

/**
* @since 2.0.0
*/
Expand Down Expand Up @@ -1312,7 +1317,7 @@ export const array: Monad1<URI> &
return array.partitionWithIndex(fa, (_, a) => predicate(a))
},
partitionMap: (fa, f) => array.partitionMapWithIndex(fa, (_, a) => f(a)),
of: a => [a],
of,
ap: (fab, fa) => flatten(array.map(fab, f => array.map(fa, f))),
chain: (fa, f) => {
let resLen = 0
Expand Down
7 changes: 6 additions & 1 deletion src/IO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,18 @@ export function getMonoid<A>(M: Monoid<A>): Monoid<IO<A>> {
}
}

/**
* @since 2.0.0
*/
export const of = <A>(a: A): IO<A> => () => a

/**
* @since 2.0.0
*/
export const io: Monad1<URI> & MonadIO1<URI> = {
URI,
map: (ma, f) => () => f(ma()),
of: a => () => a,
of,
ap: (mab, ma) => () => mab()(ma()),
chain: (ma, f) => () => f(ma())(),
fromIO: identity
Expand Down
7 changes: 6 additions & 1 deletion src/NonEmptyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ export function filterWithIndex<A>(
return nea => fromArray(nea.filter((a, i) => predicate(i, a)))
}

/**
* @since 2.0.0
*/
export const of: <A>(a: A) => NonEmptyArray<A> = A.of as any

/**
* @since 2.0.0
*/
Expand All @@ -291,7 +296,7 @@ export const nonEmptyArray: Monad1<URI> &
URI,
map: A.array.map as any,
mapWithIndex: A.array.mapWithIndex as any,
of: A.array.of as any,
of,
ap: A.array.ap as any,
chain: A.array.chain as any,
extend: A.array.extend as any,
Expand Down
7 changes: 6 additions & 1 deletion src/Reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,18 @@ export function getMonoid<R, A>(M: Monoid<A>): Monoid<Reader<R, A>> {
}
}

/**
* @since 2.0.0
*/
export const of: <A>(a: A) => Reader<unknown, A> = T.of

/**
* @since 2.0.0
*/
export const reader: Monad2<URI> & Profunctor2<URI> & Category2<URI> & Strong2<URI> & Choice2<URI> = {
URI,
map: (ma, f) => e => f(ma(e)),
of: T.of,
of,
ap: T.ap,
chain: T.chain,
promap: (mbc, f, g) => a => g(mbc(f(a))),
Expand Down
7 changes: 6 additions & 1 deletion src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ export const modify: <S>(f: (s: S) => S) => State<S, void> = T.modify
*/
export const gets: <S, A>(f: (s: S) => A) => State<S, A> = T.gets

/**
* @since 2.0.0
*/
export const of: <S, A>(a: A) => State<S, A> = T.of

/**
* @since 2.0.0
*/
export const state: Monad2<URI> = {
URI,
map: T.map,
of: T.of,
of,
ap: T.ap,
chain: T.chain
}
Expand Down
9 changes: 8 additions & 1 deletion src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,20 @@ export function fromIO<A>(ma: IO<A>): Task<A> {

const identity = <A>(a: A): A => a

/**
* @since 2.0.0
*/
export function of<A>(a: A): Task<A> {
return () => Promise.resolve(a)
}

/**
* @since 2.0.0
*/
export const task: Monad1<URI> & MonadIO1<URI> & MonadTask1<URI> = {
URI,
map: (ma, f) => () => ma().then(f),
of: a => () => Promise.resolve(a),
of,
ap: (mab, ma) => () => Promise.all([mab(), ma()]).then(([f, a]) => f(a)),
chain: (ma, f) => () => ma().then(a => f(a)()),
fromIO,
Expand Down
15 changes: 11 additions & 4 deletions src/Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ export function elem<A>(E: Eq<A>): (a: A) => (fa: Tree<A>) => boolean {
}
}

/**
* @since 2.0.0
*/
export function of<A>(a: A): Tree<A> {
return {
value: a,
forest: empty
}
}

/**
* @since 2.0.0
*/
Expand All @@ -229,10 +239,7 @@ export const tree: Monad1<URI> & Foldable1<URI> & Traversable1<URI> & Comonad1<U
value: f(fa.value),
forest: fa.forest.map(t => tree.map(t, f))
}),
of: a => ({
value: a,
forest: empty
}),
of,
ap: (fab, fa) => tree.chain(fab, f => tree.map(fa, f)), // <- derived
chain: <A, B>(fa: Tree<A>, f: (a: A) => Tree<B>): Tree<B> => {
const { value, forest } = f(fa.value)
Expand Down

0 comments on commit 5a70b12

Please sign in to comment.