What is the #:: operator in a scala Stream? [duplicate] - scala

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Searching the Scala documentation for #::
I'm looking through the docs of Stream
The filter method has this code:
def naturalsFrom(i: Int): Stream[Int] = i #:: naturalsFrom(i + 1)
naturalsFrom(1) 10 } filter { _ % 5 == 0 } take 10 mkString(", ")
What is the #:: operator? Does this map to a function call somewhere?

As SHildebrandt says, #:: is the cons operator for Streams.
In other words, #:: is to streams what :: is to Lists
val x = Stream(1,2,3,4) //> x : scala.collection.immutable.Stream[Int] = Stream(1, ?)
10#::x //> res0: scala.collection.immutable.Stream[Int] = Stream(10, ?)
val y = List(1,2,3,4) //> y : List[Int] = List(1, 2, 3, 4)
10::y //> res1: List[Int] = List(10, 1, 2, 3, 4)

x #:: xs
returns
Stream.cons(x, xs)
which returns a Stream of an element x followed by a Stream xs.

Related

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

How can I find the index of the maximum value in a List in Scala?

For a Scala List[Int] I can call the method max to find the maximum element value.
How can I find the index of the maximum element?
This is what I am doing now:
val max = list.max
val index = list.indexOf(max)
One way to do this is to zip the list with its indices, find the resulting pair with the largest first element, and return the second element of that pair:
scala> List(0, 43, 1, 34, 10).zipWithIndex.maxBy(_._1)._2
res0: Int = 1
This isn't the most efficient way to solve the problem, but it's idiomatic and clear.
Since Seq is a function in Scala, the following code works:
list.indices.maxBy(list)
even easier to read would be:
val g = List(0, 43, 1, 34, 10)
val g_index=g.indexOf(g.max)
def maxIndex[ T <% Ordered[T] ] (list : List[T]) : Option[Int] = list match {
case Nil => None
case head::tail => Some(
tail.foldLeft((0, head, 1)){
case ((indexOfMaximum, maximum, index), elem) =>
if(elem > maximum) (index, elem, index + 1)
else (indexOfMaximum, maximum, index + 1)
}._1
)
} //> maxIndex: [T](list: List[T])(implicit evidence$2: T => Ordered[T])Option[Int]
maxIndex(Nil) //> res0: Option[Int] = None
maxIndex(List(1,2,3,4,3)) //> res1: Option[Int] = Some(3)
maxIndex(List("a","x","c","d","e")) //> res2: Option[Int] = Some(1)
maxIndex(Nil).getOrElse(-1) //> res3: Int = -1
maxIndex(List(1,2,3,4,3)).getOrElse(-1) //> res4: Int = 3
maxIndex(List(1,2,2,1)).getOrElse(-1) //> res5: Int = 1
In case there are multiple maximums, it returns the first one's index.
Pros:You can use this with multiple types, it goes through the list only once, you can supply a default index instead of getting exception for empty lists.
Cons:Maybe you prefer exceptions :) Not a one-liner.
I think most of the solutions presented here go thru the list twice (or average 1.5 times) -- Once for max and the other for the max position. Perhaps a lot of focus is on what looks pretty?
In order to go thru a non empty list just once, the following can be tried:
list.foldLeft((0, Int.MinValue, -1)) {
case ((i, max, maxloc), v) =>
if (v > max) (i + 1, v, i)
else (i + 1, max, maxloc)}._3
Pimp my library! :)
class AwesomeList(list: List[Int]) {
def getMaxIndex: Int = {
val max = list.max
list.indexOf(max)
}
}
implicit def makeAwesomeList(xs: List[Int]) = new AwesomeList(xs)
//> makeAwesomeList: (xs: List[Int])scalaconsole.scratchie1.AwesomeList
//Now we can do this:
List(4,2,7,1,5,6) getMaxIndex //> res0: Int = 2
//And also this:
val myList = List(4,2,7,1,5,6) //> myList : List[Int] = List(4, 2, 7, 1, 5, 6)
myList getMaxIndex //> res1: Int = 2
//Regular list methods also work
myList filter (_%2==0) //> res2: List[Int] = List(4, 2, 6)
More details about this pattern here: http://www.artima.com/weblogs/viewpost.jsp?thread=179766

Weird Scala behavior when defining simple Stream

Why 6? I'd like to define sequence (5, 6, ...). How to do it correctly using "val" not "def"?
scala> val f: Stream[Int] = 5 #:: f map { _ + 1 }
f: Stream[Int] = Stream(6, ?)
I'm using scala 2.9.2
You need some parentheses to tell it to apply the map to f, but not to the 5:
scala> val f: Stream[Int] = 5 #:: (f map { _ + 1 })
f: Stream[Int] = Stream(5, ?)
scala> f.take(5).toList
res2: List[Int] = List(5, 6, 7, 8, 9)

Generate a sequence of Fibonacci number in Scala [duplicate]

This question already has answers here:
What is the fastest way to write Fibonacci function in Scala?
(8 answers)
Closed 5 years ago.
def fibSeq(n: Int): List[Int] = {
var ret = scala.collection.mutable.ListBuffer[Int](1, 2)
while (ret(ret.length - 1) < n) {
val temp = ret(ret.length - 1) + ret(ret.length - 2)
if (temp >= n) {
return ret.toList
}
ret += temp
}
ret.toList
}
So the above is my code to generate a Fibonacci sequence using Scala to a value n. I am wondering if there is a more elegant way to do this in Scala?
This is a bit more elegant:
val fibs: Stream[Int] = 0 #:: fibs.scanLeft(1)(_ + _)
With Streams you "take" a number of values, which you can then turn into a List:
scala> fibs take 10 toList
res42: List[Int] = List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)
Update: I've written a blog post which goes more detail regarding how this solution works, and why you end up with a Fibonacci sequence!
There are many ways to define the Fibonacci sequence, but my favorite is this one:
val fibs:Stream[Int] = 0 #:: 1 #:: (fibs zip fibs.tail).map{ t => t._1 + t._2 }
This creates a stream that is evaluated lazily when you want a specific Fibonacci number.
EDIT:
First, as Luigi Plinge pointed out, the "lazy" at the beginning was unnecessary.
Second, go look at his answer, he pretty much did the same thing only more elegantly.
Not as elegant as Streams, not lazy, but tailrecursive and handles BigInt (which is easy to do with Luigis scanLeft too, but not so with Tal's zip - maybe just for me).
#tailrec
def fib (cnt: Int, low: BigInt=0, high: BigInt=1, sofar: List[BigInt]=Nil): List[BigInt] = {
if (cnt == 0) (low :: sofar).reverse else fib (cnt - 1, high, low + high, low :: sofar) }
scala> fib (75)
res135: List[BigInt] = List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050)
My favorite version is:
def fibs(a: Int = 0, b: Int = 1): Stream[Int] = Stream.cons(a, fibs(b, a+b))
With the default values you can just call fibs() and get the infinite Stream.
I also think it's highly readable despite being a one liner.
If you just want the first n then you can use take like fibs() take n, and if you need it as a list fibs() take n toList.
Here's yet another approach again using *Stream*s on an intermediary tuples:
scala> val fibs = Stream.iterate( (0,1) ) { case (a,b)=>(b,a+b) }.map(_._1)
fibs: scala.collection.immutable.Stream[Int] = Stream(0, ?)
scala> fibs take 10 toList
res68: List[Int] = List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)
I find this implementation to be more legible:
def fibonacci: Stream[Int] = {
def loop(a: Int, b: Int): Stream[Int] = (a + b) #:: loop(b, b + a)
loop(0, 1)
}
def fib:Stream[Int] ={
def go(f0: Int, f1:Int): Stream[Int] = {
Stream.cons(f0,go(f1,f0+f1))
}
go(0,1)
}

Make a recursive Stream from just 1 element?

I've made a Stream for Gray Codes using recursion as follows:
val gray: Stream[List[String]] = {
List("") #:: List("0", "1") #:: gray.tail.map {gnext}
}
where
val gnext = (i:List[String]) => i.map {"0" + _} ::: i.reverse.map {"1" + _}
so that, for example
scala> gray(2)
res17: List[String] = List(00, 01, 11, 10)
I don't really need the List("0", "1") in the definition, because it can be produced from element 0:
scala> gnext(List(""))
res18: List[java.lang.String] = List(0, 1)
So is there a way / pattern that can be used to produce a Stream from just the first element?
val gray: Stream[List[String]] = List("") #:: gray.map {gnext}
Or, alternatively,
val gray = Stream.iterate(List(""))(gnext)