Skip to content

Instantly share code, notes, and snippets.

View jdegoes's full-sized avatar

John A. De Goes jdegoes

View GitHub Profile
@jdegoes
jdegoes / FreeMonad.scala
Created August 21, 2016 16:24
A pedagogical free(er) monad in 23 lines of Scala
sealed trait Free[F[_], A] { self =>
final def map[B](ab: A => B): Free[F, B] = Free.flatMap(self, ab andThen (Free.point[F, B](_)))
final def flatMap[B](afb: A => Free[F, B]): Free[F, B] = Free.flatMap(self, afb)
final def interpret[G[_]: Monad](fg: F ~> G): G[A] = self match {
case Free.Point(a0) => a0().point[G]
case Free.Effect(fa) => fg(fa)
case fm : Free.FlatMap[F, A] =>
val ga0 = fm.fa.interpret[G](fg)
ga0.flatMap(a0 => fm.afb(a0).interpret[G](fg))
}
@jdegoes
jdegoes / IO.scala
Created August 9, 2016 22:26
A pedagogical implementation of the IO monad in Scala in 14 LOC
case class IO[A](unsafePerformIO: () => A) {
def map[B](ab: A => B): IO[B] = IO(() => ab(unsafePerformIO()))
def flatMap[B](afb: A => IO[B]): IO[B] =IO(() => afb(unsafePerformIO()).unsafePerformIO())
def tryIO(ta: Throwable => A): IO[A] =
IO(() => IO.tryIO(unsafePerformIO()).unsafePerformIO() match {
case Left(t) => ta(t)
case Right(a) => a
})
}
object IO {
@jdegoes
jdegoes / Advanced Functional Programming in Scala Training - ScalaWorld 2016.md
Created July 21, 2016 14:19
Advanced Functional Programming in Scala Training - ScalaWorld 2016
  1. Kiss type confusion goodbye as you learn to understand complex type signatures: T[_[_, _], _], T[({type λ[A]=F[A, K]})#λ].
  2. Level up your ability to write higher-order functions and define combinators to construct larger programs from smaller ones
  3. Learn how you can use rank-N types to program at a higher-level of abstraction, with strong correctness guarantees
  4. Discover how existentials help you compose functionality without exploding the size of type signatures
  5. Master type classes to generate generic, testable code without the tangling and non-local reasoning of inheritance
  6. Use "functional design patterns" like a boss, including functors, applicatives, monads, profunctors, monoids, and others
  7. Have your immutable cake and eat it too with "optics" that let you manipulate complex data structures with ease
  8. Traverse your own data structures without writing any recursive code through powerful, composable, generic recursion schemes
  9. Model effects with powerful, purely functional techniq
@jdegoes
jdegoes / HigherKindedJava.java
Last active January 9, 2019 11:23
Modeling higher-kinded types in a language without them.
class Option<A> {
protected Option() { }
}
interface App<F, A> {
F proof();
}
class OptionF {
private OptionF() {}
private static class AppOption<A> implements App<OptionF, A> {
@jdegoes
jdegoes / lambdaconf-sums-products.md
Created March 17, 2016 19:01
Exercises for "Sums and Products, Oh My!" — LambdaConf Outpost One Meetup (3/17/2016)

Introduction

A type is a set of values.

Int -- The set of all integer values (that fit into 64 bits)

To say that a term a has type A is to say that a is a member of the set of values represented by A.

@jdegoes
jdegoes / types.md
Last active January 8, 2022 00:00
Fun with Types

Reading & Understanding Types: Exercises to Level Up!

A type is a set of values. A value stores information at runtime in computer memory (such as a certain integer, a certain list of strings, etc.).

Monomorphic Function Types

In many languages, functions are values (Haskell, PureScript, Javascript)! Or at least, you can pretend they are (Scala, Java).

@jdegoes
jdegoes / fun-with-functions.md
Last active January 8, 2022 00:00
Fun with Functions! - Exercises Only

These exercises were part of a LambdaConf Outpost meetup. You may find the instructions here.

  1. Develop a model for boolean values (Bool), which may be either true or false.

    Think: Do you need an if-then-else construct? Why or why not?

    Bonus: Develop a model for eithers (Either), whih can be one thing ("left") or another ("right"), and model a boolean as a partitioning of a set into two disjoint sets.

  2. Develop a model for optional values (Maybe / Option), which are containers that are either empty ("nothing" / "none") or hold a single value ("just" / "some").

@jdegoes
jdegoes / fun-functions.md
Created January 15, 2016 00:48
Fun with Functions! - Exercises & Solutions
  1. Develop a model for boolean values (Bool), which may be either true or false.

    Think: Do you need an if-then-else construct? Why or why not?

    Bonus: Develop a model for eithers (Either), whih can be one thing ("left") or another ("right"), and model a boolean as a partitioning of a set into two disjoint sets.

    type Bool = forall a. a -> a -> a
    

_true :: Bool

@jdegoes
jdegoes / halogen.purs
Created June 25, 2015 21:18
Halogen Components
module Main where
import Debug.Trace
import Halogen
main = do
trace "Hello sailor!"
module Halogen where
import Control.Monad.State.Trans
import Control.Monad.State.Class
@jdegoes
jdegoes / puree2.purs
Created December 7, 2014 22:49
More ideas on Purescript-based dependency management & build tool
data Dep a b
dir :: forall a. String -> Dep a File
file :: forall a. String -> Dep a File
depends = git :// "git@github.com:bodil/purescript-signal.git" ## "1.2.3" <>
git :// "git@github.com:purescript-contrib/purescript-dom.git" ## "2.1.2" <>
bower :// "purescript-strongcheck" ## "0.7.1"