Hacker News new | ask | show | jobs
by chriswarbo 3506 days ago
In cases where we're "acting on" some value, like a dictionary, surely we should be able to store the comparison function in the value? I don't know Elm, but something like the following Haskell:

    data Dict k v = D (k -> k -> Bool) [(k, v)]

    empty :: (k -> k -> Bool) -> Dict k v
    empty f = D f []

    -- and so on
Of course, this is a naive linear-time implementation; substitute with whatever alternative you like. If you already have some type-level "comparable" thingy, you can make an "emptyFromComparable" to pre-populate with the built-in compare function.

We could do the same for things like Set too.

As far as I can see, the only 'problem' would be operations like `merge`, where we might have two different functions. The simplest thing would be to bias in favour of one, e.g. turning `merge d1 d2` into something like `extendWith d1 d2`.

I'm not saying this can replace typeclasses or modules in general, but in these sorts of cases we're already passing around a datastructure, so why bother passing around the required functions separately?

There's probably some irony here, considering the fact that GHC implements typeclasses using dictionary passing!

1 comments

http://package.elm-lang.org/packages/eeue56/elm-all-dict/2.0... is an example of a Dict where you pass in a custom (key -> comparable) function when initializing the datastructure.