Skip to content

Instantly share code, notes, and snippets.

@s5bug
Last active October 10, 2022 03:34
Show Gist options
  • Save s5bug/8f761b1ac4d6617beb3f0652b51c9962 to your computer and use it in GitHub Desktop.
Save s5bug/8f761b1ac4d6617beb3f0652b51c9962 to your computer and use it in GitHub Desktop.
20 intermediate exercises: Scala edition
// show Fluffy instances
trait Fluffy[F[_]] {
def furry[A, B](fa: F[A])(f: A => B): F[B]
}
// Exercise 1
// Relative Difficulty: 1
implicit def fluffyList: Fluffy[List] = new Fluffy[List] {}
// Exercise 2
// Relative Difficulty: 1
implicit def fluffyOption: Fluffy[Option] = new Fluffy[Option] {}
// Exercise 3
// Relative Difficulty: 5
implicit def fluffyReader[T]: Fluffy[T => *] = new Fluffy[T => *] {}
// Exercise 4
// Relative Difficulty: 5
implicit def fluffyEither[L]: Fluffy[Either[L, *]] = new Fluffy[Either[L, *]] {}
// show Misty instances
trait Misty[F[_]] {
def banana[A, B](fa: F[A])(f: A => F[B]): F[B]
def unicorn[A](a: A): F[A]
}
// Exercise 7
// Relative Difficulty: 2
implicit def mistyList: Misty[List] = new Misty[List] {}
// Exercise 8
// Relative Difficulty: 2
implicit def mistyList: Misty[Option] = new Misty[Option] {}
// Exercise 9
// Relative Difficulty: 6
implicit def mistyReader[T]: Misty[T => *] = new Misty[T => *] {}
// Exercise 10
// Relative Difficulty: 6
implicit def mistyEither[L]: Misty[Either[L, *]] = new Misty[Either[L, *]] {}
// show Additional Misty functions
trait Misty[F[_]] {
def banana[A, B](fa: F[A])(f: A => F[B]): F[B]
def unicorn[A](a: A): F[A]
}
// Exercise 6
// Relative Difficulty: 3
// (use banana and/or unicorn)
def furry_[F[_], A, B](fa: F[A])(f: A => B)(implicit misty: Misty[F]): F[B] =
???
// Exercise 12
// Relative Difficulty: 3
def jellybean[F[_], A](ffa: F[F[A]])(implicit misty: Misty[F]): F[A] =
???
// Exercise 13
// Relative Difficulty: 6
def apple[F[_], A, B](fa: F[A])(fab: F[A => B])(implicit misty: Misty[F]): F[B] =
???
// Exercise 14
// Relative Difficulty: 6
def moppy[F[_], A, B](la: List[A])(f: A => F[B])(implicit misty: Misty[F]): F[List[B]] =
???
// Exercise 15
// Relative Difficulty: 6
// (bonus: use moppy)
def sausage[F[_], A](lfa: List[F[A]])(implicit misty: Misty[F]): F[List[A]] =
???
// Exercise 16
// Relative Difficulty: 6
// (bonus: use apple + furry_)
def banana2[F[_], A, B, C](fa: F[A], fb: F[B])(f: (A, B) => C)(implicit misty: Misty[F]): F[C] =
???
// Exercise 17
// Relative Difficulty: 6
// (bonus: use apple + banana2)
def banana3[F[_], A, B, C, D](fa: F[A], fb: F[B], fc: F[C])(f: (A, B, C) => D)(implicit misty: Misty[F]): F[D] =
???
// Exercise 18
// Relative Difficulty: 6
// (bonus: use apple + banana3)
def banana4[F[_], A, B, C, D, E](fa: F[A], fb: F[B], fc: F[C], fd: F[D])(f: (A, B, C, D) => E)(implicit misty: Misty[F]): F[E] =
???
// show State instances for Fluffy and Misty
trait Misty[F[_]] {
def banana[A, B](fa: F[A])(f: A => F[B]): F[B]
def unicorn[A](a: A): F[A]
}
trait Fluffy[F[_]] {
def furry[A, B](fa: F[A])(f: A => B): F[B]
}
case class State[S, A](
state: S => (S, A)
)
// Exercise 19
// Relative Difficulty: 9
implicit def fluffyState[S]: Fluffy[State[S, *]] = new Fluffy[State[S, *]] {}
// Exercise 20
// Relative Difficulty: 10
implicit def mistyState[S]: Misty[State[S, *]] = new Misty[State[S, *]] {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment