I would like to know in which situations which data structures are optimal for using "contains" or "exists" checks.
I ask because I come from a Python background and am used to using if x in something: expressions for everything. For example, which…
I wrote an answer to the first Project Euler question:
Add all the natural numbers below one thousand that are multiples of 3 or 5.
The first thing that came to me was:
(1 until 1000).filter(i => (i % 3 == 0 || i % 5 == 0)).foldLeft(0)(_ + _)
but…
What is the best way of handling exceptions while iterating over a loop in Scala?
For instance, if I had a convert() method that could throw an exception, I'd like to catch that exception, log it, and keep iterating. Is there a "scala" way to do…
Each time a function is called, if it's result for a given set of argument values is not yet memoized I'd like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values.
How do I best…
This seems to compile fine with Java 7, and any version of the Scala libraries:
public static void main(String[] args) {
scala.collection.immutable.Set set = new scala.collection.immutable.HashSet();
Iterator iterator…
I know that to be Traversable, you need only have a foreach method. Iterable requires an iterator method.
Both the Scala 2.8 collections SID and the "Fighting Bitrot with Types" paper are basically silent on the subject of why Traversable was added.…
Assume you have a List(1,"1") it is typed List[Any], which is of course correct and expected. Now if I map the list like this
scala> List(1, "1") map {
| case x: Int => x
| case y: String => y.toInt
| }
the resulting type is…
I am trying to pull the maximum value form a map along with its key. For example:
val map = Map('a' -> 100, 'b' -> 23, ... 'z' -> 56)
Where 100 is the largest value, how would I go about pulling ('a',100)? I essentially want to use Map.max but…
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…
If I call toSeq on an immutable Set collection I get an ArrayBuffer.
scala> Set(1,2,3).toSeq // returns Seq[Int] = ArrayBuffer(1, 2, 3)
This surprises me. Given Scala's emphasis on using immutable data structures, I expect to get back an immutable…
It seems easy problem for any specific kind of Number i.e. Double/Integer but it is hard to write in general case.
implicit def iterebleWithAvg(data:Iterable[Double]) = new {
def avg:Double = data.sum / data.size
}
How to implement this for any…
Since I liked programming in Scala, for my Google interview, I asked them to give me a Scala / functional programming style question. The Scala functional style question that I got was as follows:
You have two strings consisting of alphabetic…
What is the idiomatic way of a getOrElseUpdate for immutable.Map instances?. I use the snippet below, but it seems verbose and inefficient
var map = Map[Key, Value]()
def foo(key: Key) = {
val value = map.getOrElse(key, new Value)
map += key ->…