Add monocle

This commit is contained in:
Michael Arnaldi
2020-10-05 22:01:01 +01:00
parent c2826b11f1
commit fc3342e0ed
3 changed files with 197 additions and 0 deletions
+1
View File
@@ -47,6 +47,7 @@
"Classic/EitherT",
"Classic/Equal",
"Classic/Has",
"Classic/Id",
"Classic/Identity",
"Classic/IndexedT",
"Classic/InvariantT",
+191
View File
@@ -0,0 +1,191 @@
/* adapted from https://github.com/gcanti/fp-ts */
import { identity as id } from "../../Function"
import type { IdURI } from "../../Modules"
import * as P from "../../Prelude"
import type { Equal } from "../Equal"
import type { Identity } from "../Identity"
import type { Show } from "../Show"
export type Id<A> = A
export const alt_: <A>(fx: A, fy: () => A) => A = id
export const alt: <A>(that: () => A) => (fa: A) => A = (that) => (fa) => alt_(fa, that)
export const ap_: <A, B>(fab: (a: A) => B, fa: A) => B = (mab, ma) => mab(ma)
export const ap: <A>(fa: A) => <B>(fab: (a: A) => B) => B = (fa) => (fab) =>
ap_(fab, fa)
export const apFirst: <B>(fb: B) => <A>(fa: A) => A = (_) => (fa) => fa
export const apSecond: <B>(fb: B) => <A>(fa: A) => B = (fb) => (_) => fb
export const chain_: <A, B>(fa: A, f: (a: A) => B) => B = (ma, f) => f(ma)
export const chain: <A, B>(f: (a: A) => B) => (ma: A) => B = (f) => (ma) => f(ma)
export const chainTap: <A, B>(f: (a: A) => B) => (ma: A) => A = (f) => (ma) =>
chain_(ma, (x) => map_(f(x), () => x))
export const chainTap_: <A, B>(ma: A, f: (a: A) => B) => A = (ma, f) =>
chain_(ma, (x) => map_(f(x), () => x))
export const duplicate: <A>(ma: A) => A = (ma) => ma
export const extend_: <A, B>(wa: A, f: (wa: A) => B) => B = (wa, f) => f(wa)
export const extend: <A, B>(f: (fa: A) => B) => (ma: A) => B = (f) => (ma) => f(ma)
export const extract: <A>(wa: A) => A = id
export const flatten: <A>(mma: A) => A = (mma) => mma
export const foldMap_: <M>(M: Identity<M>) => <A>(fa: A, f: (a: A) => M) => M = (_) => (
fa,
f
) => f(fa)
export const foldMap: <M>(M: Identity<M>) => <A>(f: (a: A) => M) => (fa: A) => M = (
M
) => (f) => (fa) => foldMap_(M)(fa, f)
export const getEq: <A>(E: Equal<A>) => Equal<Id<A>> = id
export const getShow: <A>(S: Show<A>) => Show<Id<A>> = id
export const map_: <A, B>(fa: A, f: (a: A) => B) => B = (ma, f) => f(ma)
export const map: <A, B>(f: (a: A) => B) => (fa: A) => B = (f) => (fa) => map_(fa, f)
export const reduce_: <A, B>(fa: A, b: B, f: (b: B, a: A) => B) => B = (fa, b, f) =>
f(b, fa)
export const reduce: <A, B>(b: B, f: (b: B, a: A) => B) => (fa: A) => B = (b, f) => (
fa
) => reduce_(fa, b, f)
export const reduceRight_: <A, B>(fa: A, b: B, f: (a: A, b: B) => B) => B = (
fa,
b,
f
) => f(fa, b)
export const reduceRight: <A, B>(b: B, f: (a: A, b: B) => B) => (fa: A) => B = (
b,
f
) => (fa) => reduceRight_(fa, b, f)
export const Applicative = P.instance<P.Applicative<[IdURI]>>({
any: () => ({}),
both: (b) => (a) => [a, b],
map
})
//export const sequence: CSequence1<URI> = <F>(F: CApplicative<F>) => <A>(
// ta: Identity<HKT<F, A>>
//): HKT<F, Identity<A>> => {
// return F.map(id)(ta)
//}
//
//export const traverse: CTraverse1<URI> = <F>(F: CApplicative<F>) => <A, B>(
// f: (a: A) => HKT<F, B>
//): ((ta: Identity<A>) => HKT<F, Identity<B>>) => {
// return (ta) => F.map(id)(f(ta))
//}
//
//export const traverse_: Traverse1<URI> = <F>(F: CApplicative<F>) => <A, B>(
// ta: Identity<A>,
// f: (a: A) => HKT<F, B>
//): HKT<F, Identity<B>> => {
// return F.map(id)(f(ta))
//}
// export const identity: CMonad1<[IdURI]> &
// CFoldable1<URI> &
// CTraversable1<URI> &
// CAlt1<URI> &
// CComonad1<URI> &
// CChainRec1<URI> &
// CApplicative1<URI> = {
// URI,
// map,
// of: id,
// ap,
// chain,
// reduce,
// foldMap,
// reduceRight,
// traverse,
// sequence,
// alt,
// extract,
// extend,
// chainRec
// }
//
// export const identityAp: CApplicative1<URI> = {
// URI,
// map,
// of: id,
// ap
// }
//
// export const identityMonad: CMonad1<URI> & CApplicative1<URI> = {
// URI,
// map,
// of: id,
// ap,
// chain
// }
//
// export const Do = () => DoG(identityMonad)
//
// export const sequenceS =
// /*#__PURE__*/
// (() => AP.sequenceS(identityAp))()
//
// export const sequenceT =
// /*#__PURE__*/
// (() => AP.sequenceT(identityAp))()
//
// //
// // Compatibility with fp-ts ecosystem
// //
//
// const traverse__ = <F>(F: Applicative<F>) => <A, B>(
// ta: Identity<A>,
// f: (a: A) => HKT<F, B>
// ): HKT<F, Identity<B>> => {
// return F.map(f(ta), id)
// }
//
// const sequence_ = <F>(F: Applicative<F>) => <A>(
// ta: Identity<HKT<F, A>>
// ): HKT<F, Identity<A>> => {
// return F.map(ta, id)
// }
//
// export const identity_: Monad1<URI> &
// Foldable1<URI> &
// Traversable1<URI> &
// Alt1<URI> &
// Comonad1<URI> &
// ChainRec1<URI> &
// Applicative1<URI> = {
// URI,
// map: map_,
// of: id,
// ap: ap_,
// chain: chain_,
// reduce: reduce_,
// foldMap: foldMap_,
// reduceRight: reduceRight_,
// traverse: traverse__,
// sequence: sequence_,
// alt: alt_,
// extract,
// extend: extend_,
// chainRec
// }
//
+5
View File
@@ -8,6 +8,7 @@ import type { Commutative } from "../Classic/Commutative"
import type { Const } from "../Classic/Const"
import type { Either } from "../Classic/Either"
import type { Equal } from "../Classic/Equal"
import type { Id } from "../Classic/Id"
import type { Identity } from "../Classic/Identity"
import type { Ix, IxC } from "../Classic/IndexedT"
import type { Inverse } from "../Classic/Inverse"
@@ -101,6 +102,9 @@ export type SyncURI = typeof SyncURI
export const XPureURI = "XPure"
export type XPureURI = typeof XPureURI
export const IdURI = "Id"
export type IdURI = typeof IdURI
declare module "../Prelude/HKT" {
interface URItoKind<FC, TC, N extends string, K, Q, W, X, I, S, R, E, A> {
[ArrayURI]: Array<A>
@@ -128,6 +132,7 @@ declare module "../Prelude/HKT" {
[LayerURI]: Layer<R, E, A>
[SyncURI]: Sync<R, E, A>
[XPureURI]: XPure<S, S, R, E, A>
[IdURI]: Id<A>
}
interface URItoIndex<N extends string, K> {
[ArrayURI]: number