Compare commits

..

No commits in common. "55be1efee1c434d1298686fdd1ff61511fafa0d3" and "b2d5ece8f0cbf2d723decc714f3f7afb3d7c2ff0" have entirely different histories.

3 changed files with 23 additions and 139 deletions

View file

@ -42,10 +42,3 @@ export const liftF = x => suspend(
/** @type {InferredMorphism<T>} */(pure) /** @type {InferredMorphism<T>} */(pure)
) )
/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
export const sum = (a, b) => a + b

View file

@ -1,53 +0,0 @@
import { Algebra, Monad } from './index.js'
/** @import { Fn, InferredMorphism, Morphism } from './types.js' */
/** @template {Fn} T */
export class IO extends Algebra(Monad) {
_effect
/**
* @type {T} effect
*/
constructor(effect) {
super()
this._effect = effect
}
/**
* @template {Fn} T
* @param {T} a
*/
static of(a) {
return new IO(() => a)
}
/**
* @param {InferredMorphism<T>} f
*/
chain(f) {
return IO.of(() => f(this.run()).run())
}
/**
* @param {InferredMorphism<T>} f
*/
map(f) {
return IO.of(() => f(this.run()))
}
/**
* @template {Fn} U
* @this {IO<T>}
* @param {IO<Morphism<T, U>>} other
* @returns {IO<U>}
*/
ap(other) {
return IO.of(() => other.run()(this.run()))
}
run() {
return this._effect()
}
}

View file

@ -1,7 +1,9 @@
import { concat } from '../iter.js'
import { liftF } from './fn.js'
import { Pure, Suspend } from './free.js' import { Pure, Suspend } from './free.js'
import { AlgebraWithBase, Category, Comonad, Foldable, Monoid } from './index.js' import { AlgebraWithBase, Category, Comonad, Foldable, Monoid } from './index.js'
/** @import { InferredMorphism, Morphism } from './types.js' */ /** @import { InferredMorphism } from './types.js' */
const Nil = Symbol('Nil') const Nil = Symbol('Nil')
@ -26,27 +28,6 @@ class ListPure extends AlgebraWithBase(Pure)(Category, Foldable, Monoid) {
return Empty return Empty
} }
/**
* @template U
* @this {ListPure<T>}
* @param {Morphism<T, ListPure<U>>} f
* @returns {ListPure<U>}
*/
chain(f) {
return f(this.head())
}
/**
* @template U
* @this {ListPure<T>}
* @param {Morphism<T, U>} f
* @returns {ListPure<U>}
*/
map(f) {
return new ListPure(f(this._value))
}
/** /**
* @template U * @template U
* @param {(acc: U, value: T) => U} f * @param {(acc: U, value: T) => U} f
@ -56,18 +37,6 @@ class ListPure extends AlgebraWithBase(Pure)(Category, Foldable, Monoid) {
reduce(f, init) { reduce(f, init) {
return f(init, this._value) return f(init, this._value)
} }
count() {
return this.isEmpty() ? 0 : 1
}
/**
* @this {List<any>}
* @returns {this is Empty}
*/
isEmpty() {
return this === Empty
}
} }
/** /**
@ -76,29 +45,24 @@ class ListPure extends AlgebraWithBase(Pure)(Category, Foldable, Monoid) {
*/ */
class ListSuspend extends AlgebraWithBase(Suspend)(Foldable, Monoid, Comonad) { class ListSuspend extends AlgebraWithBase(Suspend)(Foldable, Monoid, Comonad) {
/** /**
* @param {T} head * @param {Iterator<T>} iter
* @param {() => List<T>} tail
*/ */
constructor(head, tail) { constructor(iter) {
super( const next = iter.next()
head, const value = /** @type {T} */ (next.value)
/** @type {InferredMorphism<T>} */(tail)
)
}
const fn = /** @type {InferredMorphism<T>} */ (next.done ? List.empty : () => new ListSuspend(iter))
static empty() { super(value, fn)
return List.empty()
} }
/** /**
* @template T * @template {Iterable<T>} T
* @this {ListSuspend<T>} * @this {ListSuspend<Iterator<T>>}
* @param {T} value * @param {Iterator<T>} value
* @returns {ListSuspend<T>} * @returns {ListSuspend<T>}
*/ */
cons(value) { cons(value) {
return new ListSuspend(value, () => this) return new ListSuspend(concat(value, this._value))
} }
head() { head() {
@ -124,13 +88,8 @@ class ListSuspend extends AlgebraWithBase(Suspend)(Foldable, Monoid, Comonad) {
return this.reduce(reduceArray, []) return this.reduce(reduceArray, [])
} }
count() { static empty() {
return this.tail().count() + 1 return List.empty()
}
/** @returns {this is Empty} */
isEmpty() {
return false
} }
} }
@ -140,37 +99,22 @@ class ListSuspend extends AlgebraWithBase(Suspend)(Foldable, Monoid, Comonad) {
*/ */
export class List { export class List {
/** /**
* @template T * @template {Iterable<T>} T
* @param {T} value * @param {T} value
* @returns {List<T>} * @returns {ListSuspend<T>}
*/ */
static of(value) { static of(value) {
return new ListSuspend(value, List.empty) // @ts-ignore
return new ListSuspend(liftF(value))
} }
/** /**
* @template T * @template T
* @param {Iterable<T>} iterable * @param {Iterable<T>} iterator
* @returns {List<T>} * @returns {ListSuspend<T>}
*/ */
static from(iterable) { static from(iterator) {
const iterator = Iterator.from(iterable) return new ListSuspend(Iterator.from(iterator))
return List.fromIterator(iterator)
}
/**
* @template T
* @param {Iterator<T>} iterator
* @returns {List<T>}
*/
static fromIterator(iterator) {
const next = iterator.next()
if (next.done) {
return List.empty()
} else {
return new ListSuspend(next.value, () => List.fromIterator(iterator))
}
} }
static empty() { static empty() {