Skip to content

Instantly share code, notes, and snippets.

@justinwoo
Last active April 22, 2018 15:02
Show Gist options
  • Save justinwoo/998e04307bd4179013b3ad222c5cb8e8 to your computer and use it in GitHub Desktop.
Save justinwoo/998e04307bd4179013b3ad222c5cb8e8 to your computer and use it in GitHub Desktop.
extract field names from rows using rowtolist
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Type.Proxy (Proxy(..))
import Type.Row (class ListToRow, class RowToList, Cons, Nil, kind RowList)
class RollMySausage (rowList :: RowList) where
rollMySausage :: forall row
. RowToList row rowList
=> Proxy (Record row)
-> Array String
instance rmsCons ::
( IsSymbol name
, RollMySausage tail
, ListToRow tail tailRow
, RowToList tailRow tail
) => RollMySausage (Cons name ty tail) where
rollMySausage _ = [name] <> rest
where
name = reflectSymbol (SProxy :: SProxy name)
rest = rollMySausage (Proxy :: Proxy (Record tailRow))
instance rmsNil :: RollMySausage Nil where
rollMySausage _ = []
type MyTest =
{ a :: Int
, b :: String
, c :: Boolean
}
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
logShow $ rollMySausage (Proxy :: Proxy MyTest)
-- outputs ["a","b","c"]
@paluh
Copy link

paluh commented Nov 10, 2017

Is it possible to preserve order of fields?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment