Skip to content

Instantly share code, notes, and snippets.

@dcastro
dcastro / origami-solution.js
Last active April 30, 2018 12:58
Origami Programming Exercises
// Exercises: https://gist.github.com/hugoferreira/21f1f0ec23186adb5e6832e6aee618d6
const eq = require('lodash').isEqual
const fold = (as, base) => (f) => {
let result = base
for (let i = 0; i < as.length; i++)
result = f(result, as[i])
return result
}
@dcastro
dcastro / shapeless-fieldname.scala
Last active July 13, 2021 20:01
Shapeless - get a field's name in a type-safe way
import shapeless._
import shapeless.ops.record.Keys
import shapeless.ops.hlist.Selector
import shapeless.tag._
/**
* Gets the name of a case class's field, in a type-safe way.
* If the class does not have a field with the given name, the program won't compile.
*
* Kinda similar to C#'s `nameof` operator, but not bolted onto the language
@dcastro
dcastro / json_lawless.scala
Last active July 24, 2018 15:08
Json decoder/encoders typeclasses are lawless
import io.circe.syntax._
// hypothesis: ∀ x∈X, x.asJson.as[X] == Right(x)
// counterexample, nested options:
type X = Option[Option[Int]]
val x: X = Some(None)
x.asJson.as[X] == Right(x)
import scala.io.StdIn._
object CustomIO extends App {
/**
* Slides: https://docs.google.com/presentation/d/15O8fzIEa85a8S_vk6N3b_S8F4IYbrFkLOXmtwbcy0cQ
*/
case class IO[A](unsafeRun: () => A) {
def map[B](f: A => B): IO[B] =
@dcastro
dcastro / GenericFunctor.hs
Last active October 1, 2018 10:39
Generic functor, abstracting over a constraint
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
module Playground where
@dcastro
dcastro / LevNum.hs
Last active October 5, 2018 16:38
Levity-polymorphic Num typeclass
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE TypeInType #-}
module LevNum where
import GHC.Prim
import GHC.Exts (TYPE)
class LevNum (a :: TYPE r) where
levAdd :: a -> a -> a
@dcastro
dcastro / GenericFunctorAlias.hs
Created October 17, 2018 17:20
Alias for empty constraint `* -> Constraint`
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FunctionalDependencies #-}
module Playground4 where
import Data.Kind (Constraint)
import qualified Data.List as List
class GFunctor (c :: * -> Constraint) (f :: * -> *) | f -> c where
@dcastro
dcastro / setup.md
Last active October 19, 2018 16:12
Haskell Setup (macos)

Setup

curl -sSL https://get.haskellstack.org/ | sh


stack new <project-name> simple-hpack
cd <project-name>

// build project
@dcastro
dcastro / Fin.md
Last active October 23, 2018 18:56
Finite sets in Haskell

This assumes familiarity with kinds and GADTs. Here's the extensions you're gonna need:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ExplicitForAll #-}
@dcastro
dcastro / usage.scala
Last active December 6, 2018 12:39
"Newtype" deriving for scala
import org.scalacheck.cats.implicits._
import org.scalacheck.Gen
import io.circe.{Decoder, Encoder}
object Test extends App {
case class X(i: Int)
implicit val x: Encoder[X] = Wrapper[X].deriveInstance[Encoder]
implicit val y: Decoder[X] = Wrapper[X].deriveInstance[Decoder].withErrorMessage("some custom error msg")
val z: Gen[X] = Wrapper[X].lift(Gen.choose(0, 10))