flow-static-land
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -15,2 +15,9 @@ # Changelog | ||
## 0.2.3 | ||
- **New Feature** | ||
- `Foldable`: add default `foldMap` implementation (@gcanti) | ||
- `ChainRec` (@gcanti) | ||
- `Yoneda` (@gcanti) | ||
## 0.2.2 | ||
@@ -17,0 +24,0 @@ |
{ | ||
"name": "flow-static-land", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Implementation of common algebraic types in JavaScript + Flow", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -9,2 +9,22 @@ # Features | ||
- `Semigroup` | ||
- `Monoid` | ||
- `Functor` | ||
- `Applicative` | ||
- `Monad` | ||
- `Foldable` | ||
- `Traversable` | ||
- `ChainRec` | ||
- `Alternative` | ||
- `Eff` | ||
- `Arr` | ||
- `Maybe` | ||
- `Either` | ||
- `Task` | ||
- `State` | ||
- `Writer` | ||
- `Reader` | ||
and many more... | ||
# Example | ||
@@ -52,3 +72,2 @@ | ||
- [Phantom types with Flow](https://medium.com/@gcanti/phantom-types-with-flow-828aff73232b) | ||
- [Refinements with Flow](https://medium.com/@gcanti/refinements-with-flow-9c7eeae8478b) | ||
@@ -55,0 +74,0 @@ # More examples |
@@ -67,4 +67,4 @@ // @flow | ||
export function reduce<A, B>(f: (a: A, b: B) => A, a: A, fb: Arr<B>): A { | ||
return prj(fb).reduce(f, a) | ||
export function reduce<A, B>(f: (b: B, a: A) => B, b: B, fa: Arr<A>): B { | ||
return prj(fa).reduce(f, b) | ||
} | ||
@@ -71,0 +71,0 @@ |
@@ -58,4 +58,4 @@ // @flow | ||
function reduce<A, B>(f: (a: A, b: B) => A, a: A, fb: HKT<IsCompose<F1, F2>, B>): A { | ||
return f1.reduce((a, gb) => f2.reduce(f, a, gb), a, prj(fb)) | ||
function reduce<A, B>(f: (b: B, a: A) => B, b: B, fa: HKT<IsCompose<F1, F2>, A>): B { | ||
return f1.reduce((a, gb) => f2.reduce(f, a, gb), b, prj(fa)) | ||
} | ||
@@ -62,0 +62,0 @@ |
@@ -113,8 +113,8 @@ // @flow | ||
export function reduce<L, A, B>(f: (a: A, b: B) => A, a: A, fb: Either<L, B>): A { | ||
const b = prj(fb) | ||
if (b instanceof Left) { | ||
return a | ||
export function reduce<L, A, B>(f: (b: B, a: A) => B, b: B, fa: Either<L, A>): B { | ||
const a = prj(fa) | ||
if (a instanceof Left) { | ||
return b | ||
} | ||
return f(a, b.value0) | ||
return f(b, a.value0) | ||
} | ||
@@ -121,0 +121,0 @@ |
// @flow | ||
import type { Monoid } from './Monoid' | ||
import { HKT } from './HKT' | ||
/* | ||
`Foldable` represents data structures which can be _folded_. | ||
- `foldr` folds a structure from the right (not implemented) | ||
- `foldl` folds a structure from the left | ||
- `foldMap` folds a structure by accumulating values in a `Monoid` | ||
reduce = foldl | ||
*/ | ||
export interface Foldable<F> { | ||
reduce<A, B>(f: (a: A, b: B) => A, a: A, fb: HKT<F, B>): A | ||
reduce<A, B>(f: (b: B, a: A) => B, b: B, fa: HKT<F, A>): B | ||
} | ||
/* A default implementation of `foldMap` using `foldl`. */ | ||
export function foldMap<F, M, A>(foldable: Foldable<F>, monoid: Monoid<M>, f: (a: A) => M, fa: HKT<F, A>): M { | ||
return foldable.reduce((acc, x) => monoid.concat(f(x), acc), monoid.empty(), fa) | ||
} |
@@ -14,3 +14,8 @@ // @flow | ||
import type { Comonad } from './Comonad' | ||
import type { ChainRec } from './ChainRec' | ||
import type { Either } from './Either' | ||
import { tailRec } from './ChainRec' | ||
import { compose } from './Fun' | ||
class IsIdentity {} | ||
@@ -46,4 +51,4 @@ | ||
export function reduce<A, B>(f: (a: A, b: B) => A, a: A, fb: Identity<B>): A { | ||
return f(a, prj(fb)) | ||
export function reduce<A, B>(f: (b: B, a: A) => B, b: B, fa: Identity<A>): B { | ||
return f(b, prj(fa)) | ||
} | ||
@@ -65,2 +70,6 @@ | ||
export function chainRec<A, B>(f: (a: A) => Identity<Either<A, B>>, a: A): B { | ||
return tailRec(compose(extract, f), a) | ||
} | ||
export function getSetoid<A>(setoid: Setoid<A>): Setoid<Identity<A>> { | ||
@@ -110,3 +119,4 @@ return { | ||
extend, | ||
extract | ||
extract, | ||
chainRec | ||
}: Monad<IsIdentity> & | ||
@@ -117,3 +127,4 @@ Foldable<IsIdentity> & | ||
Extend<IsIdentity> & | ||
Comonad<IsIdentity>) | ||
Comonad<IsIdentity> & | ||
ChainRec<IsIdentity>) | ||
} |
@@ -91,5 +91,5 @@ // @flow | ||
export function reduce<A, B>(f: (a: A, b: B) => A, a: A, fb: Maybe<B>): A { | ||
const b = prj(fb) | ||
return b == null ? a : f(a, b) | ||
export function reduce<A, B>(f: (b: B, a: A) => B, b: B, fa: Maybe<A>): B { | ||
const a = prj(fa) | ||
return a == null ? b : f(b, a) | ||
} | ||
@@ -96,0 +96,0 @@ |
193650
153
4171
102