Module

Codec.Json.Unidirectional.Value.SortRowList

Package
purescript-json-codecs
Repository
jordanmartinez/purescript-json-codecs

#IntThenAlphaSortedRowList Source

class IntThenAlphaSortedRowList :: RowList Type -> RowList Type -> Constraintclass 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

For proof, see https://try.purescript.org/?code=FAWw9gJgrgNgpgAgLIEMCWA7BB3AFnAJzmGDRAAcwCAXBABSJigmNIqtoBEVqUA6ADJoAztT4AVAJ7k4whAAoho%2BXz4BKNW0o0EAUQBm%2BuAGNa8g0dOay22hZNiAwjBTDhfR2AzCw8BTDAAc2t2HXECSTooIgBlYwI0clpXBHDI6Lg4hKSSEHQsAC4CvUMHBABVDDRqUHyEAF5UiKjY%2BMSxIgwWAgaAHl6m9NbssWxq3E9vX0QIMGAEBADAhAASBGFcMGxVhBEOAHIEeYWdybl5cQQABgQAFjUjk9Ovc8uARgQAJgfjhbWzhSXT5fH5Pf4vQEIADMCAAHKCTmsAHJoGAkAC06NSCHwRF2cigwjgEAQ1DApIIKGMAGtgJjSfgEFQ0IFMCgYLsunAAB5M-QMxBweAgOAYMz6NAEUQIACSYs09JQXQFCCFcBFYt21CJMH58iJxi8JLl1E0EB4KGxjUuJtlYtI3l4GGMiF0AEdsXhCIKPY1jK5EAB9AA0CEDfOx4eEocuUeB6IAfOsGo1hJ8HaIlS6EAB5Agky5evGGigoPF%2BgNh0PhsD82PrGNh9bxpMl8hlxByNMZp3ZmKbbaF3GdgeQtC7b4NBAAIguM4QvSTGy2uwXSen88X61HaGBW%2BnamnJD2NEOjRPZgAOnPwwA3dlQOAPRMIe9MJ-Hnw6IoIfRUdkcvwCiWi%2BJrPkmSjJAgL6QQgKCkF%2BtD8o0%2BgwIkCCBOSKJogsRbEAsmEIBgPLJMYxhTv6RJESRoZUuRta-Ag2HVtBSZ0YxAK4KSobYaxGHkrQ4LeDicFkRxEK4B81BvKGnHAtQ6ZPAsAA%2Bv44h8Az8rgLb8aSHxCXIkmicYjEqUy1C4mMVEvnJOyEcR3K0ApJBAA

Instances

#ToOrdering Source

class ToOrdering :: forall k. k -> Int -> Constraintclass ToOrdering value order | value -> order

#InsertionSort Source

class InsertionSort :: RowList Type -> RowList Type -> RowList Type -> Constraintclass InsertionSort unsorted acc sorted | unsorted acc -> sorted

Instances

#InsertRow Source

class InsertRow :: Ordering -> Symbol -> Type -> RowList Type -> Symbol -> Type -> RowList Type -> RowList Type -> Constraintclass 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