Install via yarn add locutus and import:
import { seq } from 'locutus/r/base/seq'.
Or with CommonJS: const { seq } = require('locutus/r/base/seq')
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.
#
code
expected result
1
seq(5)
[1, 2, 3, 4, 5]
2
seq(1, 5, 2)
[1, 3, 5]
3
seq(1, 2, undefined, 5)
[1, 1.25, 1.5, 1.75, 2]
Notes
Generates numeric sequences, similar to common R seq() forms.
Supported forms: seq(to), seq(from, to), seq(from, to, by), seq(from, to, undefined, length_out).
Here's what our current TypeScript equivalent to R's seq looks like.
exportfunctionseq(from: number, to?: number, by?: number, length_out?: number): number[] { // discuss at: https://locutus.io/r/seq/ // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Generates numeric sequences, similar to common R seq() forms. // note 2: Supported forms: seq(to), seq(from, to), seq(from, to, by), seq(from, to, undefined, length_out). // example 1: seq(5) // returns 1: [1, 2, 3, 4, 5] // example 2: seq(1, 5, 2) // returns 2: [1, 3, 5] // example 3: seq(1, 2, undefined, 5) // returns 3: [1, 1.25, 1.5, 1.75, 2]
const start = Number(to === undefined ? 1 : from) const end = Number(to === undefined ? from : to) if (!Number.isFinite(start) || !Number.isFinite(end)) { return [] }
const lengthOut = normalizeLengthOut(length_out) if (lengthOut !== null) { if (lengthOut <= 0) { return [] } if (lengthOut === 1) { return [start] } const step = (end - start) / (lengthOut - 1) constout: number[] = [] for (let i = 0; i < lengthOut; i++) { out.push(roundNumber(start + step * i)) } return out }
const step = by === undefined ? (end >= start ? 1 : -1) : Number(by) if (!Number.isFinite(step) || step === 0) { thrownewRangeError('seq(): by must be a non-zero finite number') }
if ((end - start) * step < 0) { return [] }
constout: number[] = [] const epsilon = Math.abs(step) * 1e-12 if (step > 0) { for (let value = start; value <= end + epsilon; value += step) { out.push(roundNumber(value)) } } else { for (let value = start; value >= end - epsilon; value += step) { out.push(roundNumber(value)) } }
return out }
functionnormalizeLengthOut(value: number | undefined): number | null { if (value === undefined) { returnnull }
const n = Number(value) if (!Number.isFinite(n)) { returnnull }
returnMath.trunc(n) }
functionroundNumber(value: number): number { returnNumber.parseFloat(value.toFixed(12)) }
exportfunctionseq(from, to, by, length_out) { // discuss at: https://locutus.io/r/seq/ // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Generates numeric sequences, similar to common R seq() forms. // note 2: Supported forms: seq(to), seq(from, to), seq(from, to, by), seq(from, to, undefined, length_out). // example 1: seq(5) // returns 1: [1, 2, 3, 4, 5] // example 2: seq(1, 5, 2) // returns 2: [1, 3, 5] // example 3: seq(1, 2, undefined, 5) // returns 3: [1, 1.25, 1.5, 1.75, 2]
const start = Number(to === undefined ? 1 : from) const end = Number(to === undefined ? from : to) if (!Number.isFinite(start) || !Number.isFinite(end)) { return [] }
const lengthOut = normalizeLengthOut(length_out) if (lengthOut !== null) { if (lengthOut <= 0) { return [] } if (lengthOut === 1) { return [start] } const step = (end - start) / (lengthOut - 1) const out = [] for (let i = 0; i < lengthOut; i++) { out.push(roundNumber(start + step * i)) } return out }
const step = by === undefined ? (end >= start ? 1 : -1) : Number(by) if (!Number.isFinite(step) || step === 0) { thrownewRangeError('seq(): by must be a non-zero finite number') }
if ((end - start) * step < 0) { return [] }
const out = [] const epsilon = Math.abs(step) * 1e-12 if (step > 0) { for (let value = start; value <= end + epsilon; value += step) { out.push(roundNumber(value)) } } else { for (let value = start; value >= end - epsilon; value += step) { out.push(roundNumber(value)) } }
return out }
functionnormalizeLengthOut(value) { if (value === undefined) { returnnull }
const n = Number(value) if (!Number.isFinite(n)) { returnnull }
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.