Scala - How to convert Map[Int,List[List[IntVar]]] to - scala

I am new in Scala programming and the flatmap function gives me headache.
Before you ask me to search, I made some research on Stackoverflow : https://stackoverflow.com/search?q=scala+map+to+list but I didn't find something to solve easily my problem.
Here is my problem :
For a scala programming project with Jacop (http://jacopguide.osolpro.com/guideJaCoP.html) ,
I need to convert this kind of map
Map[Int,List[List[IntVar]]]
to :
List[T]
// In my case , only a List[IntVar]
I know I should use several times flatMap but I wonder how correctly use that in my case.
Thanks for your help

If you want every IntVar from the values of the map, you could to this:
map.values.flatten.flatten.toList
The call to values returns an Iterable containing all the values of the map. In this case, it returns an object of type Iterable[List[List[IntVar]]]. The first flatten-call on this object flattens it to an Iterable[List[IntVar]]. The second flatten-call flattens this object further to an Iterable[IntVar]. Finally, the toList method converts it to a List[IntVar].

Related

Arrays.asList() in Scala

Is there an equivalent to Arrays.asList() in Scala?
Or rather, how would you take a String and convert it into an Array, and then a List in Scala?
Any advice would be appreciated.
One common use of Arrays.asList is to produce a list containing the given elements:
Arrays.asList(x, y, z);
The Scala equivalent to that is just
Seq(x, y, z)
The other is to turn an existing array into list:
Arrays.asList(array);
In Scala, this is
array.toSeq
(note that I use Seq instead of List here; in Scala, List is a specific implementation, not an interface. Depending on what you want to do with it, some other type can be appropriate).
Or in many cases, nothing at all. Because Array[A] is implicitly convertible to IndexedSeq[A], collection operations can be done directly on it without converting first.
The same applies to String, with a caveat that operations Lists are good at are quite uncommon for strings, so string.toList is even less likely to be appropriate.

What's the most idiomatic way to express an iterable of a single element in Scala?

So far when I need to pass around an Iterable that consists of just one element, I pass a Some value; but that requires an implicit conversion.
In Java I would use java.util.Collections.singleton, and I guess there's something equivalent in Scala that better fits this use case.
Iterable(x), just as to get a Seq of a single element you write Seq(x), List(x), etc.
After taking a look to the implementation of the apply methods (constructors) of the collections (Iterable, Seq, List) that could fit the bill, they take varargs which require extra wrapping the object in an array and then looping over it or calling another method.
So I think I'm going to stick with consing the object like x :: Nil; that looks like the most lightweight way to achieve it and it's explicit that you are making a collection.

Most common string in list with counter

I am trying to write a function that
takes a List read from a file as input
outputs the most frequently used string as well an integer that shows the number of times that it was used.
example output:
("Cat",5)
function signature:
def mostFreq(info: List[List[String]]): (String, Int) =
First,I thought about creating a
Map variable and a counter variable
iterating over my list to fill the map
then iterate over the map
However, there must be a simpler way to do this scala but I'm not used to scala's library just yet.
I have seen this as one way to do it that uses only integers.
Finding the most frequent/common element in a collection?
But I was wondering how it could be done using string and integers.
The solution from the linked post has just about everything you need for this.
def mostFreq(info: List[List[String]]): (String, Int) =
info.flatten.groupBy(identity).mapValues(_.size).maxBy(_._2)
It doesn't handle ties terribly well, but you haven't stated how ties should be handled.

How to iterate through lazy iterable in scala? from stanford-tmt

Scala newbie here,
I'm using stanford's topic modelling toolkit
and it has a lazy iterable of type LazyIterable[(String, Array[Double])]
How should i iterate through all the elements in this iterable say it to print all these values?
I tried doing this by
while(it.hasNext){
System.out.println(it.next())
}
Gives an error
error: value next is not a member of scalanlp.collection.LazyIterable[(String, Array[Double])]
This is the API source -> iterable_name ->
InferCVB0DocumentTopicDistributions in
http://nlp.stanford.edu/software/tmt/tmt-0.4/api/edu/stanford/nlp/tmt/stage/package.html
Based on its source code, I can see that the LazyIterable implements the standard Scala Iterable interface, which means you have access to all the standard higher-order functions that all Scala collections implement - such as map, flatMap, filter, etc.
The one you will be interested in for printing all the values is foreach. So try this (no need for the while-loop):
it.foreach(println)
Seems like method invocation problem, just check the source code of LazyIterable, look at line 46
override def iterator : Iterator[A]
when you get an instance of LazyIterable, invoke iterator method, then you can do what you want.

Create SortedMap from Iterator in scala

I have an val it:Iterator[(A,B)] and I want to create a SortedMap[A,B] with the elements I get out of the Iterator. The way I do it now is:
val map = SortedMap[A,B]() ++ it
It works fine but feels a little bit awkward to use. I checked the SortedMap doc but couldn't find anything more elegant. Is there something like:
it.toSortedMap
or
SortedMap.from(it)
in the standard Scala library that maybe I've missed?
Edit: mixing both ideas from #Rex's answer I came up with this:
SortedMap(it.to:_*)
Which works just fine and avoids having to specify the type signature of SortedMap. Still looks funny though, so further answers are welcome.
The feature you are looking for does exist for other combinations, but not the one you want. If your collection requires just a single parameter, you can use .to[NewColl]. So, for example,
import collection.immutable._
Iterator(1,2,3).to[SortedSet]
Also, the SortedMap companion object has a varargs apply that can be used to create sorted maps like so:
SortedMap( List((1,"salmon"), (2,"herring")): _* )
(note the : _* which means use the contents as the arguments). Unfortunately this requires a Seq, not an Iterator.
So your best bet is the way you're doing it already.