Elm Exactly
Results / elm-core /

SortByTupleWithEmptyStringTest

elm-0.19.1 passelm-0.19.2 passelm-0.19.3 passlamdera-1.5.0 pass
`List.sortBy` with a tuple key whose second component is sometimes `""` orders by the first component, then by the string.

`keyOf n` is `( modBy 2 n, suffix )`, where `suffix` is `""` when `n > 2` and
`"x"` otherwise. For `[ 4, 1, 2, 3 ]` the keys are `(0, "")`, `(1, "x")`,
`(0, "x")` and `(1, "")`. Tuples compare component by component and `""` is less
than `"x"`, so the sorted keys are `(0, "")`, `(0, "x")`, `(1, "")`, `(1, "x")`,
giving `[4,2,3,1]`.

Results

elm-0.19.1

passran in 48 ms
Output
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 48 ms
Output
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 59 ms
Output
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 45 ms
Output
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/SortByTupleWithEmptyStringTest] in its title.

Source

The -- CHECK: lines give the expected output.

module SortByTupleWithEmptyStringTest exposing (main)
{-| `List.sortBy` with a tuple key whose second component is sometimes `""` orders by the first component, then by the string.
`keyOf n` is `( modBy 2 n, suffix )`, where `suffix` is `""` when `n > 2` and
`"x"` otherwise. For `[ 4, 1, 2, 3 ]` the keys are `(0, "")`, `(1, "x")`,
`(0, "x")` and `(1, "")`. Tuples compare component by component and `""` is less
than `"x"`, so the sorted keys are `(0, "")`, `(0, "x")`, `(1, "")`, `(1, "x")`,
giving `[4,2,3,1]`.
-}
-- CHECK: result: [4,2,3,1]
import Html exposing (text)
keyOf : Int -> ( Int, String )
keyOf n =
let
suffix =
if n > 2 then
""
else
"x"
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"