Num — number utilities
Working with numbers in a pipeline usually means scattering anonymous arrow functions everywhere:
Arr.map(n => n * 2), Arr.filter(n => n >= 0 && n <= 100). Num replaces these with
composable, named operations that read naturally inside pipe.
Generating number ranges
Section titled “Generating number ranges”Num.range produces an array of numbers from from to to (both inclusive), with an
optional step. When the step does not land exactly on to, the sequence stops at the last
value that does not exceed it:
This pairs naturally with Arr.map and Arr.filter to build datasets for charts, pagination, or
test fixtures without manually constructing arrays.
Curried arithmetic
Section titled “Curried arithmetic”add, subtract, multiply, and divide each take the operand first and the value last —
making them directly composable in pipe and Arr.map:
subtract(b)(a) = a - b and divide(b)(a) = a / b, so they read as “subtract b” and
“divide by b” — natural for transforming arrays of values.
Clamping values to a range
Section titled “Clamping values to a range”Num.clamp constrains a number to stay within [min, max] (both inclusive). Values below min
become min; values above max become max:
A common use case is normalising user input — ensuring a slider value stays within the allowed range before sending it to an API.
Checking membership with between
Section titled “Checking membership with between”Num.between is a predicate that returns true when the value falls within [min, max]
(inclusive on both ends). It composes directly with Arr.filter:
This eliminates the common n >= min && n <= max inline expression scattered throughout filter
callbacks.
Safe parsing
Section titled “Safe parsing”Num.parse converts a string to a number and wraps the result in Maybe — None if the string
doesn’t represent a valid number, Some otherwise:
This avoids the isNaN dance and integrates with the rest of the Maybe API:
Composing it all
Section titled “Composing it all”Num functions are designed to appear as steps in a pipe chain alongside Arr operations.
Here, a range of integers is scaled, filtered to a sub-range, and summed:
When to use Num
Section titled “When to use Num”Use Num when:
- You need to generate a sequence of numbers for iteration, pagination, or test data
- You’re mapping or filtering arrays of numbers and want to avoid inline arrow functions
- You need to constrain user input to a valid range with
clamp - You’re parsing numeric strings from form fields, query params, or configuration and want a typed
Maybeinstead of aNaNcheck
Keep using plain arithmetic operators when:
- The expression is a one-off inside a function body where readability is not improved by naming
- You don’t need to compose the operation in a pipeline