Skip to content

Instantly share code, notes, and snippets.

View Jacoby6000's full-sized avatar

Jacob Barber Jacoby6000

  • Plano, TX
View GitHub Profile
// 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)
}
@Jacoby6000
Jacoby6000 / ListContainsExtractor.scala
Last active October 9, 2015 19:00 — forked from sam/ListContainsExtractor.scala
Trying to come up with an Extractor I can pass input to other than my match object.
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")
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))
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
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) =>
{
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]
}
case class Chunk(...)
def process(chunk: Chunk): (List[String], List[String]) = {
...
storeInDb
...
returnErrsAndWarns
}
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)
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 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))
}