finished list
This commit is contained in:
parent
0336213ca9
commit
6e2f725e7a
3 changed files with 64 additions and 13 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { curry } from '../curry.js'
|
import { curry } from '../curry.js'
|
||||||
import { Free, Suspend, suspend, pure } from './free.js'
|
import { Suspend, suspend, pure } from './free.js'
|
||||||
|
|
||||||
/** @import {InferredMorphism, Morphism, ChainConstructor} from './types.js' */
|
/** @import {InferredMorphism, Morphism, ChainConstructor} from './types.js' */
|
||||||
|
|
||||||
|
@ -42,4 +42,3 @@ export const liftF = x => suspend(
|
||||||
/** @type {InferredMorphism<T>} */(pure)
|
/** @type {InferredMorphism<T>} */(pure)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { concat } from '../iter.js'
|
||||||
import { liftF } from './fn.js'
|
import { liftF } from './fn.js'
|
||||||
import { Pure, Suspend } from './free.js'
|
import { Pure, Suspend } from './free.js'
|
||||||
import { AlgebraWithBase, Comonad, Foldable } from './index.js'
|
import { AlgebraWithBase, Comonad, Foldable } from './index.js'
|
||||||
|
@ -12,7 +13,7 @@ const Nil = Symbol('Nil')
|
||||||
*/
|
*/
|
||||||
class ListPure extends AlgebraWithBase(Pure)(Foldable) {
|
class ListPure extends AlgebraWithBase(Pure)(Foldable) {
|
||||||
head() {
|
head() {
|
||||||
return this.head
|
return this._value
|
||||||
}
|
}
|
||||||
|
|
||||||
tail() {
|
tail() {
|
||||||
|
@ -30,14 +31,13 @@ class ListPure extends AlgebraWithBase(Pure)(Foldable) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @template T
|
* @template T
|
||||||
* @extends {Suspend<T>}
|
* @extends {Suspend<T>}
|
||||||
*/
|
*/
|
||||||
class ListSuspend extends AlgebraWithBase(Suspend)(Foldable, Comonad) {
|
class ListSuspend extends AlgebraWithBase(Suspend)(Foldable, Comonad) {
|
||||||
/**
|
/**
|
||||||
* @param {IteratorObject<T>} iter
|
* @param {Iterator<T>} iter
|
||||||
*/
|
*/
|
||||||
constructor(iter) {
|
constructor(iter) {
|
||||||
const next = iter.next()
|
const next = iter.next()
|
||||||
|
@ -47,6 +47,16 @@ class ListSuspend extends AlgebraWithBase(Suspend)(Foldable, Comonad) {
|
||||||
super(value, fn)
|
super(value, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template {Iterable<T>} T
|
||||||
|
* @this {ListSuspend<Iterator<T>>}
|
||||||
|
* @param {Iterator<T>} value
|
||||||
|
* @returns {ListSuspend<T>}
|
||||||
|
*/
|
||||||
|
cons(value) {
|
||||||
|
return new ListSuspend(concat(value, this._value))
|
||||||
|
}
|
||||||
|
|
||||||
head() {
|
head() {
|
||||||
return this._value
|
return this._value
|
||||||
}
|
}
|
||||||
|
@ -67,7 +77,7 @@ class ListSuspend extends AlgebraWithBase(Suspend)(Foldable, Comonad) {
|
||||||
}
|
}
|
||||||
|
|
||||||
extract() {
|
extract() {
|
||||||
return this.reduce((acc, value) => acc.concat(value), [])
|
return this.reduce(reduceArray, [])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,8 +112,16 @@ export class List {
|
||||||
|
|
||||||
const Empty = new ListPure(Nil)
|
const Empty = new ListPure(Nil)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template T
|
||||||
|
* @param {T[]} acc
|
||||||
|
* @param {T} value
|
||||||
|
* @returns {T[]}
|
||||||
|
*/
|
||||||
|
const reduceArray = (acc, value) => acc.concat(value)
|
||||||
|
|
||||||
const a = Array.from({ length: 100 }).map((_, i) => i)
|
|
||||||
|
const a = Array.from({ length: 10 }).map((_, i) => i)
|
||||||
|
|
||||||
const wawa = List.from(a)
|
const wawa = List.from(a)
|
||||||
console.log(wawa.reduce((acc, value) => acc.concat(value), []))
|
console.log(wawa.reduce((acc, value) => acc.concat(value), []))
|
||||||
|
|
34
src/iter.js
Normal file
34
src/iter.js
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/**
|
||||||
|
* @template T
|
||||||
|
* @param {T | Iterable<T> | Iterator<T>} value
|
||||||
|
* @returns {value is Iterable<T>}
|
||||||
|
*/
|
||||||
|
export function isIterable(value) {
|
||||||
|
return value[Symbol.iterator] != null
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @template T
|
||||||
|
* @param {T | Iterable<T> | Iterator<T>} value
|
||||||
|
* @yields {T}
|
||||||
|
*/
|
||||||
|
export function* iter(value) {
|
||||||
|
if (isIterable(value)) {
|
||||||
|
yield* Iterator.from(value)
|
||||||
|
} else {
|
||||||
|
yield value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template T
|
||||||
|
* @param {...(Iterable<T> | Iterator<T>)} iterators
|
||||||
|
* @yields {T}
|
||||||
|
*/
|
||||||
|
export const concat = function*(...iterators) {
|
||||||
|
for (const iter of iterators) {
|
||||||
|
for (const item of Iterator.from(iter)) {
|
||||||
|
yield item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue