Created
October 14, 2010 16:24
-
-
Save fedesilva/626497 to your computer and use it in GitHub Desktop.
Optionally saves your skin
This file contains 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
scala> // Problem: Some java apis return nulls. | |
scala> System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME") | |
res0: java.lang.String = null | |
scala> System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME").split(",") | |
java.lang.NullPointerException | |
at .<init>(<console>:7) | |
at .<clinit>(<console>) | |
at RequestResult$.<init>(<console>:9) | |
at RequestResult$.<clinit>(<console>) | |
at RequestResult$scala_repl_result(<console>) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) | |
at java.lang.reflect.Method.invoke(Method.java:597) | |
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981) | |
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981) | |
at scala.util.control.Exception$Catch.apply(Exception.scala:79) | |
at scala.tools.nsc... | |
scala> // Solution: `optionally` defined in gist below. | |
scala> optionally(System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME")) | |
res1: Option[java.lang.String] = None | |
scala> optionally(System.getenv("PATH")) | |
res2: Option[java.lang.String] = Some(/Users/f/.rvm/gems/ruby-1.8.7-p302/bin:/Users/f/.rvm/gems/ruby-1.8.7-p302@global/bin:/Users/f/.rvm/rubies/ruby-1.8.7-p302/bin:/Users/f/.rvm/bin:/Users/f/Library/homebrew/bin:/Users/f/Library/homebrew/Cellar/python/2.7/bin:/Users/f/Library/homebrew/sbin:/Users/f/bin:/Users/f/.cljr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/f/Workshop/Library/scala/dist/current/bin) | |
scala> optionally(System.getenv("PATH")).getOrElse("").split(":").map(_.split("/")) | |
res3: Array[Array[java.lang.String]] = Array(Array(, Users, f, .rvm, gems, ruby-1.8.7-p302, bin), Array(, Users, f, .rvm, gems, ruby-1.8.7-p302@global, bin), Array(, Users, f, .rvm, rubies, ruby-1.8.7-p302, bin), Array(, Users, f, .rvm, bin), Array(, Users, f, Library, homebrew, bin), Array(, Users, f, Library, homebrew, Cellar, python, 2.7, bin), Array(, Users, f, Library, homebrew, sbin), Array(, Users, f, bin), Array(, Users, f, .cljr, bin), Array(, usr, bin), Array(, bin), Array(, usr, sbin), Array(, sbin), Array(, usr, local, bin), Array(, usr, X11, bin), Array(, Users, f, Workshop, Library, scala, dist, current, bin)) | |
scala> optionally(System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME")).getOrElse("").split(":").map(_.split("/")) | |
res4: Array[Array[java.lang.String]] = Array(Array()) | |
scala> val from = "[email protected],username;[email protected],anotheruser;[email protected]" | |
from: java.lang.String = [email protected],username;[email protected],anotheruser;[email protected] | |
scala> optionally(from).getOrElse("").split(";").map(_.split(",")).collect({ case Array(u,n) => (u,n)}) | |
res6: Array[(java.lang.String, java.lang.String)] = Array(([email protected],username), ([email protected],anotheruser)) | |
scala> optionally(null).getOrElse("").split(";").map(_.split(",")).collect({ case Array(u,n) => (u,n)}) | |
res7: Array[(java.lang.String, java.lang.String)] = Array() | |
This file contains 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
scala> def optionally[T](any:T):Option[T] = any match { | |
| case null => None | |
| case None => None | |
| case _ => Some(any) | |
| } | |
optionally: [T](any: T)Option[T] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment