Is there a way to filter a list of union types in Elm without explict case/pattern matching?

hwj*_*wjp 4 elm

I've got a list of things and I want to filter based on a Union type. simplified, it might be something like this:

type Groceries =
  Apples
  | Cheese
  | Widgets

shoppingList = [Apples, Cheese, Cheese, Widgets, Apples]
Run Code Online (Sandbox Code Playgroud)

is there a nice syntax for filtering all the elements that match a particular subtype?

# idk, eg
fruit = shoppingList |> List.filter =Apple? 
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用带有语句的 lambda case,但它看起来太冗长了!

gle*_*nsl 8

你可以做:

fruit = shoppingList |> List.filter ((==) Apples)
Run Code Online (Sandbox Code Playgroud)

(==)采用中缀等于运算符并将其视为普通函数。然后,应用Apples到它会返回一个部分应用的函数,该函数将应用到它的任何内容与 进行比较Apples

然而,这仅适用于可以直接比较的简单变体。