Last active
May 25, 2024 10:19
-
-
Save dacr/cbbf0e0b53366b98832ae61aed356f11 to your computer and use it in GitHub Desktop.
fold feature (instead of matching) to simplify code / published by https://github.com/dacr/code-examples-manager #a084d3e8-7f88-4b73-9853-3448e4ea57fe/af97f162f1cc80d5dfdbc4ee9433af3d8c6bde06
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
// summary : fold feature (instead of matching) to simplify code | |
// keywords : scala, language-feature, fold, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : a084d3e8-7f88-4b73-9853-3448e4ea57fe | |
// created-on : 2021-04-05T16:56:55Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
// --------------------- | |
// written after [John De Goes - 12 Steps To Better Scala (Part I)](https://youtu.be/71yhnTGw0hY) | |
import scala.util.{Try,Success,Failure} | |
// ---------------------------------------------------------------------------------------------------- | |
{ | |
val input = Option("B") | |
val dummy1: String = input match { | |
case None => "A" | |
case Some(x) => x | |
} | |
val dummy2: String = input.map(identity).getOrElse("A") | |
val result = input.fold("A")(identity) // *** FOLD WAY WITH OPTION*** | |
assert(result == "B") | |
} | |
// ---------------------------------------------------------------------------------------------------- | |
{ | |
val input = Option("B") | |
val dummy1: Int = input match { | |
case None => 1 | |
case Some(_) => 2 | |
} | |
val dummy2: Int = input.map(_ => 2).getOrElse(1) | |
val result: Int = input.fold(1)(_ => 2) // *** FOLD WAY WITH OPTION*** | |
assert(result == 2) | |
} | |
// ---------------------------------------------------------------------------------------------------- | |
{ | |
val input: Either[String, String] = Right("r") | |
val dummy1: Int = input match { | |
case Left(_) => 1 | |
case Right(_) => 2 | |
} | |
val dummy2: Int = input.toOption.map(_ => 2).getOrElse(1) | |
val result: Int = input.fold(_ => 1, _ => 2) // *** FOLD WAY WITH EITHER *** | |
assert(result == 2) | |
} | |
// ---------------------------------------------------------------------------------------------------- | |
{ | |
val input: Try[String] = Success("something") | |
val dummy1: Int = input match { | |
case Failure(_) => 1 | |
case Success(_) => 2 | |
} | |
val dummy2: Int = input.map(_ => 2).getOrElse(1) | |
val result = input.fold(_ => 1, _ => 2) // *** FOLD WAY WITH TRY *** | |
assert(result==2) | |
} | |
// ---------------------------------------------------------------------------------------------------- | |
{ | |
case class UserProfile(name: String, postalCode: String) | |
def getCountryFromIP(ip: String): String = "france" | |
def lookupCountry(x: String) = "france" | |
val someProfile = Option(UserProfile("joe", "35510")) | |
// *** FOLD WAY WITH OPTION WHILE USING andThen function composition *** | |
val country = someProfile.fold(getCountryFromIP("4.4.4.4"))(((x: UserProfile) => x.postalCode) andThen lookupCountry) | |
assert(country == "france") | |
} | |
// ---------------------------------------------------------------------------------------------------- | |
println("Everything OK") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment