Search code examples
elm

Reduce List to longest String in the List with Elm


I'm trying to figure out how to create a function that takes a List of Strings and returns a new list that only contains the longest string in the list.

I'm having a hard time figuring out several aspects of this problem including how to keep track of the maximum and change it when there is a new longest string. Do I need to use an Effect / Task?

Example: a function that takes ["one", "two", "three"] and returns ["three"] since it is the longest string.


Solution

  • If you just want a function that gives you the longest string of a list:

    List.sortBy String.length >> List.reverse >> List.take 1