C
C
cs0ip2013-09-23 15:20:09
Scala
cs0ip, 2013-09-23 15:20:09

Scala Stream

Can someone explain this aspect of how threads work?

scala> 1 #:: (null:Stream[Int])
res5: scala.collection.immutable.Stream[Int] = Stream(1, ?)

The following facts haunt me:
Scala reference, 6.12.3
If op is right-associative, the same operation is interpreted as { val x = e1; e2.op(x) },
where x is a fresh name.

Scala example, 12
Two methods in class List which are not supported by class Stream are :: and :::.
The reason is that these methods are dispatched on their right-hand side argument,
which means that this argument needs to be evaluated before the method is called.
...
Instead of x :: xs, one uses Stream.cons(x, xs) for constructing a stream with
first element x and (unevaluated) rest xs.

Those. this shows that the argument on the right must be evaluated before the #:: method is executed on it. At the same time, it is also obvious that this does not happen and somehow the Stream.cons(x, xs) method is called instead, which already allows you to postpone the calculations.

This transition is something I still don't understand. How is one method replaced by another?
And another separate question, is there any way to see which methods were called in the end, so that it would be easier to track such conversions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
btd, 2013-09-23
@cs0ip

6.12.3 has nothing to do with #::. It's more about list operations ::, :::. In scala, if a statement starts with: it automatically becomes right associative and is executed from the right argument.

1 :: 2 :: 3 :: Nil // List[Int]().::(3).::(2).::(1)

Looking at the code, #:: is just a synonym for cons:
github.com/scala/scala/blob/v2.10.2/src/library/scala/collection/immutable/Stream.scala#L1042

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question