# on 16-Aug-2014 (Sat)

#### Flashcard 149629021

Tags
#bonds #duration #finance
Question
For modified duration the common units are the percent change in price per one percentage point change in yield per year (for example yield going from 8% per year (y = 0.08) to 9% per year (y = 0.09)). This will give modified duration close to the value of [...]
Macaulay duration (and the same when rates are continuously compounded)

status measured difficulty not learned 37% [default] 0

#### Parent (intermediate) annotation

Open it
uration the common units are the percent change in price per one percentage point change in yield per year (for example yield going from 8% per year (y = 0.08) to 9% per year (y = 0.09)). This will give modified duration close to the value of <span>Macaulay duration (and the same when rates are continuously compounded)<span><body><html>

#### Original toplevel document

Bond duration - Wikipedia, the free encyclopedia
average time until repayment (measured in units of time such as years) while modified duration is a price sensitivity measure when the price is treated as a function of yield, the percentage change in price with respect to yield. Units <span>For modified duration the common units are the percent change in price per one percentage point change in yield per year (for example yield going from 8% per year (y = 0.08) to 9% per year (y = 0.09)). This will give modified duration close to the value of Macaulay duration (and the same when rates are continuously compounded). Formally, modified duration is a semi-elasticity, the percent change in price for a unit change in yield, rather than an elasticity, which is a percentage change in output for a percen

#### Annotation 149629100

 #anonymous-functions #functions #scala The following expression creates a successor function for integers: var inc = (x:Int) => x+1  Variable inc is now a function that can be used the usual way: var x = inc(7)-1

