Git Product home page Git Product logo

tutorial-cats's Introduction

Tutorial cats

Your job is to make all the test green!

Notice that:

Cats implicits for a given type follow this convention import cats.instances.mytype._

Cats syntax for a given type follow this convention import cats.syntax.atype._

TODO 01 - Eq

Comparing with Eq, === and =!=

~testOnly example.EqSpec

TODO 02 - Printable

Implement the printable for Gato (spanish for Cat).

cats usually split typeclasses in the following way:

  • A trait defining the general behavior
  • A companion object that contains the type classes
  • A syntax object containing implicit class(es)
~testOnly example.PrintableSpec

TODO 03 - Monoid

Monoids can combine two elements of the same type (thanks to semigroup) and have an empty element.

|+| is an alias for combine present in semigroup

~testOnly example.MonoidSpec

TODO 04 - Functor

Playing with Functors.

Functors can map over something

~testOnly example.FunctorSpec

TODO 05 - Write and Read

Handling additional information to the main logic flow.

Write monad add additional information

Read monad expects contextual information

~testOnly example.WriteAndReadSpec

TODO 06 - State

Solving equations.

State Monad cumulates values

~testOnly example.StateSpec

TODO 07 - Validation

Validation Monad and Either Monad handle errors

~testOnly example.ValidationSpec

Problems?

If you are really, really, really having problems finding the right imports just include the following

import cats._
import cats.implicits._

However one of the goals of this exercise is to be able to identify where the typeclass implementation is located.

Contribution policy

Contributions via GitHub pull requests are gladly accepted from their original author. Along with any pull requests, please state that the contribution is your original work and that you license the work to the project under the project's open source license. Whether or not you state this explicitly, by submitting any copyrighted material via pull request, email, or other means you agree to license the material under the project's open source license and warrant that you have the legal authority to do so.

tutorial-cats's People

Contributors

baddoub avatar fagossa avatar joriscode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

tutorial-cats's Issues

Adding an example of `Validated` besides `mapN` could be a good idea.

I'm proposing something along these lines

import cats._
import cats.implicits._
import cats.data.Validated
import cats.data.Validated.{Invalid, Valid}

type ErrorOr[T] = T => Either[List[String], T]
type Result[T]  = T => Validated[List[String], T]

case class Student(country: String, age: Int)

def validateCountry(country: String): ErrorOr[Student] =
  student => Either.cond(student.country == country, student, List(s"Invalid country $country"))

def validateAge(minAge: Int): ErrorOr[Student] =
  student => Either.cond(student.age >= minAge, student, List(s"Invalid age $minAge"))

def run[T](validations: ErrorOr[T]*): Result[T] =
  t => validations.toList
    .map(rule => rule(t).toValidated)
    .foldLeft(t.asRight[List[String]].toValidated) {
      case (i@Valid(_), Valid(_)) => i // any side is ok
      case (i@Invalid(_), Valid(_)) => i
      case (Valid(_), i@Invalid(_)) => i
      case (Invalid(e1), Invalid(e2)) => Invalid(Semigroup[List[String]].combine(e1, e2))
    }

run[Student](
    validateAge(5),
    validateCountry("Frances")
)(Student("France", 3))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.