Announcing Lazy Sequences (Seq Module)

I really like using lazy sequences when programming. They are often more comfortable, flexible, and fun to use than arrays. They are a very important data structure in many languages - see the F# Seq module, Enumerable in C#, OCaml Seq, iter tools in Rust, and Python itertools. There is a TC39 proposal to add some useful functions to JavaScript but until then there packages to fill the gap.

I’ve really missed having this functionality in ReScript. So I built a full-featured, well-tested, and documented library - the :boom: ReScript Seq module :boom:. There are nearly 100 useful functions. The links below will let you see a list of all the functions and see code samples to get a flavor for what you can do with it. I started from the OCaml source code (which somehow didn’t make it into Belt) but it uses a lot of recursion and caused stack overflows. So I adapted it. I researched tools from other languages/platforms and tried to incorporate the best stuff into a cohesive package.

ReScript Sequences Overview

Code examples

Hope you like it and find it useful. This is part of my rescript-extras package. I tried to break it out into its own thing but couldn’t due to circular references and am not too savvy with monorepos. But you can easily just use the Seq module if that is all you care about.

13 Likes

I just finished a big update to my Seq module. It now lives in its own repo. There are a bunch of new useful functions like split, which allows you to group adjacent items in a list and reduce them. reduceUntil and reduceWhile let you short-circuit a reduce operation. permutations and combinations can be useful for testing. everyOk converts a list of results into a list of ok values or returns the first error. pairAhead, window, and neighbors make it easy to analyze adjacent items in an array. Plus all the usual array/list functions you expect like find, map, findMap, findLast, filter, reverse, takeUntil, and sort. Everything is fully documented and tested.

Unless you need random access to items in an array, I think using lazy sequences is easier, more powerful, and more fun than using array-specific functions. All it takes is one call to fromArray and you can start using it. You can easily generate new sequences from scratch with range, unfold, foreverWith, and many others.

Here are code samples.

11 Likes