Convert match statement to pattern matching anonymous function

1,612 views
Skip to first unread message

Oliver Ruebenacker

unread,
Jun 16, 2016, 12:55:37 PM6/16/16
to scala-user

     Hello,

  I have something a bit like this:

    val list = 1 to 10
    val list2 = list.map(i => i match {
      case 2 | 4 | 6 => i / 2
      case 3 | 9 => i / 3
      case _ => i
    })

  IntelliJ says I should "Convert match statement to pattern matching anonymous function". How would I do that? Thanks!

     Best, Oliver

--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Kevin Wright

unread,
Jun 16, 2016, 1:14:47 PM6/16/16
to Oliver Ruebenacker, scala-user
It means:

  val list = 1 to 10
  val list2 = list map {

Alexey Shuksto

unread,
Jun 16, 2016, 1:41:36 PM6/16/16
to scala-user
Also, you can just click inspection lightbulb or hit Alt-Enter and IDEA will convert it for you (as with many more code inspections).

Clint Gilbert

unread,
Jun 16, 2016, 1:49:30 PM6/16/16
to scala...@googlegroups.com
Aside: the reason this works is that a PartialFunction extends
Function1, and not the other way around. There are pros and cons to
this; the convenient syntax Kevin showed is one of the big pros.

On 06/16/2016 01:14 PM, Kevin Wright wrote:
> It means:
>
> val list = 1 to 10
> val list2 = list map {
> case 2 | 4 | 6 => i / 2
> case 3 | 9 => i / 3
> case _ => i
> }
>
>
>
> On 16 June 2016 at 17:55, Oliver Ruebenacker <cur...@gmail.com
> <mailto:cur...@gmail.com>> wrote:
>
>
> Hello,
>
> I have something a bit like this:
>
> val list = 1 to 10
> val list2 = list.map(i => i match {
> case 2 | 4 | 6 => i / 2
> case 3 | 9 => i / 3
> case _ => i
> })
>
> IntelliJ says I should "Convert match statement to pattern
> matching anonymous function". How would I do that? Thanks!
>
> Best, Oliver
>
> --
> Oliver Ruebenacker
> Senior Software Engineer, Diabetes Portal
> <http://www.type2diabetesgenetics.org/>, Broad Institute
> <http://www.broadinstitute.org/>
>
>
>
> --
> 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
> <mailto:scala-user+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--



signature.asc

Oliver Ruebenacker

unread,
Jun 16, 2016, 1:55:04 PM6/16/16
to Kevin Wright, scala-user
Well, "Cannot resolve symbol i".

Clint Gilbert

unread,
Jun 16, 2016, 2:09:42 PM6/16/16
to scala...@googlegroups.com
It's just a pattern match, like any other. If you need access to the
things that matched, name them with '@'. If that's not convenient, use
the i => i match { ... } form.

On 06/16/2016 01:54 PM, Oliver Ruebenacker wrote:
> Well, "Cannot resolve symbol i".
>
> On Thu, Jun 16, 2016 at 1:14 PM, Kevin Wright <kev.lee...@gmail.com
> <mailto:kev.lee...@gmail.com>> wrote:
>
> It means:
>
> val list = 1 to 10
> val list2 = list map {
> case 2 | 4 | 6 => i / 2
> case 3 | 9 => i / 3
> case _ => i
> }
>
>
>
> On 16 June 2016 at 17:55, Oliver Ruebenacker <cur...@gmail.com
> <mailto:cur...@gmail.com>> wrote:
>
>
> Hello,
>
> I have something a bit like this:
>
> val list = 1 to 10
> val list2 = list.map(i => i match {
> case 2 | 4 | 6 => i / 2
> case 3 | 9 => i / 3
> case _ => i
> })
>
> IntelliJ says I should "Convert match statement to pattern
> matching anonymous function". How would I do that? Thanks!
>
> Best, Oliver
>
> --
> Oliver Ruebenacker
> Senior Software Engineer, Diabetes Portal
> <http://www.type2diabetesgenetics.org/>, Broad Institute
> <http://www.broadinstitute.org/>
>
>
>
>
>
>
> --
> Oliver Ruebenacker
signature.asc

Serge

unread,
Jun 16, 2016, 2:12:39 PM6/16/16
to scala...@googlegroups.com
val list = 1 to 10
val list2 = list map {
  case i @ (2 | 4 | 6)  => i / 2
  case i @ (3 | 9)      => i / 3
  case i                => i
}
--
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.

Oliver Ruebenacker

unread,
Jun 16, 2016, 2:54:07 PM6/16/16
to Serge, scala-user
Awesome, thanks! I did not know about the @ feature.

Алексей Шуксто

unread,
Jun 16, 2016, 3:20:50 PM6/16/16
to Oliver Ruebenacker, Serge, scala-user

case i@(2 | 4 | 6) => i / 2


чт, 16 июня 2016, 21:54 Oliver Ruebenacker <cur...@gmail.com>:
You received this message because you are subscribed to a topic in the Google Groups "scala-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-user/Jc8H4VUhb8Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-user+...@googlegroups.com.

Kevin Wright

unread,
Jun 16, 2016, 4:06:28 PM6/16/16
to Алексей Шуксто, Oliver Ruebenacker, Serge, scala-user
I’d like to say use a for-comprehension here, unfortunately type inference doesn’t work quite as you’d want.

So using the @ bind syntax:

  val list = 1 to 10

  val list2 = list map {
    case i @ ( 2 | 4 | 6 ) => i / 2
    case i @ ( 3 | 9 )     => i / 3
    case i                 => i
  }

Or if you wish to avoid magic numbers and capture more of the intent:

  val list2 = list map {
    case i if i % 2 == 0 => i / 2
    case i if i % 3 == 0 => i / 3
    case i               => i
  }

Kevin Wright
mail / hangouts / msn : kev.lee...@gmail.com
vibe / skype: kev.lee.wright

"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
Reply all
Reply to author
Forward
0 new messages