scala-collections

Argument type of anonymous function

﹥>﹥吖頭↗ 提交于 2019-12-08 17:09:58
问题 I'm having some trouble with this code. It's supposed to be an OperationTree with Elements BinaryOperations and UnaryOperations. The method eval does the evaluation and looks up the variables in a map. Here's the code 1 import collection.immutable.HashMap 2 sealed abstract class OpTree[T]{ 3 4 def eval(v:HashMap[Char,T]):T = { 5 case Elem(x) => x 6 case UnOp(f,c) => { 7 f(c.eval(v)) 8 } 9 case BinOp(f,l,r) => { 10 f(l.eval(v),r.eval(v)) 11 } 12 case Var(c) => { 13 v.get(c) 14 } 15 } 16 } 17 /

Mutable HashMap with a mutable default value doesn't keep the changes [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-08 13:07:21
问题 This question already has an answer here : Update mutable HashMap value which is a mutable collection (1 answer) Closed last year . Suppose that I want a mutable HashMap[Int, HashSet[Int]] that has integers as keys mutable hash sets of integers as values I want that an empty mutable HashSet is created by default whenever a value for a new key is accessed or updated. Here is what I tried: import collection.mutable.{HashMap, HashSet} val hm = HashMap .empty[Int, HashSet[Int]] .withDefault(_ =>

How to provide helper methods to build a Map

橙三吉。 提交于 2019-12-08 10:14:09
问题 I have a lot of client code that build Map using the same keys (to query MongoDB). My idea is to provide helper methods that hide keys. First try, I have used default parameters (cf object Builder below) but the client hava to deal with Option I now use a builder pattern (cf class Builder below) Is there a better way ? class Builder { val m = collection.mutable.Map[String, Int]() def withA(a: Int) = {m += (("a", a))} def withB(b: Int) = {m += (("b", b))} def withC(c: Int) = {m += (("c", c))}

How to use companion factory objects as strategy?

让人想犯罪 __ 提交于 2019-12-08 03:59:54
问题 I have a test which fails intermittently because of ordering issues when I iterate over the values in a Map . Scala helpfully provides a ListMap which makes the tests stable, at the expense of performance. So I abstracted the ImmutableMapFactory as a val and use it in my code. class C { val immutableMapFactory = scala.collection.immutable.Map def func = { ... immutableMapFactory(pairs :_*) } } Now my plan was to extend C and override immutableMapFactory for tests class TestableC extends C {

How to return multiple items from a list, given their indices?

落爺英雄遲暮 提交于 2019-12-07 18:00:31
问题 Let's say, I have a list. If I want to return something at the index of that list, I can just pass the lists' val, the index. Like so. val list = List(1, 2, 3) list(0) //Int = 1 But what if I want items at multiple indexes on this list? I want to be able to do this... list(0, 1) and get a collection of the items at those indices. This is crazily simple in Ruby. Anyone have any suggestions? 回答1: You can flip the logic around, so that for each index, you're getting the index, you retrieve the

Scala for-comprehension returning an ordered map

左心房为你撑大大i 提交于 2019-12-07 14:26:50
问题 How can I use a for-comprehension that returns something I can assign to an ordered Map? This is a simplification of the code I have: class Bar class Foo(val name: String, val bar: Bar) val myList: java.util.List[Foo] = ... val result: ListMap[String, Bar] = for { foo <- myList } yield (foo.name, foo.bar) I need to make sure my result is an ordered Map, in the order tuples are returned from the for-comprehension. With the above, I get the error: error: type mismatch; found : scala.collection

How to convert list of integers to a map with frequency per bin?

爷,独闯天下 提交于 2019-12-07 09:01:17
问题 Lets say I have a list of numbers: val numbers = List(15, 30, 110, 140, 170, 210) How can I count the number of integers per bin of a 100 in order to get: Map(0 -> 2, 100 -> 3, 200 -> 1) 回答1: scala> List(1,2,3,101,330,302).groupBy(i => i/100) .map {case (i,l) => (i*100,l.length)} res1: scala.collection.immutable.Map[Int,Int] = Map(100 -> 1, 300 -> 2, 0 -> 3) 回答2: Starting Scala 2.13 , you can use the groupMapReduce method which is (as its name suggests) an equivalent of a groupBy followed by

flatMap on Map with wildcard type parameter

十年热恋 提交于 2019-12-07 07:03:23
问题 I'am trying to write something like this: trait Typed[T] trait Test { def testMap: Map[Typed[_], Int] def test = testMap.flatMap {case (typed, size) => Seq.fill(size)(typed)} } But I get the following error: error: no type parameters for method flatMap: (f: ((Typed[_], Int)) => Traversable[B])(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.immutable.Map[com.quarta.service.querybuilder.Typed[_],Int],B,That])That exist so that it can be applied to arguments (((Typed[_], Int

Implement a scala collection so that map, filter, etc. produce the right type

做~自己de王妃 提交于 2019-12-07 06:36:54
问题 I'm trying to implement a default valued map, and I'd like filters, maps, etc. over a DefaultingMap to also produce a DefaultingMap whenever possible. Here's my initial implementation: class DefaultingMap[K, V](defaultValue: => V) extends mutable.HashMap[K, V] with mutable.MapLike[K, V, DefaultingMap[K, V]] { override def empty = new DefaultingMap[K, V](defaultValue) override def default(key: K): V = { val result = this.defaultValue this(key) = result result } } I get objects of type

Efficient groupwise aggregation on Scala collections

我们两清 提交于 2019-12-07 05:55:26
问题 I often need to do something like coll.groupBy(f(_)).mapValues(_.foldLeft(x)(g(_,_))) What is the best way to achieve the same effect, but avoid explicitly constructing the intermediate collections with groupBy ? 回答1: You could fold the initial collection over a map holding your intermediate results: def groupFold[A,B,X](as: Iterable[A], f: A => B, init: X, g: (X,A) => X): Map[B,X] = as.foldLeft(Map[B,X]().withDefaultValue(init)){ case (m,a) => { val key = f(a) m.updated(key, g(m(key),a)) } }