Created
June 28, 2012 18:42
-
-
Save coacoas/3013140 to your computer and use it in GitHub Desktop.
Scala's Option from Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import scala.Function0; | |
import scala.Function1; | |
import scala.Option; | |
import scala.Some; | |
import scala.runtime.AbstractFunction0; | |
import scala.runtime.AbstractFunction1; | |
public class OptionTest { | |
public static void main(String[] args) { | |
Option<String> some = new Some<String>("testing"); | |
Option<String> none = Option.apply(null);; | |
Function0<String> easy = new AbstractFunction0<String>() { | |
@Override | |
public String apply() { | |
return "This is much less tricky than I expected"; | |
} | |
}; | |
Function1<String, Integer> length = new AbstractFunction1<String, Integer>() { | |
@Override | |
public Integer apply(String arg0) { | |
return arg0.length(); | |
} | |
}; | |
System.out.println("Testing == " + some.getOrElse(easy)); | |
System.out.println("Tricky == " + none.getOrElse(easy)); | |
System.out.println("Some length == " + some.map(length)); | |
System.out.println("None length == " + none.map(length)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Testing == testing | |
Tricky == This is much less tricky than I expected | |
Some length == Some(7) | |
None length == None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment