Created
February 12, 2018 11:34
-
-
Save soareschen/6d1409305281c2b432e289b5093ace93 to your computer and use it in GitHub Desktop.
Composition of row polymorphic functions in PureScript
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
module Main where | |
import Prelude | |
import Data.Record | |
import Control.Monad.Eff (Eff) | |
import Control.Monad.Eff.Console (CONSOLE, log) | |
fooHandler :: forall r. { foo :: String | r } -> String | |
fooHandler args = "(foo-handler " <> args.foo <> ")" | |
barHandler :: forall r. { bar :: String | r } -> String | |
barHandler args = "(bar-handler " <> args.bar <> ")" | |
composeHandlers :: forall a. (a -> String) -> (a -> String) -> a -> String | |
composeHandlers f g x = "(compose-handler " <> (f x) <> " " <> (g x) <> ")" | |
-- composedHandler :: forall r. { foo :: String, bar :: String } -> String | |
composedHandler = composeHandlers fooHandler barHandler | |
-- injectBar :: forall r. { | r } -> { bar :: String | r } | |
injectBar record = unionMerge record { bar: "injectedBar" } | |
-- extendedHandler :: forall r. { foo:: String | r } -> String | |
extendedHandler = map composedHandler injectBar | |
args = { foo: "fooValue", baz: "bazValue" } | |
-- result :: String | |
result = extendedHandler args | |
main :: forall e. Eff (console :: CONSOLE | e) Unit | |
main = do | |
log $ result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment