Created
August 29, 2021 12:35
-
-
Save davidchase/9ab3cea4a6a36008db61b2dae485cd50 to your computer and use it in GitHub Desktop.
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 Reader exposing | |
( return | |
, ask | |
, runReader | |
, local | |
, fmap | |
, chain | |
) | |
type Reader r a = Reader (r -> a) | |
id x = x | |
constant a = \_ -> a | |
return a = Reader (constant a) | |
ask = Reader id | |
runReader e reader = | |
case reader of | |
Reader r -> r(e) | |
local f reader = | |
Reader (\c -> runReader f(c) reader) | |
fmap f reader = | |
Reader (\c -> f(runReader c reader)) | |
chain f reader = | |
Reader (\c -> runReader c (f(runReader c reader))) | |
-- fun to derive | |
map f reader = | |
chain (\a -> pointed (f(a))) reader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment