Last active
July 20, 2023 00:02
-
-
Save natefaubion/edd2508acc4d91b9710721e67631a980 to your computer and use it in GitHub Desktop.
Record currying
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.Symbol (class IsSymbol) | |
import Prim.Row as Row | |
import Prim.RowList as RowList | |
import Record as Record | |
import Type.Proxy | |
class Curry a b c z | a b c -> z where | |
curry :: (a -> b) -> c -> z | |
class WrapRecord rl a b | rl a -> b where | |
wrapRecord :: Proxy rl -> a -> b | |
instance | |
( IsSymbol sym | |
, Row.Cons sym a () r | |
) => | |
WrapRecord (RowList.Cons sym a RowList.Nil) a { | r } where | |
wrapRecord _ a = Record.insert (Proxy :: _ sym) a {} | |
else instance | |
WrapRecord (RowList.Cons sym a tail) { | r } { | r } where | |
wrapRecord _ = identity | |
instance Curry { | r } b { | r } b where | |
curry = identity | |
else instance | |
( Row.Union s x r | |
, Row.Nub r r | |
) => | |
Curry { | r } b { | s } ({ | x } -> b) where | |
curry k a = \b -> k (Record.disjointUnion a b) | |
curry' | |
:: forall r rl a b x z | |
. RowList.RowToList r rl | |
=> WrapRecord rl a x | |
=> Curry { | r } b x z | |
=> ({ | r } -> b) | |
-> a | |
-> z | |
curry' f a = curry f ((wrapRecord (Proxy :: _ rl) a)) | |
infixl 0 curry' as ~ | |
replace :: { needle :: String, replacement :: String, haystack :: String } -> String | |
replace _ = "" | |
test1 = replace ~ { needle: "_", replacement: "-" } | |
test2 = test1 ~ "ok" | |
test3 = test1 { haystack: "ok" } | |
test4 = replace ~ { needle: "_" } ~ { replacement: "-" } ~ "ok" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment