Created
January 3, 2013 23:59
-
-
Save drapp/4448664 to your computer and use it in GitHub Desktop.
Free Yourself from the Tyranny of Null
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
try { | |
Some(Integer.parseInt(header)) | |
} catch { | |
case e: NumberFormatException => None | |
} |
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
// The mb prefix stands for maybe | |
def contrivedExample(mbNumber: Option[Int]): Int = { | |
val step1 = possiblyNumber.map(number => number + 5) | |
val step2 = step1.filter(number => number > 0) | |
step2.getOrElse(0) | |
} |
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
def getIntFromHeader(mbRequest: Option[Request], key: String): Option[Int] = { | |
mbRequest.flatMap { request => | |
request.headers.flatMap { headers => | |
headers.get(key).flatMap { header => | |
catching(classOf[NumberFormatException]).opt(Integer.parseInt(header)) | |
} | |
} | |
} | |
} |
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
val mbHeaders: Option[Map[String, String]] = // ... | |
val mbHeader = mbHeaders.flatMap(headers => headers.get(key)) |
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
class Request { | |
// ... | |
Option<Map<String, String>> headers; | |
// ... | |
} | |
/** | |
* @param request The request object to look in | |
* @param headerName the header name to look for | |
*/ | |
public Option<Integer> getIntFromHeader(Option<Request> request, String headerName) { | |
if(request.isDefined && request.get().getHeaders.isDefined() { | |
// Ideally our data structures would be returning Option rather than null | |
Option<String> header = Option.fromNull(request.get().getHeaders().get().get(headerName)); | |
if(header.isDefined()) { | |
try { | |
return new Some<Integer>(Integer.parseInt(header.get())); | |
} catch(Exception e) { | |
return new None<Integer>(); | |
} | |
} | |
} | |
return new None<Integer>(); | |
} |
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
class Request { | |
// ... | |
val headers: Option[Map[String, String]] | |
// ... | |
} | |
def getIntFromHeader(mbRequest: Option[Request], key: String): Option[Int] = for { | |
request <- mbRequest | |
headers <- request.headers | |
header <- headers.get(key) //returns an option | |
intVal <- catching(classOf[NumberFormatException]).opt(Integer.parseInt(header)) | |
} yield { | |
intVal | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment