Stream: beginners

Topic: List.sortBy


view this post on Zulip Martin Stewart (Jul 01 2024 at 10:18):

I'm looking into the possibility of making an Elm to Roc converter. One thing I noticed though was that, unless I'm missing something, it's not possible to implement Elm's List.sortBy : (a -> comparable) -> List a -> List a due to comparable not thing a thing in Roc? Is there a way to work around this?

view this post on Zulip Kilian Vounckx (Jul 01 2024 at 10:47):

For now, there is List.sortWith which takes a list and a comparison function of type a, a -> [LT, EQ, GT]. But I remember there being some discussion of adding a Compare ability or similar

view this post on Zulip Kilian Vounckx (Jul 01 2024 at 10:48):

https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/ordering.2Fsorting.20ability

view this post on Zulip Kilian Vounckx (Jul 01 2024 at 10:49):

Here is the discussion

view this post on Zulip Martin Stewart (Jul 01 2024 at 10:49):

Thanks, I'll have a look at that.

view this post on Zulip Luke Boswell (Jul 01 2024 at 10:52):

And @Jasper Woudenberg's WIP PR https://github.com/roc-lang/roc/pull/6615

view this post on Zulip Martin Stewart (Jul 01 2024 at 10:55):

Looking at the details I suspect this won't be exactly what I need. For example, if something like (String, F64) isn't automatically treated as comparable then I'm probably not going to get very far. My hope of just doing a syntax level conversion between Elm and Roc was probably too optimistic :sweat_smile:

view this post on Zulip Luke Boswell (Jul 01 2024 at 11:17):

I think in that case it would have Sortable derived

view this post on Zulip Luke Boswell (Jul 01 2024 at 11:17):

Wouldn't it just be opaque types that need to be manually implemented

view this post on Zulip Kilian Vounckx (Jul 01 2024 at 11:18):

I thought Str wouldn't get the ability because of unicode messiness?

view this post on Zulip Luke Boswell (Jul 01 2024 at 11:18):

Yeah, good point

view this post on Zulip Luke Boswell (Jul 01 2024 at 11:25):

What about instead of converting to a Roc Str type, make an opaque ElmStr and convert to that. Then give it behaviour that matches Elm.

view this post on Zulip Kilian Vounckx (Jul 01 2024 at 11:50):

That would work I think, if tuples and other non-opaque types all automatically derive it as well

view this post on Zulip Jasper Woudenberg (Jul 01 2024 at 16:43):

The plan is definitely that tuples/records/lists will be comparable to one-another!


Last updated: Jul 06 2025 at 12:14 UTC