Last active
April 22, 2018 15:02
-
-
Save justinwoo/998e04307bd4179013b3ad222c5cb8e8 to your computer and use it in GitHub Desktop.
extract field names from rows using rowtolist
This file contains 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 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"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it possible to preserve order of fields?