monadologia v1.1.5
monadologia
A Functional Programming helper library
install
npm i monadologia or <script src="dist/monadologia"/>
execute example
clone repository cd typescript_example npm i npm run start done!
use
import {pipe, compose, either, maybe.. etx} from 'monadologia' import * as monadologia from 'monadologia'
or window.monadologia.pipe window.monadologia.either..
> ## api
compose(value) or monadologia.compose(value)
example function a(v: any){ console.log("call a func") return v; } function b(v: any){ console.log("call b func") return v v } function c(v: any){ console.log("call c func") return v v } const composed = monadologia.compose(a, b, c); console.log("COMPOSED : " + composed(2));
pipe(value) or monadologia.pipe(value)
example function a(v: any){ console.log("call a func") return v; } function b(v: any){ console.log("call b func") return v v } function c(v: any){ console.log("call c func") return v v } const p = monadologia.pipe(a, b, c)(2); console.log("PIPE : " + p);
curry(value1)(value2).. or monadologia.curry(value1)(value2)..
example const curried = monadologia.curry(function(a: any, b: any, c: any){ console.log("CURRIED : " + a, b, c) }) curried(1)(2)(3); curried(1, 2)(3); curried(1)(2, 3);
go() or monadologia.go()
example function a(v: any){ console.log("call a func") return v; } function b(v: any){ console.log("call b func") return v v } function c(v: any){ console.log("call c func") return v v } const p = monadologia.go(2, a, b, c); console.log("GO : " + p);
interface Maybe { isNothing(): boolean; map(f: (v: T)=> S): Maybe; flatten(): T | Maybe; chain(f: (v: T) => Maybe | Either): Maybe; value: T; maybeToEither(): Either }
example
monadologia.maybe(4)
.map((v: number)=> v * 4)
.chain((v: number)=> monadologia.maybe(""))
.flatten(); // Some, ""
monadologia.maybe(4)
.map((v: number)=> v * 4)
.chain((v: number)=> monadologia.maybe(""))
.map((v: string) => null)
.flatten(); // Nothing, null
interface Either { constructor(v: T): Either map(f: (v: T) => S): Either; flatten(): T| Either; chain(f: (v: T) => Either| Maybe): Either; value: T; catch(f: (v: any) => S): Either catch(f: (v: any) => any): Either eitherToMaybe(): Maybe } interface EitherFactory { right(v: T): Either; left(v: T): Either; tryCatch(f: (v: any) => T): (v: any)=> Either; }
example function callback(v: string): string { if(v === "error") throw new Error("error") return v; }
const testFunc = monadologia.either.tryCatch(callback)
const res = testFunc("test")
res.catch((d: string) => d) .map((v: string) => 10) .map((v: number) => "") .chain((v: any) => testFunc("A").catch((m: string)=> m)) .flatten() // A
res.catch((d: string) => d) .map((v: string) => 10) .map((v: number) => "") .chain((v: any) => testFunc("error").catch((m: string)=> m)) .flatten() // error
interface State<T, S>{
runState: (state: S)=> {value: T, state: S}
map(f: (value: T)=> Q): State<Q, S>;
flatten<P, Q>(): State<P, Q>;
chain<P, Q>(f: (value: T)=> State<P, Q>): State<P, Q>
evalValue(state: any): any
evalState(state: any): any
}
interface StateFactory{ <T, S>(v: T) : State<T, S>; (v: T) : State<T, any> get(): State<T, T> //muse input type(If the type is not entered, the type cannot be inferred from ide.) put(newState: P): State<undefined, P> //muse input type(If the type is not entered, the type cannot be inferred from ide.) modify(f: (state: any)=>P): State<undefined, P> //muse input type(If the type is not entered, the type cannot be inferred from ide.) gets(f: (state: any) => P): State<P, any> //muse input type(If the type is not entered, the type cannot be inferred from ide.) }
example monadologia.state(3) .chain((value: number)=> monadologia.state.put(999)) // value undefined state 999 .chain((value: undefined)=> monadologia.state.get()) // value 999 state 999 .chain((value: number)=> monadologia.state.modify((state: number)=>{ return []; })) // value undefined state [] .chain((value: undefined)=>{ return monadologia.state.gets((state: [])=> 200) }) // value 200 state [] .chain((v: number)=> monadologia.state("good")) // value 뽕 state[] .evalValue(10) // init state 10, value return
monadologia.state(3) .chain((value: number)=> monadologia.state.put(999)) // value undefined state 999 .chain((value: undefined)=> monadologia.state.get()) // value 999 state 999 .chain((value: number)=> monadologia.state.modify((state: number)=>{ return []; })) // value undefined state [] .chain((value: undefined)=>{ return monadologia.state.gets((state: [])=> 200) }) // value 200 state [] .chain((v: number)=> monadologia.state("H")) // value H state[] .evalState(10) // init state 10, state return
example monadologia.writer(4) .map((v: number)=> "hello!") .logging((v: string)=> "Inputed: " + v) .chain((v: string)=> monadologia.writer(v, "inputed: " + v)) .map((v: string)=> "Yaho@") .logging((v: string)=> "YAHO~~!")
const read = monadologia.reader<number, string>(100) .map((v: number)=>{ console.log(v); // 100 return v + 200; }) .map((v: number)=>{ console.log(v) // 300 return v + 40; })
read.chain((value: number)=>{ const newReader = monadologia.reader<number, string>(5) .ask() .map((v: string)=>{ console.log(v) // test return v; }). map((v: string)=>{ return 1 }) return newReader; }).runReader("test")
monadologia.task((err: Function, ok: Function)=>{ ok(1) }).chain((v: number)=> monadologia.task((err: Function, ok: Function)=>{ ok("") })).map((v: string)=> 100) t.fork(console.error, console.log)
check detail usecase on example project!