SortByTupleWithEmptyListTest
`List.sortBy` with a tuple key whose second component is sometimes `[]` orders by the first component, then by the list. `keyOf n` is `( modBy 2 n, suffix )`, where `suffix` is `[]` when `n > 2` and `[ n ]` otherwise. For `[ 4, 1, 2, 3 ]` the keys are `(0, [])`, `(1, [1])`, `(0, [2])` and `(1, [])`. Tuples compare component by component and `[]` is less than any non-empty list, so the sorted keys are `(0, [])`, `(0, [2])`, `(1, [])`, `(1, [1])`, giving `[4,2,3,1]`.
Results
elm-0.19.1
passran in 54 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. result: [4,2,3,1]
elm-0.19.2
passran in 51 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. result: [4,2,3,1]
elm-0.19.3
passran in 65 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. result: [4,2,3,1]
lamdera-1.5.0
passran in 35 msOutput
result: [4,2,3,1]
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-core/SortByTupleWithEmptyListTest] in its title.
Source
The -- CHECK: lines give the expected output.
module SortByTupleWithEmptyListTest exposing (main)
{-| `List.sortBy` with a tuple key whose second component is sometimes `[]` orders by the first component, then by the list.
`keyOf n` is `( modBy 2 n, suffix )`, where `suffix` is `[]` when `n > 2` and
`[ n ]` otherwise. For `[ 4, 1, 2, 3 ]` the keys are `(0, [])`, `(1, [1])`,
`(0, [2])` and `(1, [])`. Tuples compare component by component and `[]` is less
than any non-empty list, so the sorted keys are `(0, [])`, `(0, [2])`, `(1, [])`,
`(1, [1])`, giving `[4,2,3,1]`.
-}
-- CHECK: result: [4,2,3,1]
import Html exposing (text)
keyOf : Int -> ( Int, List Int )
keyOf n =
let
suffix =
if n > 2 then
[]
else
[ n ]
in
( modBy 2 n, suffix )
main =
let
result : List Int
result = List.sortBy keyOf [ 4, 1, 2, 3 ]
_ = Debug.log "result" result
in
text "done"