jle` 2017-02-06 22:45:17
BernhardPosselt: if you want to, say, add an item to the front of a list
jle` 2017-02-06 22:45:23
you don't have to make a copy of the original list
jle` 2017-02-06 22:45:31
your result can refer to the original list
BernhardPosselt 2017-02-06 22:45:33
right
BernhardPosselt 2017-02-06 22:45:55
however if i want to append something or put it inside the list
jle` 2017-02-06 22:45:57
same principle here; if you change a part of your immutable data structure, you can usually reference a lot of the original structure
jle` 2017-02-06 22:46:04
you can often share a lot
tfc 2017-02-06 22:46:23
lyxia: hm i see. in my case it's just that i get a lot of content from the internet and i like parsers more than using regex. but in regex i can say "get me /FOO([0-9]+)BAR/, group 1 of all matches"
erisco 2017-02-06 22:47:02
if you want append then you use Sequence. There are different data structures for different jobs
tfc 2017-02-06 22:47:07
lyxia: while the "/FOO..." part says "start matching at FOO, i don't care about the rest". and if this "i dont care" thing is nicely expressible in parsec, i've got what i want
tsahyt 2017-02-06 22:47:28
hmm, am I missing something or is there no simple way to transform a HashMap Foo a to HashMap Bar a?
tsahyt 2017-02-06 22:47:33
i.e. map over the keys
erisco 2017-02-06 22:48:03
:t (M.mapKeys, M.mapKeysMonotonic)
lambdabot 2017-02-06 22:48:05
Ord k2 => ((k1 -> k2) -> M.Map k1 a -> M.Map k2 a, (k4 -> k5) -> M.Map k4 a1 -> M.Map k5 a1)
tsahyt 2017-02-06 22:48:29
but mapKeys doesn't exist for HashMap
erisco 2017-02-06 22:48:38
oh, HashMap, heh :)
lyxia 2017-02-06 22:49:45
tfc: foobar = string "FOO" *> many digits <* string "BAR" is your pattern. Then you search it anywhere with a wrapper like: search foobar = try foobar <|> (anyChar *> searchFoobar)
laz 2017-02-06 22:50:16
tsahyt: you can't extract keys from hash map as it stores only hashes
tsahyt 2017-02-06 22:50:27
laz: yes you can extract them
tsahyt 2017-02-06 22:50:38
keys :: HashMap k v -> [k] exists
tfc 2017-02-06 22:51:10
lyxia ok, that looks already nicer than what i came up before.
tsahyt 2017-02-06 22:51:35
laz: it stores the keys along with the hashes for when collisions occur. that's also why the Eq constraint exists on pretty much all the functions provided.
erisco 2017-02-06 22:51:35
well you can define it with toList and fromList
tsahyt 2017-02-06 22:51:43
erisco: yeah, I think that's what I'll do
erisco 2017-02-06 22:51:48
you have to rebuild the whole hash anyways
tsahyt 2017-02-06 22:51:53
I was hoping for a nicer way though
laz 2017-02-06 22:52:01
tsahyt: oh, really
tsahyt 2017-02-06 22:52:58
laz: yeah, otherwise the structure would have some very undesireable properties. the same is also done with other hashtable implementations in pretty much any language. collisions must always be accounted for.
erisco 2017-02-06 22:53:46
meh, what is the statistical likelihood of collision?
tsahyt 2017-02-06 22:55:03
depends on the hash function used and the size of the table (talking about hash tables in general, not necessarily just HashMap)
tsahyt 2017-02-06 22:55:12
with a small table, it actually becomes very likely
tsahyt 2017-02-06 22:55:40
with HashMaps, probably not so much, but I don't know enough about the implementation details to say that with certainty
erisco 2017-02-06 22:57:40
well it is an IntMap which iirc uses the word size of the machine, or maybe it is just 32 bits
erisco 2017-02-06 22:58:31
depends on the distribution of the hash function and how full the table is
tsahyt 2017-02-06 22:58:44
erisco: afaik it's not exactly an IntMap anymore. e.g. HashMap Int has different performance characteristics from IntMap, even though hashing an Int is just id
tsahyt 2017-02-06 22:59:08
in my benchmarks, HashMap Int was faster for lookups, yet slower for insertions than IntMap
erisco 2017-02-06 23:02:25
actually I think the size is relevant because of the effects modulo will have
erisco 2017-02-06 23:03:48
also you can consider the data you are inserting
erisco 2017-02-06 23:04:06
are they going to be near-valued or not
erisco 2017-02-06 23:04:25
an overly complicated subject the whole thing is
tsahyt 2017-02-06 23:04:37
it depends both on hash size and how the data you're dealing with is going to be inserted. for Int there shouldn't ever be collisions, as hashes are also just Ints
erisco 2017-02-06 23:04:58
the "perfect hash"
tsahyt 2017-02-06 23:06:06
erisco: yes, because both the domain and codomain of the hashing function have the same size in that case.
tsahyt 2017-02-06 23:06:16
so the hash function can be a bijection
tsahyt 2017-02-06 23:07:01
but most of the time, you're mapping an infinite space onto a finite space, e.g. Text -> Int, so collisions must necessarily exist.
tsahyt 2017-02-06 23:07:23
theoretically infinite that is, unless someone discovers a way to have infinite memory.
cocreature 2017-02-06 23:07:24
an identity hash on ints seems like a great idea until you try using it for hyperloglog
tsahyt 2017-02-06 23:07:52
cocreature: I'm not that familiar with hyperloglog. why would that cause problems there?
erisco 2017-02-06 23:08:26
you can have infinite memory given infinite time :)
erisco 2017-02-06 23:09:24
though, um, I don't know about random access
cocreature 2017-02-06 23:09:57
tsahyt: in a lot of applications your ints are always in some specific range meaning that the top bits are more or less equal. hyperloglog uses these to index into an array. this will often mean that you only use a single element of your array
tsahyt 2017-02-06 23:10:21
is there a more general way to do unionWith? I'm looking for (v -> v -> a) -> HashMap k v -> HashMap k v -> HashMap k a basically
opqdonut 2017-02-06 23:10:41
what do you do with keys that are only in one map?
opqdonut 2017-02-06 23:10:50
you need a (v -> a) for those, right?
tsahyt 2017-02-06 23:11:00
hmm, good point. yeah I'd need that too
tsahyt 2017-02-06 23:11:11
acutally I'd want two of these, because I want to handle them differently depending on what map they came from
opqdonut 2017-02-06 23:12:19
you can use intersectionWith to handle the elements that are in both
opqdonut 2017-02-06 23:12:39
it's (a -> b -> c) -> Map k a -> Map k b -> Map k c
tsahyt 2017-02-06 23:13:35
Data.Map has this mergeWithKey function that is fully general. Of course, HashMaps don't have that either...
opqdonut 2017-02-06 23:14:06
oh right you're on hashmaps
tsahyt 2017-02-06 23:14:35
since the second map is derived from the first, I might as well just write it all as a fold at that point
tsahyt 2017-02-06 23:40:39
@hoogle Bifunctor f => (a -> b) -> f a a -> f b b
lambdabot 2017-02-06 23:40:42
Data.Bifunctor first :: Bifunctor p => (a -> b) -> p a c -> p b c
lambdabot 2017-02-06 23:40:42
Bifunctor first :: Bifunctor p => (a -> b) -> p a c -> p b c
lambdabot 2017-02-06 23:40:42
Data.Bifunctor second :: Bifunctor p => (b -> c) -> p a b -> p a c