You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Scala (and probably Haskell and other languages) there is a concept of pattern matching. This is similar to but not the same as destructoring, and similar to but not the same as switch statements.
A basic example that looks like a switch statement:
importscala.util.Randomvalx:Int=Random.nextInt(10)
x match {
case0=>"zero"case1=>"one"case2=>"two"case _ =>"other"
}
A slightly more complex example:
defshowImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]):String= {
notification match {
caseEmail(sender, _, _) if importantPeopleInfo.contains(sender) =>"You got an email from special someone!"caseSMS(number, _) if importantPeopleInfo.contains(number) =>"You got an SMS from special someone!"case other =>
showNotification(other) // nothing special, delegate to our original showNotification function
}
}
So you have a match expression, which is something match {}, and inside the block you have cases, where on the left of the => is a pattern that you match against, and on the right is the resulting expression that gets evaluated.
it gets way more complicated than this, and you can use them in weird ways like:
scala> case class Count(count: Int)
defined class Count
scala> val Count(pulledOut) = Count(1)
pulledOut: Int = 1
scala> val counts = List(Count(1), Count(2), Count(3))
counts: List[Count] = List(Count(1), Count(2), Count(3))
scala> counts.map({case Count(c) => c})
res0: List[Int] = List(1, 2, 3)
Where you use patterns for destructoring, and can have an individual cases as partial function shorthand
We should work out what kind of language / keywords would be helpful in cursorless to let us maniuplate patterns.
The text was updated successfully, but these errors were encountered:
In Scala (and probably Haskell and other languages) there is a concept of pattern matching. This is similar to but not the same as destructoring, and similar to but not the same as switch statements.
A basic example that looks like a switch statement:
A slightly more complex example:
So you have a
match
expression, which issomething match {}
, and inside the block you have cases, where on the left of the=>
is a pattern that you match against, and on the right is the resulting expression that gets evaluated.it gets way more complicated than this, and you can use them in weird ways like:
Where you use patterns for destructoring, and can have an individual cases as partial function shorthand
We should work out what kind of language / keywords would be helpful in cursorless to let us maniuplate patterns.
The text was updated successfully, but these errors were encountered: