Created
September 10, 2022 10:15
-
-
Save infu/8a015965bbc882fa6ecdc204d8dbdb8f to your computer and use it in GitHub Desktop.
Attempt to implement ReactiveX in Motoko with "chaining"
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
module { | |
public type Operator<X,Y> = (Observable<X>) -> (Observable<Y>); | |
public func map<X,Y>( fn: (X) -> (Y) ) : (Observable<X>) -> (Observable<Y>) { | |
return func ( x : Observable<X> ) { | |
Observable<Y>( func (subscriber) { | |
x.subscribe({ | |
next = func(v) { | |
subscriber.next( fn(v)) | |
} | |
}) | |
}); | |
} | |
}; | |
public type Listener<X> = { | |
next : (X) -> () | |
}; | |
public func pipe2<A,B>(ob: Observable<A>, op1 : Operator<A,B>) : Observable<B> { | |
op1(ob) | |
}; | |
public func pipe3<A,B,C>(ob: Observable<A>, op1 : Operator<A,B>, op2 : Operator<B,C>) : Observable<C> { | |
op2(op1(ob)) | |
}; | |
public func pipe4<A,B,C,D>(ob: Observable<A>, op1 : Operator<A,B>, op2 : Operator<B,C>, op3 : Operator<C,D>) : Observable<D> { | |
op3(op2(op1(ob))) | |
}; | |
public func pipe5<A,B,C,D,E>(ob: Observable<A>, op1 : Operator<A,B>, op2 : Operator<B,C>, op3 : Operator<C,D>, op4 : Operator<D,E>) : Observable<E> { | |
op4(op3(op2(op1(ob)))) | |
}; | |
public class Observable<A>( | |
init : (Listener<A>) -> () | |
) = this { | |
public func subscribe( fn : Listener<A> ) : () { | |
init(fn); | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment