Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Scala have a statement equivalent to ML's "as" construct?

In ML, one can assign names for each element of a matched pattern:

fun findPair n nil = NONE
| findPair n (head as (n1, _))::rest =
    if n = n1 then (SOME head) else (findPair n rest)

In this code, I defined an alias for the first pair of the list and matched the contents of the pair. Is there an equivalent construct in Scala?

like image 983
Evan Kroske Avatar asked Nov 03 '12 03:11

Evan Kroske


People also ask

Can Scala be used for ML?

This is because, thanks to its functional nature, it is a more common choice when it comes to building powerful applications that need to cooperate with a vast amount of data. Moreover, thanks to tools like Spark, Scala itself is becoming the number one choice for creating modern machine learning models.

What is construct in Scala?

Some of the basic Scala constructs are expressions, blocks, classes, objects, functions, methods, traits, Main methods, fields, and closures. 1. Scala Expression: A computable statement in Scala is known as expression.

Can Scala be used for data science?

Scala's name implies that it is a scalable programming language. It was created in 2003 by Martin Odersky and his research team. These days we widely use Scala in Data Science and Machine Learning fields. Scala is a small, fast, and efficient multi-paradigm programming language built on a compiler.

What is Scala in ML?

Scala is a highly scalable integration of object-oriented nature and functional programming concepts that make it easy to build scalable and complex big data applications.


1 Answers

You can do variable binding with the @ symbol, e.g.:

scala> val wholeList @ List(x, _*) = List(1,2,3)
wholeList: List[Int] = List(1, 2, 3)
x: Int = 1
like image 65
DaoWen Avatar answered Oct 02 '22 19:10

DaoWen