Scala Anonymous Functions
ts called function values. Scala supports first-class functions, which means you can express functions in function literal syntax, i.e., (x: Int) => x + 1, and that functions can be represented by objects, which are called function values. <span>The following expression creates a successor function for integers: var inc = (x:Int) => x+1 Variable inc is now a function that can be used the usual way: var x = inc(7)-1 It is also possible to define functions with multiple parameters as follows: var mul = (x: Int, y: Int) => x*y Variable mul is now a function that can be used the usual way: println(

#### Annotation 149629117

 #currying #functions #scala Curried functions are defined with multiple parameter lists, as follows: def strcat(s1: String)(s2: String) = s1 + s2  Alternatively, you can also use the following syntax to define a curried function: def strcat(s1: String) = (s2: String) => s1 + s2

Scala Currying Functions
esume WritingComputer GlossaryWho is Who Scala Currying Functions Advertisements Previous Page Next Page Currying transforms a function that takes multiple parameters into a chain of functions, each taking a single parameter. <span>Curried functions are defined with multiple parameter lists, as follows: def strcat(s1: String)(s2: String) = s1 + s2 Alternatively, you can also use the following syntax to define a curried function: def strcat(s1: String) = (s2: String) => s1 + s2 Following is the syntax to call a curried function: strcat("foo")("bar") You can define more than two parameters on a curried function based on your requirement. Let

#### Annotation 149629124

 #currying #functions #scala syntax to call a curried function: strcat("foo")("bar")

Scala Currying Functions
ith multiple parameter lists, as follows: def strcat(s1: String)(s2: String) = s1 + s2 Alternatively, you can also use the following syntax to define a curried function: def strcat(s1: String) = (s2: String) => s1 + s2 Following is the <span>syntax to call a curried function: strcat("foo")("bar") You can define more than two parameters on a curried function based on your requirement. Let us take a complete example to show currying concept: object Test { def main(args: Array[St

#### Annotation 149629141

 #collections #lists #scala lists are immutable

Scala Lists
Computer GlossaryWho is Who Scala Lists Advertisements Previous Page Next Page Scala Lists are quite similar to arrays which means, all the elements of a list have the same type but there are two important differences. First, <span>lists are immutable, which means elements of a list cannot be changed by assignment. Second, lists represent a linked list whereas arrays are flat. The type of a list that has elements of type T is written

#### Annotation 149629148

 #collections #lists #scala lists represent a linked list

Scala Lists
Scala Lists are quite similar to arrays which means, all the elements of a list have the same type but there are two important differences. First, lists are immutable, which means elements of a list cannot be changed by assignment. Second, <span>lists represent a linked list whereas arrays are flat. The type of a list that has elements of type T is written as List[T]. For example, here are few lists defined for various data types: // List of Strings val fru

#### Annotation 149629155

 #collections #lists #scala All lists can be defined using two fundamental building blocks, a tail Nil and ::, which is pronounced cons. Nil also represents the empty list. All the above lists can be defined as follows: // List of Strings val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))

Scala Lists
t;) // List of Integers val nums: List[Int] = List(1, 2, 3, 4) // Empty List. val empty: List[Nothing] = List() // Two dimensional list val dim: List[List[Int]] = List( List(1, 0, 0), List(0, 1, 0), List(0, 0, 1) ) <span>All lists can be defined using two fundamental building blocks, a tail Nil and ::, which is pronounced cons. Nil also represents the empty list. All the above lists can be defined as follows: // List of Strings val fruit = "apples" :: ("oranges" :: ("pears" :: Nil)) // List of Integers val nums = 1 :: (2 :: (3 :: (4 :: Nil))) // Empty List. val empty = Nil // Two dimensional list val dim = (1 :: (0 :: (0 :: Nil))) :: (0 :: (1 :: (0 :: Ni

#### Annotation 149629162

#collections #lists #scala

## Concatenating Lists:

You can use either ::: operator or List.:::() method or List.concat() method to add two or more lists.

Scala Lists
ve code is compiled and executed, it produces the following result: C:/>scalac Test.scala C:/>scala Test Head of fruit : apples Tail of fruit : List(oranges, pears) Check if fruit is empty : false Check if nums is empty : true C:/> <span>Concatenating Lists: You can use either ::: operator or List.:::() method or List.concat() method to add two or more lists. Following is the example: object Test { def main(args: Array[String]) { val fruit1 = "apples" :: ("oranges" :: ("pears" :: Nil)) val fruit2 =

#### Annotation 149629179

 #collections #scala #sets By default, Scala uses the immutable Set. If you want to use the mutable Set, you'll have to import scala.collection.mutable.Set class explicitly.

Scala Sets
Set is a collection that contains no duplicate elements. There are two kinds of Sets, the immutable and the mutable. The difference between mutable and immutable objects is that when an object is immutable, the object itself can't be changed. <span>By default, Scala uses the immutable Set. If you want to use the mutable Set, you'll have to import scala.collection.mutable.Set class explicitly. If you want to use both mutable and immutable sets in the same, then you can continue to refer to the immutable Set as Set but you can refer to the mutable Set as mutable.Set. Following

#### Annotation 149629186

 #collections #scala #sets If you want to use both mutable and immutable sets in the same, then you can continue to refer to the immutable Set as Set but you can refer to the mutable Set as mutable.Set.

Scala Sets
and immutable objects is that when an object is immutable, the object itself can't be changed. By default, Scala uses the immutable Set. If you want to use the mutable Set, you'll have to import scala.collection.mutable.Set class explicitly. <span>If you want to use both mutable and immutable sets in the same, then you can continue to refer to the immutable Set as Set but you can refer to the mutable Set as mutable.Set. Following is the example to declare immutable Sets as follows: // Empty set of integer type var s : Set[Int] = Set() // Set of integer type var s : Set[Int] = Set(1,3,5,7) or var s =

#### Annotation 149629193

#collections #scala #sets

## Concatenating Sets:

You can use either ++ operator or Set.++() method to concatenate two or more sets,

Scala Sets
ove code is compiled and executed, it produces the following result: C:/>scalac Test.scala C:/>scala Test Head of fruit : apples Tail of fruit : Set(oranges, pears) Check if fruit is empty : false Check if nums is empty : true C:/> <span>Concatenating Sets: You can use either ++ operator or Set.++() method to concatenate two or more sets, but while adding sets it will remove duplicate elements. Following is the example to concatenate two sets: object Test { def main(args: Array[String]) { val fruit1 = Set("a

#### Annotation 149629210

 #collections #maps #scala By default, Scala uses the immutable Map. If you want to use the mutable Set, you'll have to import scala.collection.mutable.Map class explicitly.

Scala Maps
es need not be unique. Maps are also called Hash tables. There are two kinds of Maps, the immutable and the mutable. The difference between mutable and immutable objects is that when an object is immutable, the object itself can't be changed. <span>By default, Scala uses the immutable Map. If you want to use the mutable Set, you'll have to import scala.collection.mutable.Map class explicitly. If you want to use both mutable and immutable Maps in the same, then you can continue to refer to the immutable Map as Map but you can refer to the mutable set as mutable.Map. Following

#### Annotation 149629217

 #collections #maps #scala If you want to use both mutable and immutable Maps in the same, then you can continue to refer to the immutable Map as Map but you can refer to the mutable set as mutable.Map.

Scala Maps
and immutable objects is that when an object is immutable, the object itself can't be changed. By default, Scala uses the immutable Map. If you want to use the mutable Set, you'll have to import scala.collection.mutable.Map class explicitly. <span>If you want to use both mutable and immutable Maps in the same, then you can continue to refer to the immutable Map as Map but you can refer to the mutable set as mutable.Map. Following is the example to declare immutable Maps as follows: // Empty hash table whose keys are strings and values are integers: var A:Map[Char,Int] = Map() // A map with keys and val

#### Annotation 149629224

 #collections #maps #scala If we want to add a key-value pair to a Map, we can use the operator + as follows: A += ('I' -> 1) A += ('J' -> 5) A += ('K' -> 10) A += ('L' -> 100)

Scala Maps
h keys and values. val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF") While defining empty map, the type annotation is necessary as the system needs to assign a concrete type to variable. <span>If we want to add a key-value pair to a Map, we can use the operator + as follows: A += ('I' -> 1) A += ('J' -> 5) A += ('K' -> 10) A += ('L' -> 100) Basic Operations on Map: All operations on maps can be expressed in terms of the following three methods: MethodsDescriptionkeysThis method returns an iterable containing each key in the

#### Annotation 149629231

 #collections #maps #scala example to declare immutable Maps as follows: // Empty hash table whose keys are strings and values are integers: var A:Map[Char,Int] = Map() // A map with keys and values. val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF")

Scala Maps
rt scala.collection.mutable.Map class explicitly. If you want to use both mutable and immutable Maps in the same, then you can continue to refer to the immutable Map as Map but you can refer to the mutable set as mutable.Map. Following is the <span>example to declare immutable Maps as follows: // Empty hash table whose keys are strings and values are integers: var A:Map[Char,Int] = Map() // A map with keys and values. val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF") While defining empty map, the type annotation is necessary as the system needs to assign a concrete type to variable. If we want to add a key-value pair to a Map, we can use the operator

#### Annotation 149629238

#collections #maps #scala

## Concatenating Maps

You can use either ++ operator or Map.++() method to concatenate two or more Maps,

Scala Maps
it produces the following result: C:/>scalac Test.scala C:/>scala Test Keys in colors : Set(red, azure, peru) Values in colors : MapLike(#FF0000, #F0FFFF, #CD853F) Check if colors is empty : false Check if nums is empty : true C:/> <span>Concatenating Maps You can use either ++ operator or Map.++() method to concatenate two or more Maps, but while adding Maps it will remove duplicate keys. Following is the example to concatenate two Maps: object Test { def main(args: Array[String]) { val colors1 = Map("red&

#### Annotation 149629255

 #collections #scala #tuples Scala tuple combines a fixed number of items together so that they can be passed around as a whole.

Scala Tuples
Scala - Files I/O Scala Useful ResourcesScala Quick GuideScala Useful Resources Selected ReadingDeveloper's Best PracticesEffective Resume WritingComputer GlossaryWho is Who Scala Tuples Advertisements Previous Page Next Page <span>Scala tuple combines a fixed number of items together so that they can be passed around as a whole. Unlike an array or list, a tuple can hold objects with different types but they are also immutable. Here is an example of a tuple holding an integer, a string, and the console: val t = (

#### Annotation 149629262

 #collections #scala #tuples Unlike an array or list, a tuple can hold objects with different types but they are also immutable.

Scala Tuples
loper's Best PracticesEffective Resume WritingComputer GlossaryWho is Who Scala Tuples Advertisements Previous Page Next Page Scala tuple combines a fixed number of items together so that they can be passed around as a whole. <span>Unlike an array or list, a tuple can hold objects with different types but they are also immutable. Here is an example of a tuple holding an integer, a string, and the console: val t = (1, "hello", Console) Which is syntactic sugar (short cut) for the following: val t = new

#### Annotation 149629269

 #collections #scala #tuples Here is an example of a tuple holding an integer, a string, and the console: val t = (1, "hello", Console)  Which is syntactic sugar (short cut) for the following: val t = new Tuple3(1, "hello", Console)

Scala Tuples
ements Previous Page Next Page Scala tuple combines a fixed number of items together so that they can be passed around as a whole. Unlike an array or list, a tuple can hold objects with different types but they are also immutable. <span>Here is an example of a tuple holding an integer, a string, and the console: val t = (1, "hello", Console) Which is syntactic sugar (short cut) for the following: val t = new Tuple3(1, "hello", Console) The actual type of a tuple depends upon the number and of elements it contains and the types of those elements. Thus, the type of (99, "Luftballons") is Tuple2[Int, String]. Th

#### Annotation 149629276

 #collections #scala #tuples The actual type of a tuple depends upon the number and of elements it contains and the types of those elements. Thus, the type of (99, "Luftballons") is Tuple2[Int, String]. The type of ('u', 'r', "the", 1, 4, "me") is Tuple6[Char, Char, String, Int, Int, String]

Scala Tuples
also immutable. Here is an example of a tuple holding an integer, a string, and the console: val t = (1, "hello", Console) Which is syntactic sugar (short cut) for the following: val t = new Tuple3(1, "hello", Console) <span>The actual type of a tuple depends upon the number and of elements it contains and the types of those elements. Thus, the type of (99, "Luftballons") is Tuple2[Int, String]. The type of ('u', 'r', "the", 1, 4, "me") is Tuple6[Char, Char, String, Int, Int, String] Tuples are of type Tuple1, Tuple2, Tuple3 and so on. There currently is an upper limit of 22 in the Scala if you need more, then you can use a collection, not a tuple. For each TupleN ty

#### Annotation 149629283

 #collections #scala #tuples Tuples are of type Tuple1, Tuple2, Tuple3 and so on. There currently is an upper limit of 22 in the Scala if you need more, then you can use a collection, not a tuple.

Scala Tuples
and of elements it contains and the types of those elements. Thus, the type of (99, "Luftballons") is Tuple2[Int, String]. The type of ('u', 'r', "the", 1, 4, "me") is Tuple6[Char, Char, String, Int, Int, String] <span>Tuples are of type Tuple1, Tuple2, Tuple3 and so on. There currently is an upper limit of 22 in the Scala if you need more, then you can use a collection, not a tuple. For each TupleN type, where 1 <= N <= 22, Scala defines a number of element-access methods. Given the following definition: val t = (4,3,2,1) To access elements of a tuple t, you

#### Annotation 149629290

 #collections #scala #tuples Given the following definition: val t = (4,3,2,1)  To access elements of a tuple t, you can use method t._1 to access the first element, t._2 to access the second, and so on. For example, the following expression computes the sum of all elements of t: val sum = t._1 + t._2 + t._3 + t._4

Scala Tuples
e1, Tuple2, Tuple3 and so on. There currently is an upper limit of 22 in the Scala if you need more, then you can use a collection, not a tuple. For each TupleN type, where 1 <= N <= 22, Scala defines a number of element-access methods. <span>Given the following definition: val t = (4,3,2,1) To access elements of a tuple t, you can use method t._1 to access the first element, t._2 to access the second, and so on. For example, the following expression computes the sum of all elements of t: val sum = t._1 + t._2 + t._3 + t._4 You can use Tupel to write a method that takes a List[Double] and returns the count, the sum, and the sum of squares returned in a three-element Tuple, a Tuple3[Int, Double, Double]. The

#### Annotation 149629307

 #collections #options #scala Scala Option[T] is a container for zero or one element of a given type.

Scala Options
cala - Files I/O Scala Useful ResourcesScala Quick GuideScala Useful Resources Selected ReadingDeveloper's Best PracticesEffective Resume WritingComputer GlossaryWho is Who Scala Options Advertisements Previous Page Next Page <span>Scala Option[T] is a container for zero or one element of a given type. An Option[T] can be either Some[T] or None object, which represents a missing value. For instance, the get method of Scala's Map produces Some(value) if a value corresponding to a given

#### Annotation 149629314

 #collections #options #scala An Option[T] can be either Some[T] or None object, which represents a missing value.

Scala Options
ources Selected ReadingDeveloper's Best PracticesEffective Resume WritingComputer GlossaryWho is Who Scala Options Advertisements Previous Page Next Page Scala Option[T] is a container for zero or one element of a given type. <span>An Option[T] can be either Some[T] or None object, which represents a missing value. For instance, the get method of Scala's Map produces Some(value) if a value corresponding to a given key has been found, or None if the given key is not defined in the Map. The Option ty

#### Annotation 149629321

 #collections #options #scala use getOrElse() to access a value or a default when no value is present:

Scala Options
produces the following result: C:/>scalac Test.scala C:/>scala Test show(capitals.get( "Japan")) : Tokyo show(capitals.get( "India")) : ? C:/> Using getOrElse() Method: Following is the example of showing how to <span>use getOrElse() to access a value or a default when no value is present: object Test { def main(args: Array[String]) { val a:Option[Int] = Some(5) val b:Option[Int] = None println("a.getOrElse(0): " + a.getOrElse(0) )

#### Annotation 149629338

 #classes #scala in Scala we cannot have static members. Instead, Scala has singleton objects.

Scala Classes &amp; Objects
compiled and executed, it produces the following result: C:/>scalac Test.scala C:/>scala Test Point x location : 20 Point y location : 30 Point z location : 20 C:/> Singleton objects: Scala is more object-oriented than Java because <span>in Scala we cannot have static members. Instead, Scala has singleton objects. A singleton is a class that can have only one instance, i.e., object. You create singleton using the keyword object instead of class keyword. Since you can't instantiate a singleton obje

#### Annotation 149629345

 #classes #scala A singleton is a class that can have only one instance, i.e., object. You create singleton using the keyword object instead of class keyword.

Scala Classes &amp; Objects
ala C:/>scala Test Point x location : 20 Point y location : 30 Point z location : 20 C:/> Singleton objects: Scala is more object-oriented than Java because in Scala we cannot have static members. Instead, Scala has singleton objects. <span>A singleton is a class that can have only one instance, i.e., object. You create singleton using the keyword object instead of class keyword. Since you can't instantiate a singleton object, you can't pass parameters to the primary constructor. You already have seen all the examples using singleton objects where you called Scal

#### Annotation 149629352

 #classes #scala Since you can't instantiate a singleton object, you can't pass parameters to the primary constructor.

Scala Classes &amp; Objects
nted than Java because in Scala we cannot have static members. Instead, Scala has singleton objects. A singleton is a class that can have only one instance, i.e., object. You create singleton using the keyword object instead of class keyword. <span>Since you can't instantiate a singleton object, you can't pass parameters to the primary constructor. You already have seen all the examples using singleton objects where you called Scala's main method. Following is the same example of showing singleton: import java.io._ class Point(val

#### Annotation 149629359

 #classes #scala You already have seen all the examples using singleton objects where you called Scala's main method.

Scala Classes &amp; Objects
singleton is a class that can have only one instance, i.e., object. You create singleton using the keyword object instead of class keyword. Since you can't instantiate a singleton object, you can't pass parameters to the primary constructor. <span>You already have seen all the examples using singleton objects where you called Scala's main method. Following is the same example of showing singleton: import java.io._ class Point(val xc: Int, val yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) {

#### Annotation 149629376

 #scala #traits A trait encapsulates method and field definitions, which can then be reused by mixing them into classes.

Scala Traits
Scala - Files I/O Scala Useful ResourcesScala Quick GuideScala Useful Resources Selected ReadingDeveloper's Best PracticesEffective Resume WritingComputer GlossaryWho is Who Scala Traits Advertisements Previous Page Next Page <span>A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. Unlike class inheritance, in which each class must inherit from just one superclass, a class can mix in any number of traits. Traits are used to define object types by specifying the sig

#### Annotation 149629383

 #scala #traits Unlike class inheritance, in which each class must inherit from just one superclass, a class can mix in any number of traits.

Scala Traits
's Best PracticesEffective Resume WritingComputer GlossaryWho is Who Scala Traits Advertisements Previous Page Next Page A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. <span>Unlike class inheritance, in which each class must inherit from just one superclass, a class can mix in any number of traits. Traits are used to define object types by specifying the signature of the supported methods. Scala also allows traits to be partially implemented but traits may not have constructor para

#### Annotation 149629390

 #scala #traits traits may not have constructor parameters (also called class parameters)

Scala Traits
h each class must inherit from just one superclass, a class can mix in any number of traits. Traits are used to define object types by specifying the signature of the supported methods. Scala also allows traits to be partially implemented but <span>traits may not have constructor parameters. A trait definition looks just like a class definition except that it uses the keyword trait as follows: trait Equal { def isEqual(x: Any): Boolean def isNotEqual(x: Any): Boolean = !

#### Annotation 149629397

 #scala #traits Child classes extending a trait can give implementation for the un-implemented methods.

Scala Traits
def isEqual(x: Any): Boolean def isNotEqual(x: Any): Boolean = !isEqual(x) } This trait consists of two methods isEqual and isNotEqual. Here, we have not given any implementation for isEqual where as another method has its implementation. <span>Child classes extending a trait can give implementation for the un-implemented methods. So a trait is very similar to what we have abstract classes in Java. Below is a complete example to show the concept of traits: trait Equal { def isEqual(x: Any): Boolean def isNotEq

#### Annotation 149629404

 #scala #traits a trait is very similar to what we have abstract classes in Java.

Scala Traits
consists of two methods isEqual and isNotEqual. Here, we have not given any implementation for isEqual where as another method has its implementation. Child classes extending a trait can give implementation for the un-implemented methods. So <span>a trait is very similar to what we have abstract classes in Java. Below is a complete example to show the concept of traits: trait Equal { def isEqual(x: Any): Boolean def isNotEqual(x: Any): Boolean = !isEqual(x) } class Point(xc: Int, yc: Int) e

#### Annotation 149629421

 #regex #scala val pattern = "Scala".r

Scala Regular Expressions
ions through Regex class available in the scala.util.matching package. Let us check an example where we will try to find out word Scala from a statement: import scala.util.matching.Regex object Test { def main(args: Array[String]) { <span>val pattern = "Scala".r val str = "Scala is Scalable and cool" println(pattern findFirstIn str) } } When the above code is compiled and executed, it produces the following result: C:/

#### Annotation 149629428

 #regex #scala We create a String and call the r( ) method on it. Scala implicitly converts the String to a RichString and invokes that method to get an instance of Regex.

Scala Regular Expressions
tr = "Scala is Scalable and cool" println(pattern findFirstIn str) } } When the above code is compiled and executed, it produces the following result: C:/>scalac Test.scala C:/>scala Test Some(Scala) C:/> <span>We create a String and call the r( ) method on it. Scala implicitly converts the String to a RichString and invokes that method to get an instance of Regex. To find a first match of the regular expression, simply call the findFirstIn() method. If instead of finding only the first occurrence we would like to find all occurrences of the matchi

#### Annotation 149629445

 #extractor-objects #scala An extractor in Scala is an object that has a method called unapply as one of its members.

Scala Extractors
a - Files I/O Scala Useful ResourcesScala Quick GuideScala Useful Resources Selected ReadingDeveloper's Best PracticesEffective Resume WritingComputer GlossaryWho is Who Scala Extractors Advertisements Previous Page Next Page <span>An extractor in Scala is an object that has a method called unapply as one of its members. The purpose of that unapply method is to match a value and take it apart. Often, the extractor object also defines a dual method apply for building values, but this is not required. Foll

#### Annotation 149629452

 #extractor-objects #scala The purpose of that unapply method is to match a value and take it apart.

Scala Extractors
gDeveloper's Best PracticesEffective Resume WritingComputer GlossaryWho is Who Scala Extractors Advertisements Previous Page Next Page An extractor in Scala is an object that has a method called unapply as one of its members. <span>The purpose of that unapply method is to match a value and take it apart. Often, the extractor object also defines a dual method apply for building values, but this is not required. Following example shows an extractor object for email addresses: object Test {

#### Annotation 149629459

 #extractor-objects #scala Often, the extractor object also defines a dual method apply for building values, but this is not required.

Scala Extractors
Who Scala Extractors Advertisements Previous Page Next Page An extractor in Scala is an object that has a method called unapply as one of its members. The purpose of that unapply method is to match a value and take it apart. <span>Often, the extractor object also defines a dual method apply for building values, but this is not required. Following example shows an extractor object for email addresses: object Test { def main(args: Array[String]) { println ("Apply method : " + apply("Zara&quo

#### Annotation 149629466

 #extractor-objects #scala  // The injection method (optional) def apply(user: String, domain: String) = { user +"@"+ domain } // The extraction method (mandatory) def unapply(str: String): Option[(String, String)] = { val parts = str split "@" if (parts.length == 2) { Some(parts(0), parts(1)) } else{ None } }