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
| // scala | |
| import scala.util.{Failure, Success, Try} | |
| List("1","2","3","4").foldRight(Right(List.empty[Int]): Either[String, List[Int]]) { | |
| case (current, Right(list)) => Try(current.toInt) match { | |
| case Success(int) => Right(int :: list); | |
| case Failure(err) => Left(err.getMessage) | |
| } | |
| case (_, Left(err)) => Left(err) | |
| } |
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 scala.util.Try | |
| case class Contains(test: String) | |
| object ContainsA { | |
| def unapply(obj: Contains): Option[String] = Some(obj.test) | |
| } | |
| object ContainsB { | |
| def unapply(obj: Contains): Option[Int] = Try(obj.test.toInt).toOption | |
| } | |
| val container = Contains("foo") |
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 Expr { | |
| def fold[T, B](expr: Expr[T])( // might also be called cata | |
| litHandler: T => B, | |
| addHandler: (B, B) => B, | |
| subHandler: (B, B) => B, | |
| mulHandler: (B, B) => B, | |
| divHandler: (B, B) => B): B = { | |
| def process(ex: Expr[T]): B = ex match { | |
| case Lit(literal) => litHandler(literal) | |
| case Add(left, right) => addHandler(process(left), process(right)) |
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 backfill[A](list: List[Option[A]], initialReplacement: A): List[A] = list.foldLeft((List.empty[A], initialReplacement)){ | |
| case ((list, carry), None) => ((list :+ carry), carry) | |
| case ((list, _), Some(newValue)) => ((list :+ newValue), newValue) | |
| }._1 |
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 IntParseFailure | |
| case object MalformedString extends IntParseFailure | |
| case class NegativeNumber(num: Int) extends IntParseFailure | |
| def stringToPositiveInt(str: String): Either[IntParseFailure, Int] = { | |
| Try(str.toIntOption) match { | |
| case Failure(err) => | |
| Left(MalformedString) | |
| case Success(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
| { | |
| messageRouter: MessageRouter[_] => | |
| val message = messageRouter.transform(transaction) | |
| messageRouter.messageQueue.send(message) | |
| } | |
| case class MessageRouter[A](messageQueue: MessageQueue[A], transform: Transaction => A) | |
| trait MessageQueue[A] { | |
| def send(a: A): MessageId[A] | |
| } |
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
| case class Chunk(...) | |
| def process(chunk: Chunk): (List[String], List[String]) = { | |
| ... | |
| storeInDb | |
| ... | |
| returnErrsAndWarns | |
| } | |
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 scala.annotation.tailrec | |
| implicit class ListExtensions[A](list: List[A]) { | |
| def zipLeft[B](other:List[B]): List[(A, Option[B])] = { | |
| @tailrec | |
| def go(as: List[A], bs: List[B], carry: List[(A, Option[B])]): List[(A, Option[B])] = { | |
| (as, bs) match { | |
| case (Nil , _ ) => carry | |
| case (_ , Nil ) => as.map(a => (a, None)) ::: carry | |
| case (a :: as, b :: bs) => go(as, bs, (a, Some(b)) :: carry) |
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
| case class Refrigerated[T](thing: T) // this is just something holding things that have been refrigerated. | |
| trait Refrigeratable[T] { // this is the typeclass | |
| def refrigerate(thing: T): Refrigerated[T] | |
| } | |
| object Refrigerable { | |
| @inline // this is just an annotation telling the JVM to inline this, reducing the amount of indirection occuring. | |
| def apply[T](refrigerateMethod: T => Refrigerated[T]): Refrigerable[T] = | |
| new Refrigerable { | |
| def refrigerate(thing: T): Refrigerated[T] = refrigerateMethod(thing) |
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
| // this is bad. Use my typeclasses.scala or simulacrum.scala instead of these. | |
| case class Refrigerated[T](thing: T) // this is just something holding things that have been refrigerated. | |
| trait Refrigeratable[T] { // this is the typeclass | |
| def refrigerate(thing: T): Refrigerated[T] | |
| } | |
| case class Pillow(fluffiness: Int, warmness: Int) extends Refrigeratable { | |
| def refrigerate(pillow: Pillow) = Refrigerated(pillow.copy(warmness = pillow.warmness / 2)) | |
| } |