Keyboard Shortcuts

General

Open/Close Shortcuts Menu?
Open Search Menu⌘/ctrl + /
Close Navigation Menu<
Open Navigation Menu>
Close Menuesc

Filter-only

Focus New Filter ButtonF/f
Focus Page ButtonP/p
Next Page+/=
Previous Page-/_

Ben's Thoughts

arrays
dx
elixir
enumerable
ergonomics
generator
generators
iterators
javascript
rust

A Little JavaScript Library I Published

Calendar

Table of Contents

Why Would I Do This?

Let’s start with two premises: 1. Programmers have to work with lists lot, and 2. Newer programming languages tend to have better features for programming. Or rather, let’s go with this premise: 1. I like functional programming, 2. I like the features of new programming languages. JavaScript, though updated every now and then, direly needs to be entirely reworked from the ground up. It’s missing really nice features like pattern matching, streams and iterators.

So what did I do to fix this? I created a package on the NPM registry. It uses a fluent API and a builder pattern and kinda works like Elixir streams (which have to be converted to list) or a Rust iterator that must have some sort of final method (like collect or fold).

Language: Elixir
1..3 |> Stream.map(&IO.inspect(&1)) |> Stream.map(&(&1 * 2)) |> Stream.map(&IO.inspect(&1)) |> Enum.to_list()

So what does the package look like in JavaScript?

Language: JavaScript
function* gen(start: number) { let position = start; while (true) { yield position++; } } const result = new ArrayStream(gen()) .skip(10) .filterMap((i) => (i % 2 === 0 ? i ** 2 : null)) .take(5) .reduce((acc, next) => acc + next, 0);

It works on anything that is iterable so not just generators but also arrays or something like Map.key.

So It’s Really Efficient, Right?

If you don’t know, Array methods like .map and .filter aren’t very efficient. They allocate new memory to create a copy of the array. If you have more than one operation, you’re replicating the array each time. So .map, .filter, .reduce will return three. A million operations? The array will be reallocated a million times. A single for loop that mutates it in place is much more efficient. So I would do that, right?

Well, the easiest solution for me was to create one intermediate array then add items to it. Some functions (filterMap, filter, take, skip) change the amount of items in the array, which would require replacing the items in the array and resizing it. Eh, not worth it.

In the end, its efficiency is based on the runtime. V8 (Chrome)? Array methods are similar until you get to 1 million items, then my solution goes in the tank. WebKit (Safari)? My solution’s about as efficient in any case, about the same as spider monkey (Firefox). I was expecting something much better. I’ll look into making it more efficient, but I’m happy with how the API turned out. It’s nice and ergonomic.