Created
October 23, 2019 17:17
-
-
Save Willmo36/c771a0788162717576cfe25ed3df16fd to your computer and use it in GitHub Desktop.
Restricted Monad experiment
This file contains hidden or 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
import React from "react" | |
type Level = "read" | "read_write" | |
type Restricted<A, B> = (l: Level) => (a: A) => B | |
function restricted<A, B>(required: Level, whenAuth: (a: A) => B, whenNotAuth: () => B) { | |
return (level: Level) => (a: A) => required === level | |
? whenAuth(a) | |
: whenNotAuth(); | |
} | |
function map<A, B, C>(fa: Restricted<A, B>, fn: (b: B) => C): Restricted<A, C> { | |
return (level: Level) => (a: A) => fn(fa(level)(a)) | |
} | |
//readonly chain: <R, A, B>(ma: ReaderT1<M, R, A>, f: (a: A) => ReaderT1<M, R, B>) => ReaderT1<M, R, B> | |
/** | |
* instance Monad ((->) r) where | |
f >>= k = \ r -> k (f r) r | |
f = the function (r -> _) | |
k = A -> (r -> _) | |
Prelude> let bar = (const "max") >>= \r -> (words . (++r)) | |
Prelude> bar "david" | |
["davidmax"] | |
*/ | |
function chain<A, B, C, D>(fa: Restricted<A, B>, fn: (b: B) => Restricted<B,D>): Restricted<A, D> { | |
return (level: Level) => (a: A) => { | |
const b = fa(level)(a); | |
const fb = fn(b); | |
return fb(level)(b); | |
} | |
} | |
const restrictedRoute = restricted<string, JSX.Element>( | |
"read_write", | |
id => <AdminPanel />, | |
() => <401 />, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment