Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Created September 20, 2015 13:29
Show Gist options
  • Save puffnfresh/c98358d6f513c31a0ef4 to your computer and use it in GitHub Desktop.
Save puffnfresh/c98358d6f513c31a0ef4 to your computer and use it in GitHub Desktop.
Heterogeneous records in PureScript using existential row variables
// module Hetero
exports.mkExists = function(fa) {
return fa;
};
exports.runExists = function(f) {
return function(fa) {
return f(fa);
};
};
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