acowley / Frames

Data frames for tabular data.
Other
297 stars 41 forks source link

Get the field names #184

Open idontgetoutmuch opened 10 months ago

idontgetoutmuch commented 10 months ago

This seems useful to me. We could add it to StripFieldNames but then the name doesn't indicate that you can also get the names as well as strip them.

class GetFieldNames (ts :: [(Symbol, Type)]) where
  getNames :: Rec ElField ts -> [String]

instance GetFieldNames '[] where
  getNames _ = []

instance (KnownSymbol s, GetFieldNames ts) => GetFieldNames ('(s, t) ': ts)  where
  getNames _ = symbolVal (Proxy :: Proxy s) : getNames (undefined :: Rec ElField ts)
acowley commented 10 months ago

I think this almost exists in Vinyl. If we had,

rlabels' :: forall f fs. AllFields fs => Rec f fs -> Rec (Const String) fs 
rlabels' _ = rlabels @fs

Then you can do,

ghci> recordToList . rlabels' $ ((#foo =: (23::Int)) :& (#bar =: "hi there") :& RNil)
["foo","bar"]

With extensions FlexibleContexts, TypeApplications, ScopedTypeVariables, and OverloadedLabels.