@strong-roots-capital/ratchet

Filter a stream of values monotonically

Usage no npm install needed!

<script type="module">
  import strongRootsCapitalRatchet from 'https://cdn.skypack.dev/@strong-roots-capital/ratchet';
</script>

README

ratchet

ratchet Build status npm version codecov

Filter a stream of values monotonically

Use the Option monad to "ratchet" a stream of values, guaranteeing processing over monotonically-changing values.

This function obeys the following invariants:

  • only yields monotonically-changing values
  • does not yield duplicates
  • does not yield any value that was not passed in

Install

npm install @strong-roots-capital/ratchet

Use

import { Ratchet } from '@strong-roots-capital/ratchet'
import { ordNumber } from 'fp-ts/lib/Ord'
import { pipe } from 'fp-ts/lib/pipeable'
import { map } from 'fp-ts/lib/Option'
import randomInt from 'random-int'

const ratchet = Ratchet(ordNumber)

function doTheThing(x: number) {
    pipe(
        ratchet(x),
        map((value: number) => {
            /* safely do something here with value */
            console.log(x)
        })
    )
    // `map` will only run when `x` is a value that would be
    // sorted after all previously-seen values
}

const input = Array(10).fill(100).map(randomInt)
console.log(input)
//=>[ 69, 33, 65, 12, 91, 34, 95, 91, 80, 12 ]

input.forEach(doTheThing)
//=>69
//=>91
//=>95

In this example, we use the ratchet to ensure each value processed is numerically higher than all previously-processed values.

Note: the ratchet will never emit a duplicate value.

Resources

Acknowledgments

Index

Functions

Functions

Ratchet

RatchetT›(ordering: Ord‹T›): function

Defined in src/ratchet.ts:20

Filter a stream of values monotonically.

Type parameters:

T

Parameters:

Name Type
ordering Ord‹T›

Returns: function

▸ (value: T): Option‹T›

Parameters:

Name Type
value T