diff --git a/src/lib/util/basic/list.ts b/src/lib/util/basic/list.ts index ffc672f..47219e2 100644 --- a/src/lib/util/basic/list.ts +++ b/src/lib/util/basic/list.ts @@ -1,36 +1,43 @@ -export type Nil = { readonly _tag: 'Nil' }; +export type Nil = {readonly _tag: 'Nil'} export type Cons = { - readonly _tag: 'Cons'; - readonly head: A; - readonly tail: List; -}; -export type List = Nil | Cons; + readonly _tag: 'Cons', + readonly head: A, + readonly tail: List, +} +export type List = Nil | Cons // Constructors -export const nil: Nil = { _tag: 'Nil' }; +export const nil: Nil = {_tag: 'Nil'} export const cons = (head: A, tail: List): List => ({ _tag: 'Cons', head, tail, -}); +}) // Operations -type ListMap = (f: (a: A) => B) => (fa: List) => List; -export const listMap: ListMap = (f) => (fa) => - fa._tag === 'Cons' ? cons(f(fa.head), listMap(f)(fa.tail)) : nil; +type ListMap = (f: (a: A) => B) => (fa: List) => List +export const listMap: ListMap = f => fa => + fa._tag === 'Cons' ? cons(f(fa.head), listMap(f)(fa.tail)) : nil type ListReduce = ( f: (b: B, a: A) => B, - initial: B -) => (fa: List) => B; -export const listReduce: ListReduce = (f, initial) => (fa) => - fa._tag === 'Cons' ? listReduce(f, f(initial, fa.head))(fa.tail) : initial; + initial: B, +) => (fa: List) => B +export const listReduce: ListReduce = (f, initial) => fa => + fa._tag === 'Cons' ? listReduce(f, f(initial, fa.head))(fa.tail) + : initial // Helpers -type FromArray = (arr: Array) => List; +type FromArray = (arr: Array) => List export const fromArray: FromArray = (arr: Array) => - arr.reduceRight((acc: List, val: A) => cons(val, acc), nil as List); + arr.reduceRight( + (acc: List, val: A) => cons(val, acc), + nil as List, + ) -type ToArray = (fa: List) => Array; +type ToArray = (fa: List) => Array export const toArray: ToArray = (fa: List) => - listReduce>((acc, val) => [...acc, val], [] as Array)(fa); + listReduce>( + (acc, val) => [...acc, val], + [] as Array, + )(fa)