.filter(_.nonEmpty).map(_.get)

2,018 views
Skip to first unread message

Andy C

unread,
Jun 3, 2013, 8:27:15 PM6/3/13
to scala-user
Hi,
I wonder if there is a shortcut for ".filter(_.nonEmpty).map(_.get)". I quite often need to create a list of objects of a initial sequence of data where not all elements results in newly created objects (I use None for that). At that point I want to filter out "weeds" and expose "fruits" from such a sequence. I currently use ".filter(_.nonEmpty).map(_.get)" but wonder if there is a better way of achieving it.

Thanks in advance,
Andy


Haoyi Li

unread,
Jun 3, 2013, 8:32:46 PM6/3/13
to Andy C, scala-user
Doesn't .flatten do this for you? E.g. It takes seq[option[t]] and turns it into seq[t] iirc

Sent from my Windows Phone

From: Andy C
Sent: 6/3/2013 8:27 PM
To: scala-user
Subject: [scala-user] .filter(_.nonEmpty).map(_.get)

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Vlad Patryshev

unread,
Jun 3, 2013, 8:37:44 PM6/3/13
to Andy C, scala-user
If you are using 2.8, .flatMap(_.toList); otherwise just .flatten

Thanks,
-Vlad


Brian Maso

unread,
Jun 3, 2013, 8:46:10 PM6/3/13
to Andy C, scala-user (ggroups)
collect { case Some(x) => x } also works, though I like flatten better.



On Mon, Jun 3, 2013 at 5:27 PM, Andy C <andy.c...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Best regards,
Brian Maso
(949) 395-8551
Follow me: @bmaso
br...@blumenfeld-maso.com

Som Snytt

unread,
Jun 3, 2013, 8:50:13 PM6/3/13
to Andy C, scala-user
filter out "weeds"

My contribution is that the usual word is "chaff", but "tares" will convey "weeds" more specifically.

To wit,
http://en.wikipedia.org/wiki/Chaff
http://en.wikipedia.org/wiki/Parable_of_the_Tares

"flatten" really does mean to remove the "chaff", or useless husk that encases the wheat (when there is a berry, or None otherwise).




On Mon, Jun 3, 2013 at 5:27 PM, Andy C <andy.c...@gmail.com> wrote:

Odd Möller

unread,
Jun 6, 2013, 3:00:56 PM6/6/13
to Andy C, scala-user
I would suggest .collect { case Some(x) => x.get } for this, since it combines the filtering and the maping into one sweet operation.

Greetings
Odd Möller

Alec Zorab

unread,
Jun 6, 2013, 3:11:05 PM6/6/13
to Odd Möller, Andy C, scala-user
.collect { case Some(x) => x}, surely?

Odd Möller

unread,
Jun 6, 2013, 3:16:53 PM6/6/13
to Alec Zorab, Andy C, scala-user
Of course :)

Thanks!
//Odd

Alexandru Nedelcu

unread,
Jun 7, 2013, 7:58:57 AM6/7/13
to Andy C, scala-user
On Tue, Jun 4, 2013 at 3:27 AM, Andy C <andy.c...@gmail.com> wrote:
I wonder if there is a shortcut for ".filter(_.nonEmpty).map(_.get)". I quite often need to create a list of objects of a initial sequence of data where not all elements results in newly created objects (I use None for that). At that point I want to filter out "weeds" and expose "fruits" from such a sequence. I currently use ".filter(_.nonEmpty).map(_.get)" but wonder if there is a better way of achieving it.


All alternatives:

  scala> Seq(Some(1), Some(2), None).filter(_.nonEmpty).map(_.get)
  res1: Seq[Int] = List(1, 2)

  scala> Seq(Some(1), Some(2), None).foldLeft(Seq.empty[Int])((a,e) => e.map(a :+ _).getOrElse(a))
  res2: Seq[Int] = List(1, 2)

  scala> Seq(Some(1), Some(2), None).collect { case Some(x) => x }
  res3: Seq[Int] = List(1, 2)

  scala> for (opt <- Seq(Some(1), Some(2), None); e <- opt) yield e
  res4: Seq[Int] = List(1, 2)

  scala> Seq(Some(1), Some(2), None).flatMap(x => x)
  res5: Seq[Int] = List(1, 2)

  scala> Seq(Some(1), Some(2), None).flatten
  res6: Seq[Int] = List(1, 2)


Reply all
Reply to author
Forward
0 new messages