Scala Extractors apply, unapply and pattern matching Example

Scala extractor is an object which has a method called unapply as one of its members. The unapply method matches a value and take it apart. The extractor also defines apply method for building values.

Consider an example of extracting first name and last name that uses space as separator.

firstName.scala

The object firstName defines two methods apply and unapply. The apply method turns into an object that accepts the arguments specified within the parenthesis and builds the value as specified in the method. The first and last name combined together with a space in between is returned.

The unapply method returns firstname object into an extractor and breaks the arguments as specified in the method. The unapply methods accepts a String argument and splits the String with space as separator and returns the option type over pair of strings. The result is a pair of strings if the first name and last name is passed as an argument else returns none.

Below image shows the output produced by above scala program.

scala-extractor-example-411x450

Scala Extractor Pattern Matching

If the instance of the class contains a list of zero or more parameters, compiler calls apply method on that instance. The apply method can be defined for both objects and classes.

Consider an example as shown below;

patternmatch.scala

In the above example we are defining two classes Student and Address. Student class contains name and address and address contains city and state.

In the object City we are defining an unapply method which fetches the state from the address list. In the PatternMatch object we are creating objects of Student and address and we are fetching the names of the students who stay in texas city which is the pattern to be matched.

Below image shows the output produced when above class is executed.

scala-extractor-pattern-matching-450x404

That’s all for Scala Extractors and pattern matching, we will look into file operations in scala in next post.

By admin

Leave a Reply

%d bloggers like this: