scala-collections

Using scala map in Java

删除回忆录丶 提交于 2019-12-19 09:06:46
问题 I have two files. One is scala and other is java. Scala file has a function which returns scala immutable map. Java file wants to use that map as dictionary. I am a newbie to scala and java. How can I convert that scala map to java dicionary? 回答1: Use the static forwarders from Java. In Scala 2.13 the API is simplified, with overloaded asJava for conversion to Java: $ javap -cp ~/scala-2.13.0/lib/scala-library.jar scala.jdk.javaapi.CollectionConverters Compiled from "CollectionConverters

short way to breakOut to specific collection type?

醉酒当歌 提交于 2019-12-19 06:03:53
问题 scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> m.map{case (a, b) => (a+ 1, a+2, a+3)} res42: scala.collection.immutable.Iterable[(Int, Int, Int)] = List((2,3,4)) What I want is for the result type to be List[(Int, Int, Int)]. The only way I found is: scala> m.map{case (a, b) => (a+ 1, a+2, a+3)}(breakOut[Map[_,_], (Int, Int, Int), List[(Int, Int, Int)]]) res43: List[(Int, Int, Int)] = List((2,3,4)) Is there a shorter way? 回答1: You can make it a bit

split a stream in many

拥有回忆 提交于 2019-12-19 02:32:08
问题 I'd like to know if there a elegant way to achieve something like that: val l = Stream.from(1) val parts = l.some_function(3) //any number parts.foreach( println(_) ) > 1,4,7,10... > 2,5,8,11... > 3,6,9,12... Actually I need such operation on Streams for parallelization - to split the data across multiple actors without loading the whole stuff into memory. 回答1: The answer from Split a scala list into n interleaving lists fully meets the conditions, a little bit modified to suit Streams: def

Migrating Java TreeMap code to Scala?

你离开我真会死。 提交于 2019-12-18 19:00:08
问题 I am migrating my Java code base to pure Scala and I am stuck on this one piece of code. I have an implementation of an IntervalMap i.e. a data structures that let's you efficiently map ranges [from,to] to values where the set , delete and get operations are all O(log n) (slightly different from an IntervalTree or a SegmentTree). This code uses Java's java.util.TreeMaps and while migrating to Scala, I ran into 2 big issues: Scala has no mutable.TreeMap - I decided to go around it by using

Scala Map.mapValues stackoverflowerror

巧了我就是萌 提交于 2019-12-18 16:54:50
问题 The following code results in a StackOverflowError on the last line. object StackTest extends App{ @tailrec def incrementValues(acc: Map[String, Int], inc: Int): Map[String, Int] = { if(inc == 0) acc else incrementValues(acc.mapValues(_ + 1), inc - 1) } val myMap = incrementValues(Map("key" -> 0), 10000) myMap.foreach(println) } In Scala 2.11.2: Exception in thread "main" java.lang.StackOverflowError at scala.collection.MapLike$MappedValues.foreach(MapLike.scala:245) at scala.collection

Generic, type-safe way to flatten arbitrarily nested collections in Scala?

血红的双手。 提交于 2019-12-18 13:12:46
问题 On occasion I take some time to play with Scala, whose mix of features appeals to me despite an inability to use it in my own work (thus far). For kicks I decided to try the first few 99 Haskell Problems in the most generic way possible — operating on and returning any kind of applicable collection. The first few questions weren’t too difficult, but I find myself utterly stymied by flatten . I just can’t figure out how to type such a thing. To be specific about my question: is it possible to

scala Iterable#map vs. Iterable#flatMap

丶灬走出姿态 提交于 2019-12-18 10:08:23
问题 What is the difference between the map and flatMap functions of Iterable ? 回答1: Here is a pretty good explanation: http://www.codecommit.com/blog/scala/scala-collections-for-the-easily-bored-part-2 Using list as an example: Map's signature is: map [B](f : (A) => B) : List[B] and flatMap's is flatMap [B](f : (A) => Iterable[B]) : List[B] So flatMap takes a type [A] and returns an iterable type [B] and map takes a type [A] and returns a type [B] This will also give you an idea that flatmap will

Use-cases for Streams in Scala

本小妞迷上赌 提交于 2019-12-18 09:59:53
问题 In Scala there is a Stream class that is very much like an iterator. The topic Difference between Iterator and Stream in Scala? offers some insights into the similarities and differences between the two. Seeing how to use a stream is pretty simple but I don't have very many common use-cases where I would use a stream instead of other artifacts. The ideas I have right now: If you need to make use of an infinite series. But this does not seem like a common use-case to me so it doesn't fit my

How does Scala's mutable Map update [map(key) = newValue] syntax work?

天大地大妈咪最大 提交于 2019-12-18 03:05:22
问题 I'm working through Cay Horstmann's Scala for the Impatient book where I came across this way of updating a mutable map. scala> val scores = scala.collection.mutable.Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8) scores: scala.collection.mutable.Map[String,Int] = Map(Bob -> 3, Alice -> 10, Cindy -> 8) scala> scores("Alice") // retrieve the value of type Int res2: Int = 10 scala> scores("Alice") = 5 // Update the Alice value to 5 scala> scores("Alice") res4: Int = 5 It looks like scores("Alice")

Scala collection memory footprint characteristics

本小妞迷上赌 提交于 2019-12-18 02:34:47
问题 There is a handy page about performance characteristics of the Scala collection classes. Is there similar data on memory footprint? I have a situation where I'm concerned about memory use and would like to factor this in my choice of collection to use. For instance, between Array[Array[T]] and Vector[Vector[T]] . 回答1: Here is what I've found out by filling up those respective immutable sequences with 1,000,000 objects on 2.9.0. I had them all point to the same object to factor out the size of