Skip to content

Instantly share code, notes, and snippets.

View Jacoby6000's full-sized avatar

Jacob Barber Jacoby6000

  • Plano, TX
View GitHub Profile
@Jacoby6000
Jacoby6000 / IO.scala
Last active January 19, 2018 17:16 — forked from jdegoes/IO.scala
A pedagogical implementation of the IO monad in Scala in 14 LOC
case class IO[A](unsafePerformIO: () => A) {
def map[B](ab: A => B): IO[B] = IO(() => ab(unsafePerformIO()))
def flatMap[B](afb: A => IO[B]): IO[B] =IO(() => afb(unsafePerformIO()).unsafePerformIO())
def tryIO(ta: Throwable => A): IO[A] =
val attempt = IO(() => IO.tryIO(unsafePerformIO()) // lift IO[A] to IO[Either[Throwable,A]]
attempt.unsafePerformIO() match {
case Left(t) => ta(t)
case Right(a) => a
})
}
@Jacoby6000
Jacoby6000 / Free.scala
Created January 8, 2018 19:03
Free vs Tagless
sealed trait Foo[A]
case class Bar[A](a: A) extends Foo[A]
case class Baz[A](s: String) extends Foo[A]
case class Woozle[A](foo: Foo[A]) extends Foo[A]
@Jacoby6000
Jacoby6000 / discriminators.scala
Created January 8, 2018 18:35
Scodec Discriminators
sealed trait Foo
case class Bar(a: Int) extends Foo
case class Baz(b: String) extends Foo
val barCodec: Codec[Bar] = ???
val bazCodec: Codec[Baz] = ???
val fooCodec: Codec[Foo] =
discriminated[Foo]
.by(uint32)
case class Path(path: String) extends AnyVal
trait QueryValue[U, A] {
def param(u: U): A
def function(path: Path, args: List[A]): A
def nul: A
}
trait QueryComparison[U, A] {
sealed trait QueryValue[A]
case class Parameter[A](value: A) extends QueryValue[A]
case class Function[A](path: Path, args: List[QueryValue[A]]) extends QueryValue[A]
case class Null[A]() extends QueryValue[A]
sealed trait QueryComparison[A]
case class QueryEqual[A](left: QueryValue[A], right: QueryValue[A]) extends QueryComparison[A]
case class QueryGreaterThan[A](left: QueryValue[A], right: QueryValue[A]) extends QueryComparison[A]
case class QueryGreaterThanOrEqual[A](left: QueryValue[A], right: QueryValue[A]) extends QueryComparison[A]
id | name | album | artist | year
--------------------------------------+---------------------------------------------------------------------+----------------------------------------------------+------------------------------------+---------
ebada1a7-fe44-48a6-b5af-178a6b74a88c | China Town | Revenge Of Shinobi | Yuzo Koshiro | 1989
c4f5fe57-e761-40eb-93ed-91df1adcf249 | Sugar Sweet Nightmare | Bakemonogatari: Ongaku Zenshuu Songs & Soundtracks | Yui Horie | 2011
dac75046-fee7-4416-a9f3-c892f72da44c | Hidamari no Uta (ft. Hatsune Miku) | Solitude Freak | Yuyoyuppe |
@Jacoby6000
Jacoby6000 / Deriver.scala
Created December 19, 2017 19:49
Doobie Shapeless Based Value Class Encoder
case class Foo(a: Int)
implicit val fooMeta: Meta[Foo] = valueClassMeta[Foo].apply
private def valueClassMeta[A]: PartiallyAppliedValueClassMeta[A] = new PartiallyAppliedValueClassMeta[A]
private class PartiallyAppliedValueClassMeta[A] {
def apply[L <: HList, S](
implicit ev: Generic.Aux[A, L],
toL: (S :: HNil) =:= L,
(uuid.successNel[CannotSaveSongError] |@|
section.lookupOneOf(songNameKeys).map(SongName(_)).toSuccess(MissingKey(songSection, songNameKeys)).toValidationNel |@|
section.lookupOneOf(artistKeys).map(Artist(_)).successNel |@|
section.lookupOneOf(albumKeys).map(Album(_)).successNel |@|
section.lookupOneOf(genreKeys).map(Genre(_)).successNel |@|
section.lookupOneOf(charterKeys).map(Charter(_)).successNel |@|
section.lookupOneOf(yearKeys).map(Year(_)).successNel |@|
now.successNel |@|
now.successNel)(Song.apply _)
object instances {
implicit val accessTokenDbIdentifiable: DbIdentifiable.Aux[AccessToken, AccessTokens] =
new DbIdentifiable.Aux[AccessToken, AcessTokens] {
val table = TableQuery[AccessTokens]
def getDbId(accessToken: AccessToken): Long = AccessToken
def withItemId(item: AccessToken, id: Long): AccessToken = item.copy(id = Option(id))
}
implicit val characterDbIdentifiable: DbIdentifiable.Aux[Character, Characters] =
new DbIdentifiable.Aux[Character, Characters] {
class MoreMonadErrorOps[F[_], A](val fa: F[A]) extends AnyVal {
def liftEmpty[E](implicit FE: MonadError[F, E]): PartiallyAppliedLiftEmpty[F, A, E] =
new PartiallyAppliedLiftEmpty[F, A, E] {
override val fa: F[A] = fa
override val F: MonadError[F, E] = FE
}
}
trait PartiallyAppliedLiftEmpty[F[_], A, E] {
val fa: F[A]