Skip to content

Instantly share code, notes, and snippets.

@coacoas
Created June 28, 2012 18:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coacoas/3013140 to your computer and use it in GitHub Desktop.
Save coacoas/3013140 to your computer and use it in GitHub Desktop.
Scala's Option from Java
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));
}
}
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