Zion is a functional programming library inspired by Haskell and Fantasy Land.
It provides Haskell-like algebraic data-types and polymorphic functions against Fantasy Land algebraic structures.
npm install @totalsoft/zion
import List from "@totalsoft/zion/data/list";
import { $do } from "@totalsoft/zion";
data Maybe a = Nothing | Just a
Instance of:
data List a = Cons a (List a) | Nil
Instance of:
data Map k a
Instance of:
data KeyValuePair k a
Instance of:
data Reader r a
Instance of:
data Step b a
Instance of:
- pure :: Applicative f => TypeRep f -> a -> f a
- contramap :: Contravariant f => (b -> a) -> f a -> f b
- fold :: Monoid m, Foldable f => (a -> m a) -> f a -> m a
const monadSum = (monadX, monadY) =>
$do(function*() {
const x = yield monadX;
const y = yield monadY;
return x + y;
})
expect(monadSum(Just(5), Just(6))).toStrictEqual(Just(11))
expect(monadSum(Just(5), Nothing)).toStrictEqual(Nothing)
expect(monadSum(List.fromArray([1]), List.fromArray([2]))).toStrictEqual(List.fromArray([1+2]))
expect(monadSum(List.fromArray([1, 2]), List.fromArray([3]))).toStrictEqual(List.fromArray([1 + 3, 2 + 3]))
expect(monadSum([1, 2], [3])).toStrictEqual([1 + 3, 2 + 3])
expect(monadSum([1], [2])).toStrictEqual([1 + 2])
expect(monadSum(x => x + 1, x => x - 1)(7)).toBe(7 + 1 + 7 - 1)
expect(monadSum(x => x * 2, x => x - 1)(5)).toBe(5 * 2 + 5 - 1)