Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
8
votes
2 answers

Calling Scala Monads from Java #map

I have an instance of a scala.collection.immutable.List and I want to call the map method on it, BUT from Java. I need to supply a CanBuildFrom. I noticed that a lot of the scala collections companion objects contain implicit CanBuildFrom instances,…
John Smith
  • 3,037
  • 5
  • 29
  • 31
8
votes
3 answers

Scala convert Option to an Int

I have looked at these links http://blog.danielwellman.com/2008/03/using-scalas-op.html http://blog.tmorris.net/scalaoption-cheat-sheet/ I have a map of [String, Integer] and when I do a map.get("X") I get an option. I would like the following. val…
Kannan Ekanath
  • 16,759
  • 22
  • 75
  • 101
7
votes
2 answers

Distributing Scala over a cluster?

So I've recently started learning Scala and have been using graphs as sort of my project-to-improve-my-Scala, and it's going well - I've since managed to easily parallelize some graph algorithms (that benefit from data parallelization) courtesy of…
adelbertc
  • 7,270
  • 11
  • 47
  • 70
7
votes
4 answers

Does it make any sense to use pattern matching in Scala with really simple cases?

In 'Programming in Scala, Second Edition' at page 410 you can find class Simulation which have the following method: private def next() { (agenda: @unchecked) match { case item :: rest => agenda = rest curtime = item.time …
Piotr Kukielka
  • 3,792
  • 3
  • 32
  • 40
7
votes
1 answer

collection.mutable.OpenHashMap vs collection.mutable.HashMap

For put and get operations OpenHashMap outperform HashMap by about 5 times: https://gist.github.com/1423303 Are any cases when HashMap should be preferred over OpenHashMap?
Andriy Plokhotnyuk
  • 7,883
  • 2
  • 44
  • 68
7
votes
5 answers

How to write tuple range function in scala?

I want following function range((1,1), (2,2)) which return Seq[(Int,Int)]((1,1),(1,2),(2,1),(2,2)) It is analog for one dimensional range with 1 to 2 The function should work for any scala tuple (i.e. Tuple2, Tuple3, Tuple4, ...) and be…
yura
  • 14,489
  • 21
  • 77
  • 126
7
votes
4 answers

Scala collectFirst with function returning Option[U]

I've had this situation occur a number of times in the library I'm writing, and I'm not particularly satisfied with the solutions I've come up with so far. Let's say that I have an expensive function f that takes an item of type T and returns a…
Nimrand
  • 1,748
  • 2
  • 16
  • 29
7
votes
4 answers

How to group a variable-length, repeating sequence in Scala

I have a collection of ints that repeat themselves in a pattern: val repeatingSequence = List(1,2,3,1,2,3,4,1,2,1,2,3,4,5) I'd like to section that List up when the pattern repeats itself; in this case, when the sequence goes back to 1: val…
Matt Hughes
  • 1,458
  • 2
  • 14
  • 17
7
votes
2 answers

Missing par method from Scala collections

I tried to convert a sequential list to a parallel one in Intellij, but I get the error Cannot resolve symbol par on the .par method call: import scala.collection.parallel.immutable._ ... val parList = List(1,2,3).par According to…
7
votes
3 answers

what is equivalent function of Map.compute in scala.collection.mutable.Map

Java has method in java.util.Map called compute which provides a way to update map when the key is present or absent in the map. Does scala.collection.mutable.Map provides any similar function? I've checked the documentation Map and HashMap but…
user51
  • 8,843
  • 21
  • 79
  • 158
7
votes
1 answer

Is it possible to print definition of a function in Scala

I was wondering if we can print the definition of a function in Scala. A function is treated as an object in Scala. For example: scala> val splitFunction = (value : String) => { value.split(" ")} splitFunction: String => Array[String] =…
7
votes
1 answer

How to convert an untyped java.util.List to a Scala 2.8 Buffer

I have to call some Java library code that returns an untyped java.util.List and I can't seem to convert this into a Scala 2.8 list without the compiler borking with the following error: [INFO] found : java.util.List[?0] where type ?0 [INFO] …
Age Mooij
  • 824
  • 4
  • 14
7
votes
4 answers

Adding two Set[Any]

Adding two Set[Int] works: Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Server VM, Java 1.6.0_23). Type in expressions to have them evaluated. Type :help for more information. scala> Set(1,2,3) ++ Set(4,5,6) res0:…
Alex Boisvert
  • 2,850
  • 2
  • 19
  • 18
7
votes
5 answers

Extending Scala collections: One based Array index exercise

As an exercise, I'd like to extend the Scala Array collection to my own OneBasedArray (does what you'd expect, indexing starts from 1). Since this is an immutable collection, I'd like to have it return the correct type when calling filter/map…
Pengin
  • 4,692
  • 6
  • 36
  • 62
7
votes
6 answers

What common patterns/solutions have been established in Scala for frequently-encountered problems

I'll start the ball rolling. Given a sequence of elements, some of which can be contained multiple times, one typical requirement is to count them - in the form of a tally or histogram. The oft-quoted solution…
Kevin Wright
  • 49,540
  • 9
  • 105
  • 155