When I use auto-complete on lists on InteliJ, it shows a single-right-arrow, with no documentation on what it means. It looks like →. In an example, it's called as:
val sampleList: List[String] = List("a", "b", "c");
sampleList.→()
I don't know…
Given:
val list = List("one","two","three")
val it = list.toIterator
I can run:
list map ("_" +) -> List(_one, _two, _three)
for (i <- list) yield("_" + i) -> List(_one, _two, _three)
If I run the same on the iterator I get:
it map ("_" + )…
I am looking to calculate the scalar product of two lists. Let's say we have two Lists, l1 = List(1,2,3) and l2 = List(4,5,6), the result should be List(4,10,18)
The code below works:
def scalarProduct(l1 : List[Int], l2 : List[Int]):List[Int] =…
Here's a strange behavior I fell into and I can't find any hint on why it's like this. I use in this example the estimate method of SizeEstimator from Spark but I haven't found any glitch in their code so I wonder why - if they provide a good…
I want to implement a function in Scala, that, given a Set of Sets of Ints will merge any containing Set that contains one or more common elements.
So for example, given:
def mergeSets(sets: Set[Set[Int]]): Set[Set[Int]] = ???
val sets =…
The documentation says that Set.head returns the "first" item, and .tail returns "all but the first".* Since a Set doesn't really have a "first" item, the documentation warns that without an ordered type, you might get a different result on…
I have to use a mutable linked list for a specific use case. However I'd like to avoid "Reference must be prefixed" warnings.
Aliasing the import seems to be a solution:
import scala.collection.mutable.{LinkedList => MutableLinkedList}
it works…
I have a class with a private field that is a mutable collection. The field in this particular instance is an ArrayBuffer, although my question extends to any finite, ordered, random-access collection type. I want to expose this field without…
I have an array that I want to permutate randomly. In Java, there is a method Collections.shuffle() that can shuffle the elements of a List randomly. It can be used on an array too:
String[] array = new String[]{"a", "b", "c"};
// Shuffle the…
I've discovered a strange behavior for mutable sets which I cannot understand:
I have a object which I want to add to a set. The equals method for the class is overridden. When I add two different objects to the set, which produces the same output…
In Java 1.6, the NavigableMap (and the NavigableSet) interfaces were introduced and TreeMap was updated to implement the new interface. Among other things, NavigableMap is useful for asking questions like "Which element in the collection is closest…
I have just found out this syntax for a scala Map (used here in mutable form)
val m = scala.collection.mutable.Map[String, Int]()
m("Hello") = 5
println(m) //PRINTS Map(Hello -> 5)
Now I'm not sure whether this is syntactic sugar built in to the…
What is the equivalent Scala constructor (to create an immutable HashSet) to the Java
new HashSet(c)
where c is of type Collection extends T>?.
All I can find in the HashSet Object is apply.
So scala 2.9 recently turned up in Debian testing, bringing the newfangled parallel collections with it.
Suppose I have some code equivalent to
def expensiveFunction(x:Int):Int = {...}
def process(s:List[Int]):List[Int} =…
If I have this:
val a = Array("a ","b ","c ")
val b = Array("x","y")
I would like to know if such a method exists which would let me traverse the first collection, and for each of it's elements, walk the entire second collection. For example, if we…