Details

    • Type: Bug
    • Status: CLOSED
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.11.8
    • Fix Version/s: Scala 2.12.0-M5
    • Component/s: Library (Misc)
    • Labels:
    • Environment:

      Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz (max 3.50GHz), RAM 12Gb DDR3-1333, Ubuntu 14.04.1, Linux 3.16.0-60-generic, Oracle JDK build 1.8.0_74-b02 64-bit

      Description

      I get following error when run a code below:

      Exception in thread "main" scala.MatchError: (null,null) (of class scala.Tuple2)
      	at HashMapError$.HashMapError$$$anonfun$2(HashMapError.scala:13)
      	at HashMapError$lambda$$merge2$1.apply(HashMapError.scala:13)
      	at HashMapError$lambda$$merge2$1.apply(HashMapError.scala:13)
      	at scala.collection.immutable.HashMap$$anon$2$$anon$3.apply(HashMap.scala:150)
      	at scala.collection.immutable.HashMap$HashMap1.updated0(HashMap.scala:200)
      	at scala.collection.immutable.HashMap$HashMap1.merge0(HashMap.scala:225)
      	at scala.collection.immutable.HashMap$HashTrieMap.merge0(HashMap.scala:488)
      	at scala.collection.immutable.HashMap$HashTrieMap.merge0(HashMap.scala:488)
      	at scala.collection.immutable.HashMap$HashTrieMap.merge0(HashMap.scala:488)
      	at scala.collection.immutable.HashMap.merged(HashMap.scala:117)
      	at HashMapError$.merge2(HashMapError.scala:13)
              ...
      

      import scala.collection.immutable.HashMap
       
      object HashMapError extends App {
        def merge[K, V](m1: HashMap[K, Seq[V]], m2: HashMap[K, Seq[V]]): HashMap[K, Seq[V]] =
          m2.foldLeft(m1) { case (m, (k, v2)) =>
            m.updated(k, m.get(k) match {
              case Some(v1) => v1 ++ v2
              case None => v2
            })
          }
       
        def merge2[K, V](m1: HashMap[K, Seq[V]], m2: HashMap[K, Seq[V]]): HashMap[K, Seq[V]] =
          m1.merged(m2) {
            case ((k, v1), (_, v2)) => (k, v1 ++ v2)
          }
       
        def diff[K, V](m1: HashMap[K, Seq[V]], m2: HashMap[K, Seq[V]]): HashMap[K, Seq[V]] =
          m2.foldLeft(m1) { case (m, (k, v2)) =>
            m.get(k) match {
              case Some(v1) =>
                val v = v1.diff(v2)
                if (v.isEmpty) m - k
                else m.updated(k, v)
              case None => m
            }
          }
       
        def create(first: Int, limit: Int): HashMap[String, Seq[Int]] = {
          var m = HashMap[String, Seq[Int]]()
          (first to limit).foreach { i =>
            m = m.updated(i.toString, (0 to i).toSeq)
          }
          m
        }
       
        var m1 = create(1, 10000)
        val m2 = create(1, 1000)
        (1 to 100).foreach { _ =>
          m1 = merge2(m1, m2)
          m1 = diff(m1, m2)
        }
      }
      

      But it works fine if instead of merge2 I call merge function.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                plokhotnyuk Andriy Plokhotnyuk
              • Votes:
                3 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: