Skip to content

Latest commit

 

History

History
 
 

zion

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Zion

Fantasy Land compliant algebraic data types

General

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.

Instalation & usage

npm install @totalsoft/zion
import List from "@totalsoft/zion/data/list";
import { $do } from "@totalsoft/zion";

Algebraic data types

Maybe

data  Maybe a  =  Nothing | Just a

Instance of:

List

data List a = Cons a (List a) | Nil

Instance of:

Map

data Map k a

Instance of:

KeyValuePair

data KeyValuePair k a

Instance of:

Reader

data Reader r a

Instance of:

Step

data Step b a

Instance of:

Polymorphic functions

  • 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

Do notation

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)