added some list functions

This commit is contained in:
Rowan 2025-04-17 20:27:14 -05:00
parent bcc2d003bf
commit fbbd99765b
3 changed files with 15 additions and 7 deletions

View file

@ -1,6 +1,6 @@
import { curry, curryN } from './curry.js'
import { dispatch } from './fantasy-land.js'
import { head, concat as iconcat, isIterable, iter, tail } from './list.js'
import { head, concat as iterconcat, iter, tail } from './list.js'
/** @import { Fn, Morphism, InferredMorphism, Predicate } from './types.js' */
@ -14,7 +14,7 @@ export const id = x => x
export const concat = dispatch(['fantasy-land/concat', 'concat'],
(b, a) => {
if (isIterable(a) || isIterable(b)) {
return iconcat(iter(a), iter(b))
return iterconcat(iter(a), iter(b))
} else {
return a.concat(b)
}

View file

@ -1,4 +1,5 @@
import { curry } from './curry.js'
import { concat } from './function.js'
import { inc } from './math.js'
/**
@ -28,7 +29,7 @@ export function* iter(value) {
* @param {...(Iterable<T> | Iterator<T>)} iterators
* @yields {T}
*/
export const concat = function*(...iterators) {
export const iterConcat = function*(...iterators) {
for (const iter of iterators) {
for (const item of Iterator.from(iter)) {
yield item
@ -112,12 +113,20 @@ export const tail = a => drop(1, a)
*/
export const last = a => nth(length(a) - 1, a)
export const append = curry(
/*
* @param {T | T[] | Iterable<T>} x
* @param {T[] | Iterable<T>} xs
*/
(x, xs) => concat(xs, x)
)
export const prepend = curry(
/**
* @template T
* @param {T} x
* @param {Iterable<T>} xs
* @param {T | T[] | Iterable<T>} x
* @param {T[] | Iterable<T>} xs
*/
(x, xs) => concat([x], xs))
(x, xs) => concat(x, xs))

View file

@ -25,5 +25,4 @@ export const is = curry(
export const isFn = x => type(x) === 'function'
export const isArray = Array.isArray
export const isIterable = x => x != null ? typeof x[Symbol.iterator] === 'function' : false