scala collections : map a list and carry some state?

后端 未结 2 806
广开言路
广开言路 2021-01-14 17:52

I seem to run into this problem all the time. I want to modify some of the elements in a list, but I need to keep some state as I do it, so map doesn\'t work.

Here i

相关标签:
2条回答
  • 2021-01-14 18:29

    The tricky part is that the "d" and "f" elements get no modification.

    This is what I came up with. It's a bit more concise, code wise, but does involve multiple traversals.

    val l1: List[String] = List("a","b","c","d","e","f","b","c","e","b","a")
    
    l1.reverse.tails.foldLeft(List[String]()){
      case (res, Nil) => res
      case (res, hd::tl) =>
        val count = tl.count(_ == hd)
        if (count > 0) s"$hd${count+1}" +: res
        else if (res.contains(hd+2)) (hd+1) +: res
        else hd +: res
    }
    //res0: List[String] = List(a1, b1, c1, d, e1, f, b2, c2, e2, b3, a2)
    

    By using tails, each element, hd, is able to see the future, tl, and the past, res.

    0 讨论(0)
  • 2021-01-14 18:29

    A simple but slow version

    l1.zipWithIndex.map{ case (elem, i) =>
      if (l1.count(_ == elem) == 1) {
        elem
      } else {
        val n = {l1.take(i+1).count(_ == elem)}
        s"$elem$n"
      }
    }
    

    The next version is longer, less pretty, and not functional, but should be faster in the unlikely case that you are processing a very long list.

    def makeUniq(in: Seq[String]): Seq[String] = {
      // Count occurrence of each element
      val m = mutable.Map.empty[String, Int]
    
      for (elem <- in) {
        m.update(elem, m.getOrElseUpdate(elem, 0) + 1)
      }
    
      // Remove elements with a single occurrence
      val dupes = m.filter(_._2 > 1)
    
      // Apply numbering to duplicate elements
      in.reverse.map(e => {
        val idx = dupes.get(e) match {
          case Some(i) =>
            dupes.update(e, i - 1)
            i.toString
          case _ =>
            ""
        }
    
        s"$e$idx"
        }).reverse
      }
    

    The code is easier if you wanted to apply a count to every element rather than just the non-unique ones.

    def makeUniq(in: Seq[String]): Seq[String] = {
      val m = mutable.Map.empty[String, Int]
    
      in.map{ e =>
        val i = m.getOrElseUpdate(e, 0) + 1
        m.update(e, i)
    
        s"$e$i"
      }
    }
    
    0 讨论(0)
提交回复
热议问题