News for hackers. Stuff that matters.
Re:Map.merge() – One method to rule them all by wcarss on Sunday March 31, 2019 @09:47AM (#19535561)
in python:

    >>> from collections import defaultdict
    >>> map = defaultdict(int)
    >>> for word in ['hi', 'there', 'hi']:
    ...   map[word] += 1
I am glad Java has this stuff now, but it's a little surprising that its tools are still so obtuse relative to competitors.

edit: I should have foreseen the derailment caused by and nitpicking to follow a comment like this. Sorry folks!

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.
Re:Map.merge() – One method to rule them all by tgb on Sunday March 31, 2019 @10:39AM (#19535817)
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.
Re:Map.merge() – One method to rule them all by mruts on Sunday March 31, 2019 @12:19PM (#19536363)
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
      }.toMap
Re:Map.merge() – One method to rule them all by mruts on Monday April 01, 2019 @02:22PM (#19546421)
That 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.
Re:Map.merge() – One method to rule them all by detaro on Sunday March 31, 2019 @10:47AM (#19535852)
OrderedDict is ordered by order of insertion, TreeMap is ordered by key value.
Re:Map.merge() – One method to rule them all by karmakaze on Sunday March 31, 2019 @11:04AM (#19535928)
Logically equivalent to LinkedHashMap, thought it was the other. I should use Python more often.