Last active
December 23, 2015 09:29
-
-
Save ryantanner/6614458 to your computer and use it in GitHub Desktop.
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
| val some5: Option[Int] = Some(5) | |
| val missingInt: Option[Int] = None | |
| val i: Int = 42 // Good | |
| val i: Int = "Hello" // Compiler error | |
| String st = "Hello" // Compiles | |
| String st = null // Danger! Also compiles! | |
| def findUserById(id: Int): Option[User] = { | |
| // Make a database query | |
| if(User.exists(id)) | |
| Some(User.get(id)) | |
| else | |
| None | |
| } | |
| def findNameById(id: Int): Option[String] = { | |
| findUserById(id).map { user => | |
| "Found user " + user.name | |
| } | |
| } | |
| def | |
| val eventuallyNumber: Future[Int] = getFutureNumber() | |
| def getNumber(maybeInt: Future[Int]): Future[String] = { | |
| maybeInt.map { i => | |
| "Got the number " + i | |
| } | |
| } | |
| (String) => Request => Future[Response] => Future[List[Tweet]] | |
| WS.url("http://api.twitter.com/tweets/youfoundryan").get() // Future[Response] | |
| def findTweetsByUser(username: String): Future[List[Tweet]] = { | |
| val requestUrl = "http://api.twitter.com/tweets/" + username | |
| WS.url(requestUrl).get() map { response => | |
| response.as[List[Tweet]] | |
| } | |
| } | |
| def tweetsByLocation( | |
| usernames: List[String], | |
| location: Location): Future[List[Tweet]] = { | |
| for { | |
| tweets <- usernames.map(findTweetsByUser) | |
| tweet <- tweets | |
| if (tweet.location == location) | |
| } yield tweet | |
| } | |
| def findUserById(maybeId: Future[Int]): Future[String] = { | |
| maybeId.flatMap { id => | |
| User.findById(id).map { user => // Returns Future[User] | |
| "Found user " + user.name | |
| } | |
| } | |
| } | |
| def getFutureName(username: Future[Int]): Future[String] = { | |
| for { | |
| id <- futureId | |
| user <- User.findById(id) | |
| } yield "Found user " + user.name | |
| } | |
| def findByRoute(airlineCode: String, origCode: String, destCode: String, departureDate: DateTime) = Action { | |
| Async { | |
| for { | |
| airline <- Airline.findByICAO(airlineCode) // Future[Airline] | |
| destination <- Airport.findByICAO(destCode) // Future[Airport] | |
| origin <- Airport.findByICAO(origCode) // Future[Airport] | |
| flight <- FlightAware.findByRoute(airline, destination, origin, departureDate) // Future[Flight] | |
| } yield { | |
| Ok(views.html.flight(flight)) | |
| } | |
| } | |
| } | |
| Future[Option[Flight]] | |
| class SumActor extends Actor { | |
| var sum = 0 | |
| def receive = { | |
| case GetSum => sender ! sum | |
| case Add(count) => sum += count | |
| } | |
| } | |
| val mySumActorRef = system.actorOf(Props[SumActor].withDeploy(Deploy(scope = RemoteScope("akka.tcp://sys@10.10.1.5")))) | |
| mySumActorRef ! Add(42) | |
| R -> N |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment