prelude.ts
Intro
Prelude.ts is a typescript library which aims to make functional programming
concepts accessible and productive in typescript.
It provides immutable collections (Vector, Set, Map, Stream), and constructs such as
Option.
Vector.of(1,2,3)
.map(x => x*2)
.head()
Option.sequence(
Vector.of(Option.of(1), Option.of(2)))
Vector.of(1,2,3,4).groupBy(x => x%2)
Vector.of(1,2,3).zip("a", "b", "c").takeWhile(([k,v]) => k<3)
The collections are also javascript iterables, so if you have an ES6 runtime,
you can use the for .. of
construct on them. If you're not familiar with
immutable collections, list.append(newItem)
keeps list
unchanged; append()
returns a new list. Immutability helps reasonning about code.
You can check the tests for examples of use, and browse the
API documentation.
Note that the constructors are private, and you should use static methods to build
items, for instance Option.of
, Vector.of
, Vector.ofIterable
, and so on.
At this time most of the collections are implemented using the
HAMT algorithm,
and concretely the hamt_plus library.
Besides this dependency, I'll try to limit the number of dependencies.
In addition the library is written in idiomatic javascript style, with loops
instead of recursion, so the performance should be reasonable.
Structs and equality
Javascript doesn't have structural equality, except for primitive types.
So, 1 === 1
is true. But [1] === [1]
is not, and neither {a:1} === {a:1}
.
This poses problems for collections, because if you have a Set
, you don't
want duplicate elements because of this limited definition of equality.
For that reason, prelude.ts encourages you to define for your non-primitive types
methods equals(other: any): boolean
and hashCode(): number
(the same
methods that immutable.js uses).
With these methods, structural equality is achievable, and indeed
Vector.of(1,2,3).equals(Vector.of(1,2,3))
is true
. However this can only
work if the values you put in collections have themselves properly defined equality
(see how prelude.ts can help).
If these values don't have structural equality, then we can get no better than
===
behavior.
prelude.ts attempts to assist the programmer with this; it tries to encourage
the developer to do the right thing. First, it'll refuse types without properly
defined equality in Sets and in Maps keys. Second, it has a special terminology
for types without properly defined equality: 'structs'.
When you operate with for instance a Vector
, you'll have to explicitely say
when you deal with structs. So Vector.of([1])
will not compile.
You get (a longer version of) this message:
Type 'number[]' is not assignable to type 'HasEquals'.
Property 'equals' is missing in type 'number[]'.
So the solution is to use a struct-oriented function: Vector.ofStruct([1])
.
That version compiles just fine. But now you've been warned that you can't use
this value in a Set or as a Map key, and that equality features are not provided.
Similary, functions such a map
have alternate implementations such as mapStruct
to warn the programmer in these cases.
Wishlist/upcoming features
- Either
- Future, wrapping promises?
- Non-empty vector probably
- many more functions on existing classes
Out of scope for prelude.ts
- Free monads
- Monad transformers
- Effect tracking
- Higher-kinded types simulation
I think these concepts are not expressible in a good enough manner on a language
such as typescript.
Alternatives and Influences
- monet.js -- only has the
List
and
Option
collections, implemented in functional-style ES5. - immutables.js -- doesn't have the
Option
concept, the types can be clunky. - sanctuary and ramdajs
push global functions like
R.filter(R.where(...))
while prelude.ts prefers a
fluent-api style like list.filter(..).sortBy(...)
- lodash also has the global functions, and many functions
mutate the collections.
- vavr -- it's a java library, but it's the main inspiration for prelude.ts.
Caveats
- the API may change in the future (but types should protect you)
- there could still be bugs, it's still early days
Commands
npm install
npm test
npm run-script docgen
npm run benchmarks