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
| {"lastUpload":"2020-10-16T15:12:58.640Z","extensionVersion":"v3.4.3"} |
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
| import java.io.{ByteArrayOutputStream, ByteArrayInputStream} | |
| import java.util.zip.{GZIPOutputStream, GZIPInputStream} | |
| import scala.util.Try | |
| object Gzip { | |
| def compress(input: Array[Byte]): Array[Byte] = { | |
| val bos = new ByteArrayOutputStream(input.length) | |
| val gzip = new GZIPOutputStream(bos) |
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
| loadNumberOfItems("foo.bar") shouldBe a [Int] | |
| // InvalidUrlException | |
| loadNumberOfItems("http://google.com") shouldBe a [Int] | |
| // InvalidPayloadException | |
| loadNumberOfItems("http://actual.server.endpoint.com/number") shouldBe a [Int] | |
| // ParsingException | |
| loadNumberOfItems("http://actual.server.endpoint.com/number") shouldBe a [Int] | |
| // ClassCastException |
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
| sealed trait Exception | |
| case object InvalidUrl extends Exception | |
| case object InvalidJson extends Exception |
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
| def loadNumberOfItems(url: String): Int = { | |
| val json: Json = request(url) | |
| json.getField("number").asInstanceOf[Int] | |
| } |
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
| def loadNumberOfItems(url: String): IO[Either[Exception, Int]] = | |
| Url(url) | |
| .toRight(InvalidUrl) | |
| .map(request(_)) | |
| .traverse(_.map(_.getFieldOption("number").flatMap(_.as[Int]).toRight(InvalidJson))) | |
| .map(_.flatten) |
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
| def loadNumberOfItems(url: String): IO[Either[Exception, Int]] = { | |
| val maybeUrl: Option[Url] = | |
| Url(url) | |
| val eitherUrl: Either[Exception, Url] = | |
| maybeUrl.toRight(InvalidUrl) | |
| val eitherJson: Either[Exception, IO[Json]] = | |
| eitherUrl.map(u => request(u)) | |
| val eitherResult: Either[Exception, IO[Either[Exception, Int]]] = | |
| eitherJson.map(futureJson => | |
| futureJson.map(json => json.getFieldOption("number").flatMap(x => x.as[Int]).toRight(InvalidJson))) |
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
| def count(p: A => Boolean): Int | |
| def find(p: A => Boolean): Option[A] | |
| def filter(p: A => Boolean): List[A] | |
| def exists(p: A => Boolean): Boolean | |
| def forall(p: A => Boolean): Boolean |
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
| Cafe.buyCoffee(creditCard) shouldBe (coffee, charge) |
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
| object Cafe { | |
| def buyCoffee(creditCard: CreditCard): (Coffee, Charge) = { | |
| val cup = new Coffee() // new coffee is created | |
| (cup, Charge(creditCart, cup.price)) // cup and charge is returned | |
| } | |
| } | |
| // Can be created without new keyword | |
| case class Charge(creditCard: CreditCard, amount: Double) |
NewerOlder