0.1.0 • Published 2 years ago
@line100/rescript-seq v0.1.0
rescript-seq
A Lazy Sequence
implimation. Highly abstracted stream like RxJs
but without time
.
Deleted Head Infinit Tail
┌───┐ ┌───┐ ┌──┬┐ ┌──┬┐ ┌──┬┐
│ │ │ │ │ a│┼───► b│┼───► c│┼───► ?
└───┘ └───┘ └──┴┘ └──┴┘ └──┴┘
▲ ▲
│ │
│ │
┌─────┬─┘ ┌──┴──┐
│ ref │ │ ref │
└─────┘ └─────┘
Installation
npm install @line100/rescript-seq
Usage
// infinit sequence of numbers
let rec createInfinitSeq = (i) =>
Seq.cons(i, Seq.Lazy(() => Some(createInfinitSeq(i + 1))))
// take and lazyTake
let seq = createInfinitSeq(0)
let first3 = Seq.take(seq, 3)
seqEqual(~message="take first 3 from infinit list", first3, list{0, 1, 2})
let first3 = Seq.lazyTake(seq, 3)
seqEqual(~message="lazyTake first 3 from infinit list", first3, list{0, 1, 2})
// map and lazyMap
let first3 = seq->Seq.take(3)->Seq.map(v => v + 1)
seqEqual(~message="map v + 1 after take first 3 from infinit list", first3, list{1, 2, 3})
let first3 = seq->Seq.lazyMap(v => v + 1)->Seq.take(3)
seqEqual(~message="lazyMap v + 1 before take first 3 from infinit list", first3, list{1, 2, 3})
API
Creation Operators
- fromList:
(list<'a>) => t<'a>
- fromArray:
(Array<'a>) => t<'a>
- fromArrayInPlace:
(Array<'a>) => t<'a>
- fromString:
(string) => t<string>
, convertstring
tosequence of char
Conditional Operator
- isEmpty:
(t<'a>) => bool
- equal:
(t<'a>, t<'a>) => bool
- match:
(t<'a>, list<'a>) => bool
- every:
(t<'a>, 'a => bool) = bool
Transformation Operator
- map:
(t<'a>, 'a => 'b) => t<'b>
append:
(t<'a>, t<'a>) => t<'a>
lazyMap:
(t<'a>, 'a => 'b) => t<'b>
filtering operators
- take:
(t<'a>, int) => t<'a>
- takeWhile:
(t<'a>, 'a => bool) => t<'a>
- drop:
(t<'a>, int) => t<'a>
dropWhile:
(t<'a>, 'a => bool) => t<'a>
lazyTake:
(t<'a>, int) => t<'a>
- lazyTakeWhile:
(t<'a>, 'a => bool) => t<'a>