0

I have the following question: Having an Option of List of elements I want to transform it into a List of elements by avoiding the use of .get on the Option. Below is the snippet of code that I want to change : Any idea how to modify this please :

val receivedMessages = PubSubClient.getPulledMessages(client,subscriptionFullName,pullRequest).get
        store(receivedMessages
          .map(x => {
            val sm = new PubsubSparkMessage
            sm.message = x.getMessage
            sm
          })
          .iterator)

I want to replace the .get in the line PubSubClient.getPulledMessages(client,subscriptionFullName,pullRequest).get

bad_coder
  • 11,289
  • 20
  • 44
  • 72
scalacode
  • 1,096
  • 1
  • 16
  • 38

2 Answers2

0

Taking this example:

val maybeList = Some(List("hello", "here"))

When printing this:

println(maybeList) // Some(List(hello, here))

To fix this you can do this:

maybeList.toSeq.flatMap(x=> x) // List(hello, here)

Or if you prefer with for comprehension:

for{
  list <- maybeList.toSeq
  elem <- list
} yield elem

Or in short:

maybeList.toSeq.flatten

Here the Scala Fiddle if you want to play with this.

With your Example - it looks like this:

val receivedMessages = PubSubClient.getPulledMessages(client,subscriptionFullName,pullRequest)

store(receivedMessages.toSeq
      .flatMap(x => {
        val sm = new PubsubSparkMessage
        sm.message = x.getMessage
        sm
      })
pme
  • 14,156
  • 3
  • 52
  • 95
0

This function will accomplish what you want:

def listOptToList[T](listOpt: Option[List[T]): List[T] = listOpt.toList.flatten

So your code would look something like:

val receivedMessages = PubSubClient.getPulledMessages(client,subscriptionFullName,pullRequest)

store(listOptToList(receivedMessages.map { x =>
    val sm = new PubSubSparkMessage
    sm.message = x.getMessage
    sm
  }).iterator)
Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30