What are comparable types?
type FruitSort =
Apple
| Orange
| Banana
type alias Fruit =
{ sort: FruitSort
, name: String
}
{-| Fruit needs to be "comparable". What do i need to do?
-}
type Basket =
Dict Fruit Inttype FruitSort =
Apple
| Orange
| Banana
fruitSortToInt : FruitSort -> Int
fruitSortToInt fruit =
case fruit of
Apple -> 1
Orange -> 2
Banana -> 3
fruitSortFromInt : Int -> FruitSort
fruitSortFromInt int =
case int of
1 -> Apple
2 -> Orange
3 -> Banana
_ -> Apple --be careful, the compiler won't help you there.
type alias Fruit =
{ sort: FruitSort
, name: String
}
type alias Item =
(Int,String)
itemFromFruit : Fruit -> Item
itemFromFruit {sort,name} =
(sort |> fruitSortToInt,name)
itemToFruit : Item -> Fruit
itemToFruit (int,name) =
{ sort = int |> fruitSortFromInt
, name = name
}
type Basket =
Dict Item IntQuestion
Dict needs the first type to be comparable. What does that mean?
Answer
Int, Float, Char, String, Tuple comparable comparable and List comparable are comparable types.
So you need to convert Fruit into a comparable type like Tuple Int String.
Further reading
📦Package: Use any type with turboMaCk/any-dict.
📦Package: use any type with turboMaCk/any-set.
Last updated
Was this helpful?