Skip to content

Instantly share code, notes, and snippets.

Avatar
🤓
https://leanpub.com/pfp-scala

Gabriel Volpe gvolpe

🤓
https://leanpub.com/pfp-scala
View GitHub Profile
View circe-either-codec.scala
implicit def eitherEncoder[A: Encoder, B: Encoder]: Encoder[Either[A, B]] =
Encoder[Json].contramap[Either[A, B]](_.asJson)
implicit def eitherDecoder[A: Decoder, B: Decoder]: Decoder[Either[A, B]] =
Decoder[Json].emap[Either[A, B]] { js =>
js.as[A] match {
case Left(_) => js.as[B].map(_.asRight[A]).leftMap(_.message)
case Right(x) => Right(x.asLeft[B])
}
}
View appimage.nix
{
appimagePackage = { binName, version, url, sha256 ? fakeSha256
, meta ? { platforms = [ "x86_64-linux" ]; } }:
let
pname = "${binName}-appimage";
name = "${pname}-${version}";
src = fetchurl {
inherit url sha256;
name = "${name}.AppImage";
@gvolpe
gvolpe / raceN.scala
Created Dec 13, 2019 — forked from ChristopherDavenport/raceN.scala
An Implementation of a Safe Multi-value Race
View raceN.scala
object RaceN {
import cats.implicits._
import cats.effect._
import cats.effect.concurrent._
import cats.data.NonEmptyList
// Use with atleast 3, otherwise you should prefer Concurrent.race
def raceN[F[_]: Concurrent, A](x1: F[A], x2: F[A], x3: F[A], xs: F[A]*): F[Either[NonEmptyList[Throwable], A]] = {
for {
deferred <- Deferred[F, A]
@gvolpe
gvolpe / RefMap.scala
Created Nov 22, 2019 — forked from johnynek/RefMap.scala
A wrapper on ConcurrentHashMap to use with cats.effect.Ref
View RefMap.scala
package org.bykn.refmap
import cats.data.State
import cats.effect.Sync
import cats.effect.concurrent.Ref
import java.util.concurrent.ConcurrentHashMap
import cats.implicits._
/**
View simple-refined-email.scala
import eu.timepit.refined._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.string.MatchesRegex
object refinements {
type EmailPred = MatchesRegex[W.`"""(?=[^\\s]+)(?=(\\w+)@([\\w\\.]+))"""`.T]
type Email = String Refined EmailPred
}
View init.vim
set directory=~/.vim/backup
set backupdir=~/.vim/backup " keep swap files here
filetype off " required
call plug#begin('~/.local/share/nvim/plugged') " you need to create this directory first (or change it)
Plug 'vim-airline/vim-airline' " bottom status bar
Plug 'vim-airline/vim-airline-themes'
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' } " fuzzy finder conf
Plug 'junegunn/fzf.vim' " fuzzy finder
@gvolpe
gvolpe / OpClasses.idr
Created Aug 21, 2019 — forked from sellout/OpClasses.idr
Defining ad-hoc polymorphism using dependently-typed implicits.
View OpClasses.idr
-- This illustrates (most of) a new(?) encoding of ad-hoc polymorphism using
-- dependent types and implicits.
--
-- Benefits of this approach:
-- • can have multiple “ambiguous” instances without requiring things like
-- Haskell’s newtypes to distinguish them;
-- • can disambiguate instances with minimal information, rather than having to
-- know some arbitrary instance name as you would with a system like Scala’s
-- implicits;
-- • implementers don’t need to know/provide the full family of instances – they
@gvolpe
gvolpe / sharding-fs2.scala
Last active May 24, 2019
Sharding values using Fs2
View sharding-fs2.scala
import cats.effect._
import cats.effect.concurrent.Ref
import cats.implicits._
import fs2._
import fs2.concurrent.Queue
import scala.util.control.NoStackTrace
object ShardingDemo extends IOApp {
def putStrLn[A](a: A): IO[Unit] = IO(println(a))
@gvolpe
gvolpe / CachedResource-Blog.md
Created Apr 16, 2019 — forked from Daenyth/CachedResource-Blog.md
CachedResource for cats-effect
View CachedResource-Blog.md

Motivation

cats-effect Resource is extremely handy for managing the lifecycle of stateful resources, for example database or queue connections. It gives a main interface of:

trait Resource[F[_], A] {
  /** - Acquire resource
    * - Run f
    * - guarantee that if acquire ran, release will run, even if `use` is cancelled or `f` fails
    */
  def use[B](f: A => F[B]): F[B]
View flip-andThen.hs
flip' :: (a -> b -> c) -> b -> a -> c
flip' f b a = f a b
andThen :: (a -> b) -> (b -> c) -> a -> c
andThen = flip' (.)
You can’t perform that action at this time.