Pattern Matching Tricks in Scala | Rock the JVM

preview_player
Показать описание

This video is for the Scala programmer who's only been using pattern matching in its basic form: either as a switch-on-steroids, or to deconstruct case classes. I will show you 8 tricks to use PM to its full power, so that you write clean code, test exactly what you want, and save others from the pain of reading duplicates.

Follow Rock the JVM on:

-------------------------------------------------------------------------
-------------------------------------------------------------------------
Рекомендации по теме
Комментарии
Автор

Another trick is pattern matching against an interpolated string:
def greet(str: String) = str match {
case s"Hello, ${name}" => println(s"$name has joined")
case _ => println("Someone has joined")
}

artyom.shootov
Автор

Wow.. love the video. Been using Scala for a while at work and learning while I go but haven't seen these types of pattern matching tricks. Learnt a lot of value from this. thanks.

katabd
Автор

This is why I *love* rust. A low level systems language with pattern matching. Pattern. matching. is. incredibly. ergonomic.

simonfarre
Автор

Thanks for the tricks. I haven't used scala for a few years and am diving back into it. Pretty helpful!

jeremygiaco
Автор

Thank you so much for your tips and tricks. I love scala, but I find very little quality content related to it.

rheafernandeshallu
Автор

I have learned two course of you at Udemy, nice guide. Thankyou

Peter-xnbk
Автор

Awesome tutorial series. Really enjoying working out the examples alongside the video \m\

davidsljukic
Автор

Very useful in data engineering space as well for routing different data flow with some complex conditions. Liked it!

theanigos
Автор

Hi Daniel, another very good video, as usual. Albeit, with a tiny error, in the numbers list, appending the "st", "nd", ... would not work with 11 or 12... Please keep up the great work with the Rock the JVM content. Thanks

alanlewis
Автор

Would you use pattern matching when parsing stream of data from Kafka or other databases? Do you know of a good flink + Scala tutorial?

hinrikmarhreinsson
Автор

I am wondering in your alternative patterns example, could you end in "case n" instead of "case _" ... to keep the processing more visually consistent to the examples above it.

mikepictor
Автор

Great video, but i had a question, assuming I have several objects and these objects have a main function, which receive parameters something like this:
object a {def main {p1, p2} in my another object i want to pass for this object using a match case paramater match {case paramater => a{p1, p2}. Is there any way for me to parameterize this object call too, because in this case I will have several objects that depend on this parameter match case?

vitorfilho
Автор

Video is great, the blog page in the description does not work btw.

bitcoindaddy
Автор

The video is superb! Thank you so much as I'm struggling to find quality content like this. Also, it would be more favourable if you could provide the source code as well! Thank you so much!

Tangxx
Автор

Hi, I get the same error in trick 2 process method.
My Code -
def process(aList: List[Int]) = aList match
{
case Nil => "list is empty"
case head :: tail => s"list starts with $head, tail is $tail"
}



Error - process: (aList: List[Int])String

drishtinishchhalsahu
Автор

Can I use this with type literals to build a parser, instead of using antlr?

shumymikaball
Автор

Daniel,
do you planning to make videos about cats?

gijduvon
Автор

Hi Thanks for the amazing videos, i am beginner in scala. I have tried trick#2 example from above, but i am getting below error. I am practising above example in scala worksheet.


My Code -
val numberList = List(1, 2, 3, 4)


val startWithOne = numberList match {
case 1 :: tail => s"list start with one, tail is $tail"
}


Error Message -
numberList: List[Int] = List(1, 2, 3, 4)


^
warning: match may not be exhaustive.
It would fail on the following inputs: List((x: Int forSome x not in 1)), Nil
startWithOne: String = list start with one, tail is List(2, 3, 4)

drishtinishchhalsahu
Автор

Thank you for the videos, very concise and helpful, I have a situation where my stream generated column “value” has a composite number something like: 0112547.21 the first 4 digits are the ID (0112) the rest is the value 547.21 what is best to decompose the column tried to use the regex but without success and here is what I tried to do as the first item to be extracted:
Item12: Regex = "((?s)(?<=^(01)12)\\w+)".r,

Appreciate Any help and support
Thanks again

fakirrabih
Автор

what is the purpose of your main method without showing results how a beginner will learn.
Good Material with poor presentation for beginners. Your intension is people should not learn

kailar