Created
September 20, 2015 13:29
-
-
Save puffnfresh/c98358d6f513c31a0ef4 to your computer and use it in GitHub Desktop.
Heterogeneous records in PureScript using existential row variables
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 Hetero | |
exports.mkExists = function(fa) { | |
return fa; | |
}; | |
exports.runExists = function(f) { | |
return function(fa) { | |
return f(fa); | |
}; | |
}; |
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 Hetero where | |
import Prelude | |
import Control.Monad.Eff | |
import Control.Monad.Eff.Console | |
foreign import data Exists :: (# * -> *) -> * | |
foreign import mkExists :: forall f a. f a -> Exists f | |
foreign import runExists :: forall f r. (forall a. f a -> r) -> Exists f -> r | |
newtype HasNameF r = HasNameF { name :: String | r } | |
type HasName = Exists HasNameF | |
hasName :: forall r. { name :: String | r } -> HasName | |
hasName o = mkExists (HasNameF o) | |
brian :: HasName | |
brian = hasName { name: "Brian", age: 25 } | |
josh :: HasName | |
josh = hasName { name: "Josh", legs: 4 } | |
things :: Array HasName | |
things = [ brian, josh ] | |
names :: Array String | |
names = runExists (\(HasNameF o) -> o.name) <$> things | |
main :: Eff (console :: CONSOLE) Unit | |
main = print names |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment