Module
Codec.Json.Unidirectional.Value.SortRowList
- Package
- purescript-json-codecs
- Repository
- jordanmartinez/purescript-json-codecs
#IntThenAlphaSortedRowList Source
class IntThenAlphaSortedRowList :: RowList Type -> RowList Type -> Constraint
class IntThenAlphaSortedRowList alphaSortedRows intThenAlphaSortedRows | alphaSortedRows -> intThenAlphaSortedRows
The combination of SortRows
, InsertionSort
, and InsertRow
implement this algorithm
to sort the list or FromProp
by insertion order and then by the label's alphabetical ordering.
Since RowToList
returns a list of rows in alphabetical ordering, we sort by the label's alphabetical
ordering by maintaining the original position
isort :: forall a. (a -> Int) -> List a -> List a
isort f = flip go Nil
where
go next acc = case next, acc of
Nil, _ -> acc
Cons h t, Nil -> go t $ Cons h acc
Cons h1 t1, Cons h2 t2
| f h < f h2 = go t2 $ Cons h1 acc
| otherwise = Cons h2 $ go next t2
Instances
(InsertionSort unsorted Nil sorted) => IntThenAlphaSortedRowList unsorted sorted
#ToOrdering Source
class ToOrdering :: forall k. k -> Int -> Constraint
class ToOrdering value order | value -> order
#InsertionSort Source
class InsertionSort :: RowList Type -> RowList Type -> RowList Type -> Constraint
class InsertionSort unsorted acc sorted | unsorted acc -> sorted
Instances
InsertionSort Nil acc acc
(InsertionSort tail (Cons sym a Nil) sorted) => InsertionSort (Cons sym a tail) Nil sorted
(ToOrdering a1 insertionOrder1, ToOrdering a2 insertionOrder2, Compare insertionOrder1 insertionOrder1 res, InsertRow res sym1 a1 tail1 sym2 a2 tail2 sorted) => InsertionSort (Cons sym1 a1 tail1) (Cons sym2 a2 tail2) sorted
#InsertRow Source
class InsertRow :: Ordering -> Symbol -> Type -> RowList Type -> Symbol -> Type -> RowList Type -> RowList Type -> Constraint
class InsertRow ord nextSym nextA nextTail accSym accA accTail newAcc | ord nextSym nextA nextTail accSym accA accTail -> newAcc
Instances
(InsertionSort nextTail (Cons nextSym nextA (Cons accSym accA accTail)) sorted) => InsertRow LT nextSym nextA nextTail accSym accA accTail sorted
h1 < h2 -> go t1 $ Cons h1 acc ...
(InsertionSort (Cons nextSym nextA nextTail) accTail sorted) => InsertRow EQ nextSym nextA nextTail accSym accA accTail (Cons accSym accA sorted)
.. otherwise -> Cons h2 $ go next t2
(InsertionSort (Cons nextSym nextA nextTail) accTail sorted) => InsertRow GT nextSym nextA nextTail accSym accA accTail (Cons accSym accA sorted)
.. otherwise -> Cons h2 $ go next t2
h1 < h2 -> go t1 $ Cons h1 acc otherwise -> Cons h2 $ go next t2