Hi!
I’ve noticed a pretty common (and a bit tedious) pattern in my tables: I have an array of records, and I want to be able to sort them by each field (without any kind of async server interaction).
Right now I’m doing this in a very clumsy way, by defining a sum type SortBy, and then using it to to build the table header, as an argument in the sort function, etc., but I’d like to be able to derive most of this stuff from types.
I got somewhere, but I need some directions, or maybe just to know if I’m on the right path and if this is possible.
This is what I have right now:
data SortOrder = Asc | Desc
derive instance eqSortOrder :: Eq SortOrder
flipSort :: SortOrder -> SortOrder
flipSort Asc = Desc
flipSort Desc = Asc
type SortBy sym = SProxy sym
data ArraySort a = ArraySort (SortBy a) SortOrder
sortResults ::
forall sym a r' r.
IsSymbol sym =>
Row.Cons sym a r' r =>
Ord a =>
ArraySort sym ->
Array { | r } ->
Array { | r }
sortResults (ArraySort sB sO) elems =
let res = sortWith (Record.get sB) elems
in case sO of
Asc -> res
Desc -> reverse res
data BuildM fn = BuildM fn
instance buildM_ ::
(Monoid m, IsSymbol sym) =>
FoldingWithIndex (BuildM (a -> m)) (SProxy sym) m a m where
foldingWithIndex (BuildM fn) prop arr a = fn a <> arr
buildM ::
forall a m r.
Monoid m =>
HFoldlWithIndex (BuildM (a -> m)) m { | r } m =>
(a -> m) ->
{ | r } ->
m
buildM fn r = hfoldlWithIndex (BuildM fn) mempty r
I have a function to sort my array given a SProxy and a tentative function that when complete will build Halogen HTML.
The problem is that this function needs to have access to the SProxy too, both to render the label and to call sortResults with the correct value.
If I change (a -> m) to (SProxy sym -> a -> m) it works, but I’m only allowed to have one label in my record obviously 
What can I do? Thanks!