-1

I do a group-by and I get key -> Stream(of values)

I then need to do a pattern match on the stream collection to access the last item but the pattern match doesn't work.

When I manually build the list of values using Seq the same pattern match code works

So my question is there a way to convert the Stream to Seq or List?

The IDE says toSeq is redundant

pme
  • 14,156
  • 3
  • 52
  • 95
jakstack
  • 2,143
  • 3
  • 20
  • 37
  • 1
    How are you doing the pattern match?, Which Stream are you talking about, the scala one, the java one, the akka one?, Can you please share a [**MCVE**](https://stackoverflow.com/help/reprex). - BTW, if you are trying to pattern match the stream itself using `Nil` & `x :: xs` that won't work, first, those are specific to `List`, second usually streams are not ADTs, thus they are not pattern matcheable. – Luis Miguel Mejía Suárez May 15 '19 at 14:09

2 Answers2

4

When I manually build the list of values using Seq the same pattern match code works

In scala 2.12, Seq (or sequence) are defaulted to List, see this question:

scala> Seq(1,2,3)
res3: Seq[Int] = List(1, 2, 3)

This is probably why the pattern matching works on your sequence (which happens to be a List) but not an a Stream, see this question.

The IDE says toSeq is redundant

Stream are indeed Seq:

Stream(1,2,3).toSeq
res4: collection.immutable.Seq[Int] = Stream(1, 2, 3)

So my question is there a way to convert the Stream to Seq or List?

To transform a Stream into a List, you can call the .toList method:

Stream(1,2,3).toList
res5: List[Int] = List(1, 2, 3)
Valy Dia
  • 2,781
  • 2
  • 12
  • 32
  • What collection is mostly used in scala List or Seq? – jakstack May 15 '19 at 17:24
  • 1
    `List` are a specific type of a `Seq` btw, (see [this question](https://stackoverflow.com/questions/10866639/difference-between-a-seq-and-a-list-in-scala))... So which type of `Seq` to use, really depends on your use case... `List` are quite common, and are standard, if you need an access to the head in a contant time... – Valy Dia May 15 '19 at 17:53
2

Or with this Answer you don't have to transform to List:

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

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

Make sure you reverse the Stream - so you get the last element, here an example:

n.reverse match {
    case Nil => "Empty"
    case h #:: _ => s"last element is $h"
  }

Check it here ScalaFiddle

pme
  • 14,156
  • 3
  • 52
  • 95
  • 1
    `Nil` does not work for streams, you should use `Stream.Empty`, see [this](https://stackoverflow.com/questions/28530794/why-cant-i-pattern-match-on-stream-empty-in-scala). - BTW, reversing a _possibly_ **infinite** collection does not seems like a good idea to. – Luis Miguel Mejía Suárez May 15 '19 at 14:36
  • @LuisMiguelMejíaSuárez: we pattern match to a `Seq` not `Stream` (check https://scalafiddle.io/sf/UdJCMhM/1). jakstack requirement is to match the last element - so I assumed the `Stream` is not infinite - otherwise you are right of course ;) . – pme May 15 '19 at 14:51