From 5a70b12cf565ad3a1a43066199ba7f48cab25d0c Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 31 May 2019 16:59:53 +0200 Subject: [PATCH] add of functions when missing --- docs/modules/Array.ts.md | 11 +++++++++++ docs/modules/IO.ts.md | 11 +++++++++++ docs/modules/NonEmptyArray.ts.md | 11 +++++++++++ docs/modules/Reader.ts.md | 11 +++++++++++ docs/modules/State.ts.md | 11 +++++++++++ docs/modules/Task.ts.md | 11 +++++++++++ docs/modules/Tree.ts.md | 11 +++++++++++ src/Array.ts | 7 ++++++- src/IO.ts | 7 ++++++- src/NonEmptyArray.ts | 7 ++++++- src/Reader.ts | 7 ++++++- src/State.ts | 7 ++++++- src/Task.ts | 9 ++++++++- src/Tree.ts | 15 +++++++++++---- 14 files changed, 126 insertions(+), 10 deletions(-) diff --git a/docs/modules/Array.ts.md b/docs/modules/Array.ts.md index f97e93ace..3799b91f2 100644 --- a/docs/modules/Array.ts.md +++ b/docs/modules/Array.ts.md @@ -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) @@ -911,6 +912,16 @@ assert.deepStrictEqual(modifyAt(1, double)([]), none) Added in v2.0.0 +# of (function) + +**Signature** + +```ts +export const of = (a: A): Array => ... +``` + +Added in v2.0.0 + # range (function) Create an array containing a range of integers, including both endpoints diff --git a/docs/modules/IO.ts.md b/docs/modules/IO.ts.md index 1374d37f6..a9c577937 100644 --- a/docs/modules/IO.ts.md +++ b/docs/modules/IO.ts.md @@ -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) --- @@ -169,3 +170,13 @@ export function getSemigroup(S: Semigroup): Semigroup> { ... } ``` Added in v2.0.0 + +# of (function) + +**Signature** + +```ts +export const of = (a: A): IO => () => ... +``` + +Added in v2.0.0 diff --git a/docs/modules/NonEmptyArray.ts.md b/docs/modules/NonEmptyArray.ts.md index 334a81967..1e4c9fba1 100644 --- a/docs/modules/NonEmptyArray.ts.md +++ b/docs/modules/NonEmptyArray.ts.md @@ -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) @@ -148,6 +149,16 @@ export const nonEmptyArray: Monad1 & Added in v2.0.0 +# of (constant) + +**Signature** + +```ts +export const of: (a: A) => NonEmptyArray = ... +``` + +Added in v2.0.0 + # reverse (constant) **Signature** diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index 28334f9f5..2dfae6441 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -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) @@ -76,6 +77,16 @@ export const asks: (f: (r: R) => A) => Reader = ... Added in v2.0.0 +# of (constant) + +**Signature** + +```ts +export const of: (a: A) => Reader = ... +``` + +Added in v2.0.0 + # reader (constant) **Signature** diff --git a/docs/modules/State.ts.md b/docs/modules/State.ts.md index ae07ad3ff..7237c39de 100644 --- a/docs/modules/State.ts.md +++ b/docs/modules/State.ts.md @@ -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) @@ -113,6 +114,16 @@ export const modify: (f: (s: S) => S) => State = ... Added in v2.0.0 +# of (constant) + +**Signature** + +```ts +export const of: (a: A) => State = ... +``` + +Added in v2.0.0 + # put (constant) Set the state diff --git a/docs/modules/Task.ts.md b/docs/modules/Task.ts.md index 31a054a42..812f4069a 100644 --- a/docs/modules/Task.ts.md +++ b/docs/modules/Task.ts.md @@ -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) --- @@ -140,3 +141,13 @@ export const never: Task = () => new Promise(_ => ... ``` Added in v2.0.0 + +# of (function) + +**Signature** + +```ts +export function of(a: A): Task { ... } +``` + +Added in v2.0.0 diff --git a/docs/modules/Tree.ts.md b/docs/modules/Tree.ts.md index a6157a637..0ac1f6614 100644 --- a/docs/modules/Tree.ts.md +++ b/docs/modules/Tree.ts.md @@ -27,6 +27,7 @@ type Forest = Array> - [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) @@ -169,6 +170,16 @@ export function make(value: A, forest: Forest = empty): Tree { ... } Added in v2.0.0 +# of (function) + +**Signature** + +```ts +export function of(a: A): Tree { ... } +``` + +Added in v2.0.0 + # unfoldForest (function) Build a tree from a seed value diff --git a/src/Array.ts b/src/Array.ts index 5110ec473..257182ace 100644 --- a/src/Array.ts +++ b/src/Array.ts @@ -1270,6 +1270,11 @@ export function difference(E: Eq): (xs: Array, ys: Array) => Array(a: A): A => a +/** + * @since 2.0.0 + */ +export const of = (a: A): Array => [a] + /** * @since 2.0.0 */ @@ -1312,7 +1317,7 @@ export const array: Monad1 & 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 diff --git a/src/IO.ts b/src/IO.ts index e26b8e80b..6a17ef218 100644 --- a/src/IO.ts +++ b/src/IO.ts @@ -139,13 +139,18 @@ export function getMonoid(M: Monoid): Monoid> { } } +/** + * @since 2.0.0 + */ +export const of = (a: A): IO => () => a + /** * @since 2.0.0 */ export const io: Monad1 & MonadIO1 = { URI, map: (ma, f) => () => f(ma()), - of: a => () => a, + of, ap: (mab, ma) => () => mab()(ma()), chain: (ma, f) => () => f(ma())(), fromIO: identity diff --git a/src/NonEmptyArray.ts b/src/NonEmptyArray.ts index a09c4968e..1b315f607 100644 --- a/src/NonEmptyArray.ts +++ b/src/NonEmptyArray.ts @@ -280,6 +280,11 @@ export function filterWithIndex( return nea => fromArray(nea.filter((a, i) => predicate(i, a))) } +/** + * @since 2.0.0 + */ +export const of: (a: A) => NonEmptyArray = A.of as any + /** * @since 2.0.0 */ @@ -291,7 +296,7 @@ export const nonEmptyArray: Monad1 & 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, diff --git a/src/Reader.ts b/src/Reader.ts index b64c6ea20..9d198d16a 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -78,13 +78,18 @@ export function getMonoid(M: Monoid): Monoid> { } } +/** + * @since 2.0.0 + */ +export const of: (a: A) => Reader = T.of + /** * @since 2.0.0 */ export const reader: Monad2 & Profunctor2 & Category2 & Strong2 & Choice2 = { 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))), diff --git a/src/State.ts b/src/State.ts index 7b20fdcaf..34f6b97ef 100644 --- a/src/State.ts +++ b/src/State.ts @@ -70,13 +70,18 @@ export const modify: (f: (s: S) => S) => State = T.modify */ export const gets: (f: (s: S) => A) => State = T.gets +/** + * @since 2.0.0 + */ +export const of: (a: A) => State = T.of + /** * @since 2.0.0 */ export const state: Monad2 = { URI, map: T.map, - of: T.of, + of, ap: T.ap, chain: T.chain } diff --git a/src/Task.ts b/src/Task.ts index f3b7f6533..27ba28233 100644 --- a/src/Task.ts +++ b/src/Task.ts @@ -106,13 +106,20 @@ export function fromIO(ma: IO): Task { const identity = (a: A): A => a +/** + * @since 2.0.0 + */ +export function of(a: A): Task { + return () => Promise.resolve(a) +} + /** * @since 2.0.0 */ export const task: Monad1 & MonadIO1 & MonadTask1 = { 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, diff --git a/src/Tree.ts b/src/Tree.ts index cc3803886..7ef0321e3 100644 --- a/src/Tree.ts +++ b/src/Tree.ts @@ -220,6 +220,16 @@ export function elem(E: Eq): (a: A) => (fa: Tree) => boolean { } } +/** + * @since 2.0.0 + */ +export function of(a: A): Tree { + return { + value: a, + forest: empty + } +} + /** * @since 2.0.0 */ @@ -229,10 +239,7 @@ export const tree: Monad1 & Foldable1 & Traversable1 & Comonad1 tree.map(t, f)) }), - of: a => ({ - value: a, - forest: empty - }), + of, ap: (fab, fa) => tree.chain(fab, f => tree.map(fa, f)), // <- derived chain: (fa: Tree, f: (a: A) => Tree): Tree => { const { value, forest } = f(fa.value)