Skip to content

Instantly share code, notes, and snippets.

@nsaunders
Created August 27, 2019 14:32
Show Gist options
  • Save nsaunders/4700e45b92cc285b146f898fd60b8739 to your computer and use it in GitHub Desktop.
Save nsaunders/4700e45b92cc285b146f898fd60b8739 to your computer and use it in GitHub Desktop.
Keys (labels) from a row in PureScript
module Keys where
import Prelude (Unit, mempty, ($))
import Data.List.Types (List, (:))
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Effect (Effect)
import Effect.Console (logShow)
import Type.Data.Row (RProxy(..))
import Type.RowList (class RowToList, Cons, Nil, RLProxy(..), kind RowList)
class Keys (row :: # Type) where
keys :: forall proxy. proxy row -> List String
instance keysImpl ::
( RowToList row rl
, KeysRowList rl
) => Keys row where
keys _ = keysRowList (RLProxy :: RLProxy rl)
class KeysRowList (rl :: RowList) where
keysRowList :: forall proxy. proxy rl -> List String
instance keysRowListCons ::
( IsSymbol key
, KeysRowList rlt
) => KeysRowList (Cons key v rlt) where
keysRowList _ = reflectSymbol (SProxy :: SProxy key) : keysRowList (RLProxy :: RLProxy rlt)
instance keysRowListNil :: KeysRowList Nil where
keysRowList _ = mempty
main :: Effect Unit
main = logShow $ keys (RProxy :: RProxy (id :: Int, name :: String, age :: Int))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment