Skip to content

Instantly share code, notes, and snippets.

View monadplus's full-sized avatar

Arnau Abella monadplus

View GitHub Profile
@monadplus
monadplus / cats.md
Last active June 17, 2019 07:07
What's inside cats ?
import cats._, cats.data._, cats.implicits._

Kernel

  • Semigroup
  • Band
  • Commutative Semigroup
@monadplus
monadplus / collections.md
Last active June 12, 2019 09:48
Functional Data Structures

Exhaustive list of data structures here.

import cats.collection._

List of data structures: BitSet, Dequeue, Diet, Discrete, DisjointSets, Heap, ISet, AvlMap, PairingHeap, PartiallyOrderedSet, Range, AvlSet, TreeList

@monadplus
monadplus / Notes.md
Last active June 28, 2019 16:03
Notes from dailylife.

Variance:

  • Covariant ( return type )
  • Contravariant ( argument type )

Covariant type on contravariant position:

case class Box[+A](value: A) {
  def set(a: A): Box[A] = Box(a)
}
@monadplus
monadplus / Profunctor.hs
Last active June 17, 2019 18:58
Data.Profunctor
-- Source: https://hackage.haskell.org/package/profunctors-5.4/docs/Data-Profunctor.html
class Profunctor p where
-- dimap | lmap, rmap
dimap :: (a -> b) -> (c -> d) -> p b c -> p a d
lmap :: (a -> b) -> p b c -> p a c
rmap :: (b -> c) -> p a b -> p a c
class Profunctor p => Strong p where
-- first' | second'
@monadplus
monadplus / async.md
Last active July 4, 2019 09:28
cats-effect.Async and context switching

Cats-effect Async.async has the following pitfall:

During the execution of async, the effect is guaranteed to run on the backer ContextShift required by the creation of the Async type class. The main issue is that, if the callback is running on its on blocking execution context, the IO will not switch back to the main execution context (or the one running before calling async). And the rest of your code will be executed on the blocking execution context. This could end up making your cpu bounded code run on a specialized thread pool that may not be ready to handle more threads running on it.

You can test it yourself by running the following snippet:

import cats._, cats.data._, cats.implicits._
import cats.effect._, cats.effect.implicits._
import fs2._, fs2.io._
@monadplus
monadplus / short-circuit-foldleft.md
Last active July 3, 2019 21:42
Short-circuit on foldLeft

FoldLeftSC

A Foldable.foldLeftimplementation that short-circuits on a given predicate


The first attempt was a failure. Using foldM and Option.

def foldLeft[F[_]: Foldable, A, B](fa: F[A])(b: B)(f: (B, A) => B)(p: B => Boolean): B = 
@monadplus
monadplus / license.txt
Created July 18, 2019 07:48
All my gists are under the following license.
All public gists https://gist.github.com/monadplus
Copyright 2019, Arnau Abella Gassol
MIT License, http://www.opensource.org/licenses/mit-license.php
@monadplus
monadplus / keys_format.md
Created September 19, 2019 10:38
Keys formats
@monadplus
monadplus / ioref_stref_mvar_tvar_tmvar.md
Last active September 26, 2019 07:18
IORef/STRef/MVar/TVar/TMVar
  • IORef: Mutable references in the IO monad.
  • STRef: Mutable references in the (strict) ST monad.
  • MVar: synchronized mutable variables between threads.

STRef provides mutable references, IORef provides mutable references, exception catching, threads, and of course IO.

Rule of thumb: use the weakest abstraction.