Re:Map.merge() – One method to rule them all
by meddlepal on Sunday March 31, 2019 @11:23AM
(#19536041)
Yes... now turn that map into a lock free thread-safe dict in Python and show us the same code. I get that for free in Java via ConcurrentHashMap.
Re:Map.merge() – One method to rule them all
by ben-schaaf on Sunday March 31, 2019 @11:27AM
(#19536061)
If you mean lock-free as in no GIL, you obviously can't. But otherwise all python code is inherently thread safe.
Re:Map.merge() – One method to rule them all
by meddlepal on Sunday March 31, 2019 @11:41AM
(#19536125)
Yea I meant lock free and ready for multi-threaded use without a GIL. I didn't mean to dump on Python either, I write a lot of Python and Java but comparisons like yours aren't really fair because the JVM is just considerably more powerful than Python's runtime. I could take a huge list of words, hand subsets to threads and let them all update the same map using the same API and without needing a lock.
You'd preferably use collections.Counter for this.
Re:Map.merge() – One method to rule them all
by kbsletten on Sunday March 31, 2019 @10:27AM
(#19535734)
To be fair, getValueOrDefault is strictly more powerful than defaultdict because you can vary the initial value by the key.
Re:Map.merge() – One method to rule them all
by detaro on Sunday March 31, 2019 @10:42AM
(#19535830)
dict.get() does the same in Python (optional second parameter for a default value). But I've wondered too why there isn't a form of the default dict that can take parameters like the key for the factory function.
But your python code is still really ugly. In Scala:
import scala.collection.mutable.Map
> val map = Map.empty[String, Int]
> List("hi", "there", "hi").map { x =>
> map(x) = i
> }
Re:Map.merge() – One method to rule them all
by virtualwhys on Sunday March 31, 2019 @12:41PM
(#19536517)
Not the same but immutable approach is more idiomatic Scala.
List("hi", "there", "hi")
.zipWithIndex
.map { case (x, i) =>
x -> i
}.toMapThat example doesn’t really illustrate the point of updating maps with repeated values.
Re:Map.merge() – One method to rule them all
by nayuki on Sunday March 31, 2019 @10:32AM
(#19535766)
Funny you say that, because I find the key-sorted TreeMap to be useful in Java, but Python seems to have no equivalent.
Re:Map.merge() – One method to rule them all
by karmakaze on Sunday March 31, 2019 @10:41AM
(#19535822)
Python has OrderedDict.
edit: I should have foreseen the derailment caused by and nitpicking to follow a comment like this. Sorry folks!