09.10.2014 Views

Download Scala Tutorial (PDF Version) - Tutorials Point

Download Scala Tutorial (PDF Version) - Tutorials Point

Download Scala Tutorial (PDF Version) - Tutorials Point

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

def filter(p: (A) => Boolean): Option[A]<br />

Returns this Option if it is nonempty and applying the predicate p to this Option's value returns true. Otherwise, return<br />

None.<br />

def filterNot(p: (A) => Boolean): Option[A]<br />

Returns this Option if it is nonempty and applying the predicate p to this Option's value returns false. Otherwise,<br />

return None.<br />

def flatMap[B](f: (A) => Option[B]): Option[B]<br />

Returns the result of applying f to this Option's value if this Option is nonempty. Returns None if this Option is empty.<br />

def foreach[U](f: (A) => U): Unit<br />

Apply the given procedure f to the option's value, if it is nonempty. Otherwise, do nothing.<br />

def getOrElse[B >: A](default: => B): B<br />

Returns the option's value if the option is nonempty, otherwise return the result of evaluating default.<br />

def isDefined: Boolean<br />

Returns true if the option is an instance of Some, false otherwise.<br />

def iterator: Iterator[A]<br />

Returns a singleton iterator returning the Option's value if it is nonempty, or an empty iterator if the option is empty.<br />

def map[B](f: (A) => B): Option[B]<br />

Returns a Some containing the result of applying f to this Option's value if this Option is nonempty. Otherwise return<br />

None.<br />

def orElse[B >: A](alternative: => Option[B]): Option[B]<br />

Returns this Option if it is nonempty, otherwise return the result of evaluating alternative.<br />

15<br />

def orNull<br />

Returns the option's value if it is nonempty, or null if it is empty.<br />

<strong>Scala</strong> Iterators<br />

An iterator is not a collection, but rather a way to access the elements of a collection one by one. The two basic<br />

operations on an iterator it are next and hasNext. A call to it.next() will return the next element of the iterator and<br />

advance the state of the iterator. You can find out whether there are more elements to return using<br />

Iterator's it.hasNext method.<br />

The most straightforward way to "step through" all the elements returned by an iterator is to use a while loop. Let us<br />

see a real example:<br />

object Test {<br />

def main(args: Array[String]) {<br />

val it = Iterator("a", "number", "of", "words")<br />

}<br />

}<br />

while (it.hasNext){<br />

println(it.next())<br />

}<br />

When the above code is compiled and executed, it produces the following result:<br />

C:/>scalac Test.scala<br />

C:/>scala Test<br />

a<br />

number<br />

TUTORIALS POINT<br />

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!