Option is implicitly convertible to an Iterable - but why does it not just just implement Iterable directly:
def iterator = new Iterator[A] {
var end = !isDefined
def next() = {
val n = if (end) throw new NoSuchElementException() else get
…
In Scala collections, if one wants to iterate over a collection (without returning results, i.e. doing a side effect on every element of collection), it can be done either with
final def foreach(f: (A) ⇒ Unit): Unit
or
final def map[B](f: (A) ⇒ B):…
I've just noticed a disturbing behavior.
Let's say I have a standalone program consisting of a sole object:
object ParCollectionInInitializerTest {
def doSomething { println("Doing something") }
for (i <- (1 to 2).par) {
println("Inside…
I'm curious if Scala has some gem hidden in its collection classes that I can use. Basically I'm looking for something like a FIFO queue, but that has an upper-limit on its size such that when the limit is hit, the oldest (first) element is removed…
Please note: This is not a duplicate question, since this question specifies on all methods Iterator has, not just map and flatMap. Therefore Future.traverse are not a good answer.
Let's say I have this simple statement:
(1 to…
I am trying to implement A* search in Scala (version 2.10), but I've ran into a brick wall - I can't figure out how to use Scala's Priority Queue.
I have a set of squares, represented by (Int, Int)s, and I need to insert them with priorities…
I'm having compilation issues between Scala and Java.
My Java code needs a
java.util.Map>
My scala code has a
Map[Double, Vector[Foo]]
I get the compilation error:
error: type mismatch;
found :…
Is it somehow possible, using Scala's parallel collections to parallelize an Iterator without evaluating it completely beforehand?
Here I am talking about parallelizing the functional transformations on an Iterator, namely map and flatMap.
I think…
According to scaladoc, sliding() returns...
"An iterator producing iterable collections of size size, except the last and the only element will be truncated if there are fewer elements than size."
For me, intuitivelly, sliding(n) would return a…
I want to split a string delimited by commas and use the result as either a Seq or a Set:
def splitByComma(commaDelimited: String): Array[String]
= commaDelimited.trim.split(',')
def splitByCommaAsSet(commaDelimited: String): Set[String]
=…
What is the best way to create a Map[K,V] from a Set[K] and function from K to V?
For example, suppose I have
scala> val s = Set(2, 3, 5)
s: scala.collection.immutable.Set[Int] = Set(2, 3, 5)
and
scala> def func(i: Int) = "" + i + i
func: (i:…
How to do that without creating any new collections? Is there something better than this?
val m = scala.collection.mutable.Map[String, Long]("1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4)
m.foreach(t => if (t._2 % 2 == 0) m.remove(t._1))
println(m)
P.S.…