Install via yarn add locutus and import:
import { scan } from 'locutus/elixir/Enum/scan'.
Or with CommonJS: const { scan } = require('locutus/elixir/Enum/scan')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
if (hasInitial) { const reducer = maybeReducer if (!isScanReducer<TAccumulator, TValue>(reducer)) { thrownewTypeError('scan(): reducer must be a function') }
let accumulator = initialOrReducer asTAccumulator for (let i = 0; i < values.length; i++) { accumulator = reducer(accumulator, values[i] asTValue, i, values) out.push(accumulator) } return out }
if (!isScanReducer<TValue, TValue>(initialOrReducer)) { thrownewTypeError('scan(): reducer must be a function') }
const reducer = initialOrReducer let accumulator = values[0] asTValue out.push(accumulator) for (let i = 1; i < values.length; i++) { accumulator = reducer(accumulator, values[i] asTValue, i, values) out.push(accumulator) }
return out }
functionisScanReducer(value) { returntypeof value === 'function' } exportfunctionscan(values, initialOrReducer, maybeReducer) { // discuss at: https://locutus.io/elixir/scan/ // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Produces running accumulator states, similar to Elixir Enum.scan/2 and Enum.scan/3. // example 1: scan([1, 2, 3, 4], (acc, value) => Number(acc) + Number(value)) // returns 1: [1, 3, 6, 10] // example 2: scan([1, 2, 3], 10, (acc, value) => Number(acc) + Number(value)) // returns 2: [11, 13, 16] // example 3: scan([], 5, (acc, value) => Number(acc) + Number(value)) // returns 3: []
if (hasInitial) { const reducer = maybeReducer if (!isScanReducer(reducer)) { thrownewTypeError('scan(): reducer must be a function') }
let accumulator = initialOrReducer for (let i = 0; i < values.length; i++) { accumulator = reducer(accumulator, values[i], i, values) out.push(accumulator) } return out }
if (!isScanReducer(initialOrReducer)) { thrownewTypeError('scan(): reducer must be a function') }
const reducer = initialOrReducer let accumulator = values[0] out.push(accumulator) for (let i = 1; i < values.length; i++) { accumulator = reducer(accumulator, values[i], i, values) out.push(accumulator) }
return out }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.