Skip to content

Instantly share code, notes, and snippets.

@monadplus
Last active June 17, 2019 07:07
Show Gist options
  • Save monadplus/8322283673f6ce8f4b0370b9fc31585a to your computer and use it in GitHub Desktop.
Save monadplus/8322283673f6ce8f4b0370b9fc31585a to your computer and use it in GitHub Desktop.
What's inside cats ?
import cats._, cats.data._, cats.implicits._

Kernel

  • Semigroup
  • Band
  • Commutative Semigroup
  • Semilattice
  • Monoid
  • Commutative Monoid
  • Group
  • Abelian Group ( Commutative Group)
  • Bounded Semilattice
  • /////////
  • Comparison
  • Eq
  • Hash
  • PartialOrder
  • Order

Type classes

  • Show
  • Semigroup
  • Monoid
  • Functor
    • Contravariant
     contramap[A, B](fa: F[A])(f: B => A): F[B]
    
    • Invariant
    imap[A, B](fa: F[A])(f: A => B)(g: B => A): F[B]
    
  • FunctorFilter
  • Applicative
    • ApplicativeError
  • Foldable
  • UnorderedFoldable
  • Reducible ( Foldable )
  • Traverse
  • TraverseFilter
  • UnorderedTraverse
  • NonEmptyTraverse
  • Monad
  • MonadError
  • Comonad
extract :: f a -> a
coflatMap :: (f a -> b) -> f a -> f b
coflatten :: f a -> f f a
  • Inject and InjectK
inj :: a -> b       || f -> g 
prj :: b -> Maybe a || g -> Maybe f
  • SemigroupK
  • MonoidK
  • Semigroupal
product :: f a -> f b -> f (a,b)
  • Alternative ( Applicative + MonoidK )
  • Defer
  • Parallel
  • Distributive ( extends Functor )
distribute :: Functor g => (a -> f b) -> g a -> f g b
cosequence :: Functor g => g f a -> f g a
compose :: Functor g => Distributive f g a
  • Composed ( there is almost one for each type class)
  • Bifunctor
  • Bifoldable
  • Bimonad (Monad + Comonad)
  • BiTraverse
  • CommutativeApplicative (order of compositive does not interfere)
  • CommutativeMonad (order of compositive does not interfere)
  • NotNull ( take a look at shapeless)
  • Representable (Iso)
  • Instances:
    • Anyval, BigDecimal, BigInt, BitSet, Duration, Either, Eq, Equiv, FiniteDuration, Function, Future, Hash, Invariant, List, Map, Option, Order, Ordering, Parallel, PartialOrder, Queue, Set, SortedMap, SortedSet, Stream, String, Symbol, Try, Tuple, Uuid, Vector

Data types

  • Id
  • ZipList
  • Const
  • Either and EitherK
  • Validated
  • Eval
  • WriterT
  • State
  • Kleisli
  • Cokleisli
f a -> b
  • FunctionK
  • Ior
  • Nested
  • Binested
  • IdT
  • OptionT
  • EitherT
  • IorT
  • IndexedReadWriteStateT
  • IndexedStateT
  • Newtype (like AnyVal but zero runtime overhead)
  • FreeApplicative
  • FreeMonad
  • OneAnd
  • NonEmptyList
OneAnd[List, A]
  • Chain and NonEmptychain
  • ContT (cps)

Arrow

import cats.arrow._
  • FunctionK
f -> g
  • Compose
compose :: f b c -> f a b -> f a c
  • Category (Compose + id)
  • Profunctor
dimap :: (c -> a) -> (b -> d) -> f a b -> f c d
  • Strong
first  :: f a b -> f (a, c) (b, c)
second :: f a b -> f (c, a) (c, b)
  • Choice
choice : f a c -> f b c -> f (Either a b) c
  • Arrow (Cat + Strong)
split :: f a b -> f c d -> f (a,c) (b,d)
merge :: f a b -> f a c -> f a (b, c)
  • Commutative Arrow
  • ArrowChoice (Arrow + Choice

Alleycats

Must be added to dependencies and then

import alleycats._
  • Empty, Zero, One
  • Pure (applicative), Extract(comonad)
  • EmptyK
empty[A]: F[A]
  • ConstK
cons[A](hd: A, tl: F[A]): F[A]
  • Instances:
    • Future: Pure
    • Iterable: Foldable
    • List: Emptyk, Constk
    • Map: Traverse, TraverseFilter, Foldable, Functor
    • Option: emptyK
    • Set: Monad, Traverse, TraverseFilter, Foldable, Functor
    • Try: Bimonad

cats-testkit

Must be added to dependencies.

TODO

cats-law

Must be added to dependencies.

TODO

Functional data structures.

Moved to https://gist.github.com/monadplus/f15aee8f54363805d797af40140e9d80

Autoderived type classes instances.

// Full auto
import derived.auto.show._
// Full auto cached
import derived.cached.show._
// Manual semi
implicit val showFoo: Show[Foo] =  derived.semi.show
  • Eq
  • PartialOrder
  • Order
  • Hash
  • Functor
  • Foldable
  • Traverse
  • Show
  • Monoid and MonoidK
  • Semigroup and SemigroupK
  • Empty (defined in Alleycats)

Auto-transformation for algebras.

Supports the following transformations:

  • FunctorK
  • InvariantK
  • SemigroupalK
  • ApplyK

FAQ Does cats-tagless support algebras with extra type parameters?

  • Yes Does Cats-tagless support algebras with abstract type member?
  • Yes, with some caveats ( see FAQ workarounds)

Example:

import cats.tagless._

@finalAlg
@autoFunctorK
@autoSemigroupalK
@autoProductNK
trait ExpressionAlg[F[_]] {
  def num(i: String): F[Float]
  def divide(dividend: Float, divisor: Float): F[Float]
}

implicit object tryExpression extends ExpressionAlg[Try] {
  def num(i: String) = Try(i.toFloat)
  def divide(dividend: Float, divisor: Float) = Try(dividend / divisor)
}

implicit val fk : Try ~> Option = λ[Try ~> Option](_.toOption)

tryExpression.mapK(fk)

The example on Free is more compelling:

@finalAlg @autoFunctorK
trait Increment[F[_]] {
  def plusOne(i: Int): F[Int]
}

implicit object incTry extends Increment[Try] {
  def plusOne(i: Int) = Try(i + 1)
}

def program[F[_]: Monad: Increment](i: Int): F[Int] = for {
  j <- Increment[F].plusOne(i)
  z <- if (j < 10000) program[F](j) else Monad[F].pure(j)
} yield z

implicit def toFree[F[_]]: F ~> Free[F, ?] = λ[F ~> Free[F, ?]](t => Free.liftF(t))

program[Free[Try, ?]](0).foldMap(FunctionK.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment