Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I match on a Stream?

Tags:

scala

Specifically:

scala> def f(n: Seq[Any]) = n match { 
     case Nil => "Empty" 
     case h :: t => "Non-empty" 
}
f: (n: Seq[Any])String

scala> f(Stream())
res1: String = Empty

scala> f(List(1))
res17: String = Non-empty

scala> f(Stream(1))
scala.MatchError: Stream(1, ?) (of class scala.collection.immutable.Stream$Cons)
  at .f(<console>:13)
  ... 33 elided

There are a lot of other ways to implement this, but at written the code was statically safe and failed at run time. What's going on?

like image 457
Michael Lorton Avatar asked Mar 02 '17 01:03

Michael Lorton


People also ask

Why is streaming not working?

Internet buffering problems are usually caused by one of three issues. Your internet connection is too slow to keep up with the incoming data. The streaming provider can't send your device the data it needs fast enough. Your home Wi-Fi network is slowing things down.

How many subs do you need to go live on YouTube?

After verification, it will take 24 hours for your account to be activated for livestreaming. Once activated, you can livestream from desktop with any number of subscribers, but you need to have at least 1,000 subscribers to livestream from mobile.

Are streamed content only limited to videos?

The term "streaming media" can apply to media other than video and audio, such as live closed captioning, ticker tape, and real-time text, which are all considered "streaming text". Streaming is most prevalent in video on demand and streaming television services. Other services stream music or video games.


1 Answers

For Stream, the concat symbol should be #::, the pattern match should like:

  def f(n: Seq[Any]) = n match {
    case Nil => "Empty"
    case h :: t => "Non-empty"
    case h #:: t => "Non-empty stream"
  }

for :: is for List / Seq type(List extends from Seq:) ), see:

final case class ::[B](override val head: B, private[scala] var tl: List[B]) extends List[B] {
like image 74
chengpohi Avatar answered Oct 23 '22 03:10

chengpohi