Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c14cb7c

Browse files
committed
implement FantasyX typeclasses
1 parent 7523700 commit c14cb7c

File tree

1 file changed

+103
-48
lines changed

1 file changed

+103
-48
lines changed

‎src/fantasy/fantasyx.ts

Lines changed: 103 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ import { State } from './state'
55
import { Actions, Plan, XcomponentClass, Update } from '../interfaces'
66
import { $ } from './typeclasses'
77
import { map, Functor } from './typeclasses/functor'
8-
8+
import{FlatMap,flatMap}from'./typeclasses/flatmap'
99
import { Monad } from './typeclasses/monad'
10-
interface Semigroup {
11-
concat: <A>(a: A) => A
12-
}
13-
function isSemigroup(a: any): a is Semigroup {
14-
return a && typeof a.concat == 'function'
15-
}
10+
import { Cartesian, product } from './typeclasses/cartesian'
11+
import { Apply } from './typeclasses/apply'
12+
import { Applicative } from './typeclasses/applicative'
13+
import { datatype } from './typeclasses'
1614

15+
@datatype('FantasyX')
1716
export class FantasyX<F extends Stream, I, S, A> {
1817
plan: State<Subject<F, I>, $<F, State<S, A>>>
1918
constructor(plan: State<Subject<F, I>, $<F, State<S, A>>>) {
2019
this.plan = plan
2120
}
22-
apply(WrappedComponent: XcomponentClass<F, I, S>) {
21+
22+
apply(WrappedComponent: XcomponentClass<F, I, S>, actions?: Actions<I>) {
2323
return x((intent$: Subject<F, I>) => {
24-
return { update$: this.toStream(intent$) }
24+
return { update$: this.toStream(intent$), actions }
2525
})(WrappedComponent)
2626
}
2727

@@ -51,45 +51,23 @@ export class FantasyX<F extends Stream, I, S, A> {
5151
)
5252
}
5353

54-
// fold<B>(f: (acc: B, i: A) => B, base: B): FantasyX<E, I, S, B> {
55-
// return new FantasyX<E, I, S, B>(intent$ => {
56-
// let machine = this.plan(intent$)
57-
// let update$ = streamOps.merge(
58-
// streamOps.just(State.pure<S, B>(base)),
59-
// streamOps.scan<State<S, A>, State<S, B>>((accS, curS) => {
60-
// return Monad.State.flatMap(acc =>
61-
// Functor.State.map(cur =>
62-
// State.pure<S, B>(f(acc, cur))
63-
// , curS
64-
// ), accS
65-
// )
66-
// }, State.pure<S, B>(base), machine.update$
67-
// )
68-
// )
69-
// return { update,ドル actions: machine.actions }
70-
// })
71-
// }
72-
73-
// combine<C, B>(
74-
// f: (a: A, b: B) => C,
75-
// fB: FantasyX<E, I, S, B>
76-
// ): FantasyX<E, I, S, C> {
77-
// return new FantasyX<E, I, S, C>(intent$ => {
78-
// let machineB = fB.plan(intent$),
79-
// machineA = this.plan(intent$)
80-
// let update$ = streamOps.combine<State<S, A>, State<S, B>, State<S, C>>(
81-
// (S1, S2) =>
82-
// S1.chain(s1 =>
83-
// S2.chain(s2 =>
84-
// State.pure<S, C>(f(s1, s2))
85-
// )
86-
// )
87-
// , machineA.update,ドル machineB.update$
88-
// )
89-
// let actions = Object.assign({}, machineA.actions, machineB.actions)
90-
// return { update,ドル actions }
91-
// })
92-
// }
54+
combine<C, B>(
55+
f: (a: A, b: B) => C,
56+
fB: FantasyX<F, I, S, B>
57+
): FantasyX<F, I, S, C> {
58+
return new FantasyX<F, I, S, C>(
59+
Monad.State.flatMap(updateA$ => (
60+
Functor.State.map(updateB$ => (
61+
streamOps.combine<State<S, A>, State<S, B>, State<S, C>>((S1, S2) => (
62+
Monad.State.flatMap(s1 => (
63+
Functor.State.map(s2 => (
64+
f(s1, s2)
65+
), S2)
66+
), S1)
67+
), updateA$, updateB$)
68+
), fB.plan)
69+
), this.plan))
70+
}
9371

9472
// patch(f: (a: A) => Partial<S> = _ => _): FantasyX<E, I, S, void> {
9573
// return new FantasyX<E, I, S, void>(intent$ => {
@@ -219,3 +197,80 @@ export class FantasyX<F extends Stream, I, S, A> {
219197
// }
220198

221199
}
200+
201+
declare module './typeclasses' {
202+
export interface _<A> {
203+
"FantasyX": FantasyX<Stream, any, any, A>
204+
}
205+
}
206+
207+
export class FantasyXFunctor implements Functor<"FantasyX"> {
208+
map<A, B, I, S>(f: (a: A) => B, fa: FantasyX<Stream, I, S, A>): FantasyX<Stream, I, S, B> {
209+
return fa.map(f)
210+
}
211+
}
212+
213+
declare module './typeclasses/functor' {
214+
export namespace Functor {
215+
export let FantasyX: FantasyXFunctor
216+
}
217+
}
218+
219+
Functor.FantasyX = new FantasyXFunctor
220+
221+
export class FantasyXCartesian implements Cartesian<"FantasyX"> {
222+
product<A, B, I, S>(fa: FantasyX<Stream, I, S, A>, fb: FantasyX<Stream, I, S, B>): FantasyX<Stream, I, S, [A, B]> {
223+
return new FantasyX(
224+
FlatMap.State.flatMap(s1$ => (
225+
Functor.State.map(s2$ => (
226+
streamOps.combine((a, b) => Cartesian.State.product(a, b), s1$, s2$)
227+
), fb.plan)
228+
), fa.plan))
229+
}
230+
}
231+
232+
declare module './typeclasses/cartesian' {
233+
export namespace Cartesian {
234+
export let FantasyX: FantasyXCartesian
235+
}
236+
}
237+
238+
Cartesian.FantasyX = new FantasyXCartesian
239+
240+
export class FantasyXApply implements Apply<"FantasyX"> {
241+
ap<A, B, I, S>(
242+
fab: FantasyX<Stream, I, S, (a: A) => B>,
243+
fa: FantasyX<Stream, I, S, A>
244+
): FantasyX<Stream, I, S, B> {
245+
return new FantasyX(
246+
FlatMap.State.flatMap(s1$ => (
247+
Functor.State.map(s2$ => (
248+
streamOps.combine((s1, s2) => Apply.State.ap(s1, s2), s1$, s2$)
249+
), fa.plan)
250+
), fab.plan))
251+
}
252+
map = Functor.FantasyX.map
253+
product = Cartesian.FantasyX.product
254+
}
255+
256+
declare module './typeclasses/apply' {
257+
export namespace Apply {
258+
export let FantasyX: FantasyXFunctor
259+
}
260+
}
261+
262+
Apply.FantasyX = new FantasyXFunctor
263+
264+
export class FantasyXApplicative extends FantasyXApply {
265+
pure<I, A>(v: A): FantasyX<Stream, I, A, A> {
266+
return Applicative.Xstream.pure<I, A>(v).toFantasyX()
267+
}
268+
}
269+
270+
declare module './typeclasses/applicative' {
271+
export namespace Applicative {
272+
export let FantasyX: FantasyXApplicative
273+
}
274+
}
275+
276+
Applicative.FantasyX = new FantasyXApplicative

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /