Skip to content

Instantly share code, notes, and snippets.

class Availability extends React.Component {
constructor(props) {
super(props);
this.state = {
calendar: null
};
}
componentDidMount() {
setTimeout(() => {
this.setState({
const Kitten = (props) => {
return (
<h1>Cats</h1> // put jsx in here
)
}
// or
class Kitten extends React.Component {
constructor(props) {
super(props);
}
object Main extends App {
import cats._
import cats.implicits._
val ans: Eval[Int] = for {
a <- Eval.now { println("a"); 40 }
b <- Eval.always { println( "b" ); 2 }
c <- Eval.later { println( "c" ); 2 }
} yield {
object Main extends App {
import cats._
import cats.implicits._
private val now: Eval[Double] = Eval.now(math.random)
println(now.value)
println(now.value)
private val later: Eval[Double] = Eval.later(math.random)
object Main extends App {
import cats._
import cats.implicits._
println(123.show)
println("xyc".show)
println(true.show)
// using show on custom objects
/*
3 Monad laws
1. Right identity: pure(a).flatMap(func) == func(a)
2. m.flatMap(pure) = m
3. m.flatMap(f).flatMap(g) = m.flatMap(x => f(x).flatMap(g))
*/
trait MyMonad[F[_]] {
// has a contramap method
trait MyContravariantFunctor[F[_]] {
def contramap[A, B](fa: F[A], f: B => A): F[B]
}
// We can use contramap to build a show for Salary from the show for Money
import cats._
import cats.implicits._
object Main extends App {
import cats._
import cats.implicits._
def power[F[_]](f: F[Int])(implicit func: Functor[F]): F[Int] = {
f.map(x => x*x)
}
/*
Functor Laws:
1. Identity
fa.map(a => a) == fa
2. Composition
fa.map(f).map(g) = fa.map(g(f(_)))
*/
trait MyFunctor[F[_]] {
def map[A, B](fa: F[A], f: A => B): F[B]
}
// eg union of non empty sets
trait MySemigroup[A] {
// must be associative
def combine(a1: A, a2: A): A
}
// eg union of sets, where the empty set is the identity element
trait MyMonoid[A] extends MySemigroup[A] {
// is an identity element
def empty: A