Skip to content

Instantly share code, notes, and snippets.

@fvilante
Created May 14, 2019 15:36
Show Gist options
  • Save fvilante/52568ec6a29a6a3b1de864fe430be443 to your computer and use it in GitHub Desktop.
Save fvilante/52568ec6a29a6a3b1de864fe430be443 to your computer and use it in GitHub Desktop.
Don't know why but this seems very impressive to me
// Don't know why but this seems very impressive to me
type Func<A, B> = (_: A) => B
class Value<A> {
constructor(private _content: A ) { }
static of = <A>(data: A): Value<A> => new Value(data)
map = <B>(f: Func<A, B>): Value<B> =>
Value.of(f(this._content))
join = () => this._content
fmap = <B>(f: Func<A, Value<B>>): Value<B> =>
Value.of(this._content).map(f).join()
}
class Compose<A, B> {
constructor(private _func: Func<A, B>) { }
static of = <A,B>(fn: Func<A, B>): Compose<A, B> =>
new Compose(fn)
then = <C>(g: Func<B, C>): Compose<A, C> =>
Compose.of((x: A) => g(this._func(x)))
map = this.then
apply = <A>(data: Value<A>): Value<A> => data.map(this)
}
// alias
const Pipe = Compose
const a = (_: string):number => 2
const b = (_: number): number[] => [2]
const c = (_: number[]): string[] => ['a']
const d = (_: string[]): string => 'a'
const f = Value.of(a).map(num => [num]).map(c).map(d)
// ok -> type of f is Compose<string, string>
const value = f.apply(Value.of(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment