-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a Nest type to kind.ts that allows for easier nesting of kinds. Modified Newtype to use kind.ts#Hold instead of a fake object/accessor pattern. Also played with various ideas in contrib and examples.
- Loading branch information
Showing
8 changed files
with
196 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { In, Kind, Out } from "../kind.ts"; | ||
import type { Flatmappable } from "../flatmappable.ts"; | ||
|
||
import { todo } from "../fn.ts"; | ||
|
||
export interface KindGenerator extends Kind { | ||
readonly kind: Generator<Out<this, 0>, Out<this, 1>, In<this, 0>>; | ||
} | ||
|
||
export const FlatmappableGenerator: Flatmappable<KindGenerator> = { | ||
wrap: (a) => { | ||
const ret = function* () { | ||
yield a; | ||
return undefined; | ||
}; | ||
return ret; | ||
}, | ||
map: todo(), | ||
apply: todo(), | ||
flatmap: todo(), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { Scheduler, Stream } from "npm:@most/[email protected]"; | ||
import type { $, In, InOut, Kind, Out } from "../kind.ts"; | ||
import type { $, Kind, Nest, Out } from "../kind.ts"; | ||
import type { Wrappable } from "../wrappable.ts"; | ||
import type { Mappable } from "../mappable.ts"; | ||
import type { Applicable } from "../applicable.ts"; | ||
|
@@ -76,7 +76,12 @@ export const map: Mappable<KindStream>["map"] = M.map; | |
|
||
export const apply: Applicable<KindStream>["apply"] = M.ap; | ||
|
||
export const flatmap: Flatmappable<KindStream>["flatmap"] = M.chain; | ||
export const flatmap: Flatmappable<KindStream>["flatmap"] = (faui) => (ua) => | ||
pipe( | ||
ua, | ||
M.map(faui), | ||
M.switchLatest, | ||
); | ||
|
||
export const WrappableStream: Wrappable<KindStream> = { wrap }; | ||
|
||
|
@@ -98,14 +103,7 @@ export const bindTo = createBindTo(FlatmappableStream); | |
export const tap = createTap(FlatmappableStream); | ||
|
||
export interface TransformStream<U extends Kind> extends Kind { | ||
readonly kind: Stream< | ||
$< | ||
U, | ||
[Out<this, 0>, Out<this, 1>, Out<this, 2>], | ||
[In<this, 0>], | ||
[InOut<this, 0>] | ||
> | ||
>; | ||
readonly kind: Stream<Nest<U, this>>; | ||
} | ||
|
||
export function transformStream<U extends Kind>( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// deno-lint-ignore-file no-explicit-any | ||
// | ||
import type { $, Hold, In, Kind, Out } from "../kind.ts"; | ||
import type { Pair } from "../pair.ts"; | ||
import type { Mappable } from "../mappable.ts"; | ||
|
||
import * as O from "../option.ts"; | ||
import * as F from "../fn.ts"; | ||
import * as P from "../pair.ts"; | ||
import { flow, pipe, todo } from "../fn.ts"; | ||
|
||
export interface Profunctor<U extends Kind> { | ||
readonly dimap: <D, L, A, I, B = never, C = never, E = unknown>( | ||
fld: (l: L) => D, | ||
fai: (a: A) => I, | ||
) => (ua: $<U, [A, B, C], [D], [E]>) => $<U, [I, B, C], [L], [E]>; | ||
} | ||
|
||
export interface Forget<R, A, B> extends Hold<B> { | ||
readonly runForget: (a: A) => R; | ||
} | ||
|
||
export interface KindForget extends Kind { | ||
readonly kind: Forget<Out<this, 1>, In<this, 0>, Out<this, 0>>; | ||
} | ||
|
||
export const ProfunctorForget: Profunctor<KindForget> = { | ||
dimap: (fld, _fai) => (ua) => ({ | ||
runForget: flow(fld, ua.runForget), | ||
}), | ||
}; | ||
|
||
export interface Star<U extends Kind, X, A> { | ||
readonly runStar: <B = never, C = never, D = unknown, E = unknown>( | ||
x: X, | ||
) => $<U, [A, B, C], [D], [E]>; | ||
} | ||
|
||
export interface KindStar<U extends Kind> extends Kind { | ||
readonly kind: Star<U, In<this, 0>, Out<this, 0>>; | ||
} | ||
|
||
export function profunctorStar<U extends Kind>( | ||
M: Mappable<U>, | ||
): Profunctor<KindStar<U>> { | ||
return { | ||
dimap: (fld, fai) => (ua) => ({ | ||
// This should work but we need to do some type spelunking | ||
runStar: (flow(fld, ua.runStar, M.map(fai))) as any, | ||
}), | ||
}; | ||
} | ||
|
||
export interface Strong<U extends Kind> extends Profunctor<U> { | ||
readonly first: < | ||
A, | ||
X = never, | ||
B = never, | ||
C = never, | ||
D = unknown, | ||
E = unknown, | ||
>( | ||
ua: $<U, [A, B, C], [D], [E]>, | ||
) => $<U, [Pair<A, X>, B, C], [Pair<D, X>], [E]>; | ||
readonly second: < | ||
A, | ||
X = never, | ||
B = never, | ||
C = never, | ||
D = unknown, | ||
E = unknown, | ||
>( | ||
ua: $<U, [A, B, C], [D], [E]>, | ||
) => $<U, [Pair<X, A>, B, C], [Pair<X, D>], [E]>; | ||
} | ||
|
||
export function strong<U extends Kind>({ dimap }: Profunctor<U>): Strong<U> { | ||
return { | ||
dimap, | ||
first: dimap(P.getFirst, (a) => P.pair(a, null as any)), | ||
second: dimap(P.getSecond, (a) => P.pair(null as any, a)), | ||
}; | ||
} | ||
|
||
export const ProfunctorStarFn: Profunctor<KindStar<F.KindFn>> = profunctorStar( | ||
F.MappableFn, | ||
); | ||
export const StrongStarFn: Strong<KindStar<F.KindFn>> = strong( | ||
ProfunctorStarFn, | ||
); | ||
|
||
// Hmm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { $, Kind, Out } from "../kind.ts"; | ||
import type { Option } from "../option.ts"; | ||
|
||
import * as O from "../option.ts"; | ||
import * as A from "../array.ts"; | ||
import { pipe } from "../fn.ts"; | ||
|
||
export type Tree<A> = { | ||
readonly value: Option<A>; | ||
readonly forest: Forest<A>; | ||
}; | ||
|
||
export type Forest<A> = ReadonlyArray<Tree<A>>; | ||
|
||
// deno-lint-ignore no-explicit-any | ||
export type AnyTree = Tree<any>; | ||
|
||
export type Type<U> = U extends Tree<infer A> ? A : never; | ||
|
||
export interface KindTree extends Kind { | ||
readonly kind: Tree<Out<this, 0>>; | ||
} | ||
|
||
export function tree<A>(value: Option<A>, forest: Forest<A> = []): Tree<A> { | ||
return { value, forest }; | ||
} | ||
|
||
export function init<A = never>(forest: Forest<A> = []): Tree<A> { | ||
return { value: O.none, forest }; | ||
} | ||
|
||
export function wrap<A>(value: A, forest: Forest<A> = []): Tree<A> { | ||
return { value: O.some(value), forest }; | ||
} | ||
|
||
export function map<A, I>(fai: (a: A) => I): (ua: Tree<A>) => Tree<I> { | ||
const mapValue = O.map(fai); | ||
return (ua) => tree(mapValue(ua.value), pipe(ua.forest, A.map(map(fai)))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters