Scala List building - scala

I am having trouble understanding Scala lists. I just want to create a list of lists: List(list(1,2), List(3,4))
In the REPL I am trying:
val list= List()
val lt = List(1,2)
val ls = List(3,4)
list::lt resolves to - res0: List[Any] = List(List(), 1, 2)
list::ls resolves to - res1: List[Any] = List(List(), 3, 4)
I'm coming from java and have never programmed functionally. I am just not getting it.
Thanks for any help!!

You'll want to read the book, where it explains that cons :: prepends to the thing on the right.
one way to initialize new lists is to string together elements with
the cons operator, with Nil as the last element.
scala> List(1,2) :: Nil
res1: List[List[Int]] = List(List(1, 2))
The book also explains about operators ending in a colon.
If you stick a List[Nothing] on the front of a List[Int], you get the List[Any] you witnessed.

Related

Prepend to an Iterable?

How can I prepend, i.e. cons, to an Iterable?
scala> val xs: Iterable[Int] = Seq(1)
xs: Iterable[Int] = List(1)
scala> xs :: 5
<console>:15: error: value :: is not a member of Int
xs :: 5
^
I looked at the docs, but didn't figure it out.
:: is specific to List. It is a List, in fact.
There is no concept of prepending to an Iterable, as not all Iterables will guarantee order (Set does not, for example). You may want Seq instead, which would use +: to prepend.
Iterable doesn't have a prepend method, but you can use "++" to join two iterables:
scala> val xs: Iterable[Int] = Seq(1)
xs: Iterable[Int] = List(1)
scala> List(5) ++ xs
res0: List[Int] = List(5, 1)
Seq does have a prepend method, +:, and you could convert to a seq:
scala> 5 +: xs.toSeq
res1: Seq[Int] = List(5, 1)

Scala Iteratively build lists

As a Scala beginner I am still struggling working with immutable lists. All I am trying to do append elements to my list. Here's an example of what I am trying to do.
val list = Seq()::Nil
val listOfInts = List(1,2,3)
listOfInts.foreach {case x=>
list::List(x)
}
expecting that I would end up with a list of lists: List(List(1),List(2),List(3))
Coming from java I am used to just using list.add(new ArrayList(i)) to get the same result. Am I way off here?
Since the List is immutable you can not modify the List in place.
To construct a List of 1 item Lists from a List, you can map over the List. The difference between forEach and map is that forEach returns nothing, i.e. Unit, while map returns a List from the returns of some function.
scala> def makeSingleList(j:Int):List[Int] = List(j)
makeSingleList: (j: Int)List[Int]
scala> listOfInts.map(makeSingleList)
res1: List[List[Int]] = List(List(1), List(2), List(3))
Below is copy and pasted from the Scala REPL with added print statement to see what is happening:
scala> val list = Seq()::Nil
list: List[Seq[Nothing]] = List(List())
scala> val listOfInts = List(1,2,3)
listOfInts: List[Int] = List(1, 2, 3)
scala> listOfInts.foreach { case x=>
| println(list::List(x))
| }
List(List(List()), 1)
List(List(List()), 2)
List(List(List()), 3)
During the first iteration of the foreach loop, you are actually taking the first element of listOfInts (which is 1), putting that in a new list (which is List(1)), and then adding the new element list (which is List(List()) ) to the beginning of List(1). This is why it prints out List(List(List()), 1).
Since your list and listOfInts are both immutable, you can't change them. All you can do is perform something on them, and then return a new list with the change. In your case list::List(x) inside the loop actually doesnt do anything you can see unless you print it out.
There are tutorials on the documentation page.
There is a blurb for ListBuffer, if you swing that way.
Otherwise,
scala> var xs = List.empty[List[Int]]
xs: List[List[Int]] = List()
scala> (1 to 10) foreach (i => xs = xs :+ List(i))
scala> xs
res9: List[List[Int]] = List(List(1), List(2), List(3), List(4), List(5), List(6), List(7), List(8), List(9), List(10))
You have a choice of using a mutable builder like ListBuffer or a local var and returning the collection you build.
In the functional world, you often build by prepending and then reverse:
scala> var xs = List.empty[List[Int]]
xs: List[List[Int]] = List()
scala> (1 to 10) foreach (i => xs = List(i) :: xs)
scala> xs.reverse
res11: List[List[Int]] = List(List(1), List(2), List(3), List(4), List(5), List(6), List(7), List(8), List(9), List(10))
Given val listOfInts = List(1,2,3), and you want the final result as List(List(1),List(2),List(3)).
Another nice trick I can think of is sliding(Groups elements in fixed size blocks by passing a "sliding window" over them)
scala> val listOfInts = List(1,2,3)
listOfInts: List[Int] = List(1, 2, 3)
scala> listOfInts.sliding(1)
res6: Iterator[List[Int]] = non-empty iterator
scala> listOfInts.sliding(1).toList
res7: List[List[Int]] = List(List(1), List(2), List(3))
// If pass 2 in sliding, it will be like
scala> listOfInts.sliding(2).toList
res8: List[List[Int]] = List(List(1, 2), List(2, 3))
For more about the sliding, you can have a read about sliding in scala.collection.IterableLike.
You can simply map over this list to create a List of Lists.
It maintains Immutability and functional approach.
scala> List(1,2,3).map(List(_))
res0: List[List[Int]] = List(List(1), List(2), List(3))
Or you, can also use Tail Recursion :
#annotation.tailrec
def f(l:List[Int],res:List[List[Int]]=Nil) :List[List[Int]] = {
if(l.isEmpty) res else f(l.tail,res :+ List(l.head))
}
scala> f(List(1,2,3))
res1: List[List[Int]] = List(List(1), List(2), List(3))
In scala you have two (three, as #som-snytt has shown) options -- opt for a mutable collection (like Buffer):
scala> val xs = collection.mutable.Buffer(1)
// xs: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1)
scala> xs += 2
// res10: xs.type = ArrayBuffer(1, 2)
scala> xs += 3
// res11: xs.type = ArrayBuffer(1, 2, 3)
As you can see, it works just like you would work with lists in Java. The other option you have, and in fact it's highly encouraged, is to opt to processing list functionally, that's it, you take some function and apply it to each and every element of collection:
scala> val ys = List(1,2,3,4).map(x => x + 1)
// ys: List[Int] = List(2, 3, 4, 5)
scala> def isEven(x: Int) = x % 2 == 0
// isEven: (x: Int)Boolean
scala> val zs = List(1,2,3,4).map(x => x * 10).filter(isEven)
// zs: List[Int] = List(10, 20, 30, 40)
// input: List(1,2,3)
// expected output: List(List(1), List(2), List(3))
val myList: List[Int] = List(1,2,3)
val currentResult = List()
def buildIteratively(input: List[Int], currentOutput: List[List[Int]]): List[List[Int]] = input match {
case Nil => currentOutput
case x::xs => buildIteratively(xs, List(x) :: currentOutput)
}
val result = buildIteratively(myList, currentResult).reverse
You say in your question that the list is immutable, so you do are aware that you cannot mutate it ! All operations on Scala lists return a new list. By the way, even in Java using a foreach to populate a collection is considered a bad practice. The Scala idiom for your use-case is :
list ::: listOfInts
Shorter, clearer, more functional, more idiomatic and easier to reason about (mutability make things more "complicated" especially when writing lambda expressions because it breaks the semantic of a pure function). There is no good reason to give you a different answer.
If you want mutability, probably for performance purposes, use a mutable collection such as ArrayBuffer.

Safely Get Tail of Array

I called tail on an Array, but saw a warning.
scala> val arr = Array(1,2)
arr: Array[Int] = Array(1, 2)
scala> arr tail
warning: there were 1 feature warning(s); re-run with -feature for details
res3: Array[Int] = Array(2)
Scaladocs for Array shows an UnsupportedOperationException [will be thrown]
if the mutable indexed sequence is empty.
Is there a safe, won't throw exception, way to get the tail of an array?
Is there a safe, won't throw exception, way to get the tail of an
array?
You can use drop:
scala> Array(1, 2, 3).drop(1)
res0: Array[Int] = Array(2, 3)
scala> Array[Int]().drop(1)
res1: Array[Int] = Array()
Also note that as mentioned by #TravisBrown in the comment below, drop doesn't differentiate between an empty tail (for collections with one element) and the absence of tail (for empty collections) since in both the cases, drop(1) would return an empty collection.
First of all, the warning doesn't have anything to do with the fact that you're using an unsafe method. If you restart the REPL with -feature you'll see the following:
scala> arr tail
<console>:9: warning: postfix operator tail should be enabled
by making the implicit value language.postfixOps visible...
And a pointer to the documentation for scala.language.postfixOps, which includes the following note:
Postfix operators interact poorly with semicolon inference. Most programmers avoid them for this reason.
This is good advice. Being able to write arr tail instead of arr.tail isn't worth the fuss.
Now about the safety issue. The standard library doesn't provide a tailOption for collections, but you can get the same effect using headOption and map:
scala> val arr = Array(1, 2)
arr: Array[Int] = Array(1, 2)
scala> arr.headOption.map(_ => arr.tail)
res0: Option[Array[Int]] = Some([I#359be9fb)
If this is too verbose or opaque or inefficient for you, you can easily create an implicit class that would add a nicer tailOption to Array (or Seq, or IndexedSeq, etc.).
See #TravisBrown's answer for the reason of your warning.
I would personally use a scala.util.Try rather than his rather clever map on the array's head:
scala> val arr = Array(0, 1, 2)
arr: Array[Int] = Array(0, 1, 2)
scala> scala.util.Try {arr.tail} foreach {t => println(t.mkString(","))}
1,2

Difference between ::: and ++ for concatenating Lists [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Scala list concatenation, ::: vs ++
In Scala, say I have two lists
scala> val oneTwo = List(1,2)
oneTwo: List[Int] = List(1, 2)
and
scala> val threeFour = List(3,4)
threeFour: List[Int] = List(3, 4)
I can concatenates Lists by doing:
scala> oneTwo ::: threeFour
res30: List[Int] = List(1, 2, 3, 4)
Or
scala> oneTwo ++ threeFour
res31: List[Int] = List(1, 2, 3, 4)
What is the difference between both approaches?
Thanks.
The ::: method is specific to List, while ++ is part of any Traversable.
The difference arise out of two things. First, List is one of the original Scala collections, used a lot in the compiler, and subject to special optimizations. The :: concatenation is the same as used in the ML family of languages, one of the big Scala inspirations, and ::: extrapolates from it.
On the other hand, ++ came along with the redesign of Scala collections on Scala 2.8.0, which made methods and inheritance uniform. I think it existed before that (on Set, for example), but the collections did not share a common superclass, so it was basically an ad hoc method for other collections.
In terms of performance, ::: should beat ++, but probably not significantly.
From the docs:
::: [B >: A](prefix : List[B]) : List[B]
++ [B >: A](that : Iterable[B]) : List[B]
You can see the ++ works for any Iterable while ::: is specifically for List's
scala> val oneTwo = List(1,2)
oneTwo: List[Int] = List(1, 2)
scala> val threeFour = List(3,4)
threeFour: List[Int] = List(3, 4)
scala> val fiveSix = Array(5,6)
fiveSix: Array[Int] = Array(5, 6)
scala> oneTwo ++ fiveSix
res2: List[Int] = List(1, 2, 5, 6)
scala> oneTwo ::: fiveSix
<console>:10: error: value ::: is not a member of Array[Int]
oneTwo ::: fiveSix

Scala: Yielding from one type of collection to another

Concerning the yield command in Scala and the following example:
val values = Set(1, 2, 3)
val results = for {v <- values} yield (v * 2)
Can anyone explain how Scala knows which type of collection to yield into? I know it is based on values, but how would I go about writing code that replicates yield?
Is there any way for me to change the type of the collection to yield into? In the example I want results to be of type List instead of Set.
Failing this, what is the best way to convert from one collection to another? I know about _:*, but as a Set is not a Seq this does not work. The best I could find thus far is val listResults = List() ++ results.
Ps. I know the example does not following the recommended functional way (which would be to use map), but it is just an example.
The for comprehensions are translated by compiler to map/flatMap/filter calls using this scheme.
This excellent answer by Daniel answers your first question.
To change the type of result collection, you can use collection.breakout (also explained in the post I linked above.)
scala> val xs = Set(1, 2, 3)
xs: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
scala> val ys: List[Int] = (for(x <- xs) yield 2 * x)(collection.breakOut)
ys: List[Int] = List(2, 4, 6)
You can convert a Set to a List using one of following ways:
scala> List.empty[Int] ++ xs
res0: List[Int] = List(1, 2, 3)
scala> xs.toList
res1: List[Int] = List(1, 2, 3)
Recommended read: The Architecture of Scala Collections
If you use map/flatmap/filter instead of for comprehensions, you can use scala.collection.breakOut to create a different type of collection:
scala> val result:List[Int] = values.map(2*)(scala.collection.breakOut)
result: List[Int] = List(2, 4, 6)
If you wanted to build your own collection classes (which is the closest thing to "replicating yield" that makes any sense to me), you should have a look at this tutorial.
Try this:
val values = Set(1, 2, 3)
val results = for {v <- values} yield (v * 2).toList