Open stla opened 1 year ago
I don't think that a getWithDefault()
is necessary, since the get()
method already has a missing
parameter.
The map()
method is interesting. In the rough version you've shown, it modifies the items in the fastmap in place, which seems a bit strange to me. Is that what the Haskell version does? (I'm not familiar with Haskell syntax.) I'd expect it to return a new object.
I could imagine some new methods being useful. There are things I like from purrr (but adapted for maps), the JavaScript lodash library, and Python dicts. For example:
map_values
map_keys
map_keys_values
filter_values
filter_keys
filter_keys_values
to_pairs
from_pairs
update
: merge in values from another fastmap objectI don't currently have time to sit down and think rigorously about which methods would be useful. If you have real-world use cases in mind, that would be helpful.
No, Haskell does not modify in place.
I'm currently trying to translate a Haskell code to R which involves a map from the integers (encoded as characters with R) to the rational numbers (using gmp in R). Such a map is used to represent a cyclotomic number. This is my "real-world" use case :-) I've just started so far but I needed map
and filter
.
There's also union
and unionWith
in Haskell. The unionWith
function takes two maps and a binary function, it returns the map obtained by taking the union of the two input maps and by applying the input function to the two values corresponding to a common key.
unionWith <- function(f, mp1, mp2) {
keys1 <- mp1$keys()
keys2 <- mp2$keys()
commonKeys <- intersect(keys1, keys2)
mp <- fastmap()
for(k in commonKeys) {
mp$set(k, f(mp1$get(k), mp2$get(k)))
}
for(k in setdiff(keys1, commonKeys)) {
mp$set(k, mp1$get(k))
}
for(k in setdiff(keys2, commonKeys)) {
mp$set(k, mp2$get(k))
}
mp
}
Also insertWith
is very useful:
insertWith <- function(f, map, key, value) {
if(map$has(key)) {
map$set(key, f(value, map$get(key)))
} else {
map$set(key, value)
}
invisible(NULL)
}
Hello,
Haskell has very nice functions for hash maps.
For example:
And many interesting other ones. Do you intend to include such functions in the package? I can help if you want.