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 / fpmax.scala
Created July 13, 2018 03:18
FP to the Max — Code Examples
package fpmax
import scala.util.Try
import scala.io.StdIn.readLine
object App0 {
def main: Unit = {
println("What is your name?")
val name = readLine()
@jdegoes
jdegoes / afp-examples.scala
Last active February 12, 2019 19:50
Example code from Applied Functional Programming with Scala
package lambdaconf.introfp2
// import scalaz._
// import Scalaz._
object functions {
object totality {
///def f[A]: A = null
def g[A]: A = throw new Error
object game {
case class Lens[S, A](set: A => S => S, get: S => A) { self =>
def >>> [B](that: Lens[A, B]): Lens[S, B] =
Lens[S, B](
set = (b: B) => (s: S) => self.set(that.set(b)(self.get(s)))(s),
get = (s: S) => that.get(self.get(s))
)
}
case class Prism[S, A](set: A => S, get: S => Option[A]) { self =>
@jdegoes
jdegoes / AsyncToIO.scala
Last active November 11, 2017 17:02
A sketch of an `Async ~> IO`
val AsyncToIO: NaturalTransformation[Async, IO] {
def apply[A](fa: Async[A]): IO[A] = {
for {
ref <- newIORef[Either[Throwable, A]](Left(new Error("No value")))
counter <- IO(new java.util.concurrent.CountDownLatch(1))
_ <- fa.register(v => ref.set(v).flatMap(_ => IO(counter.countDown()))
_ <- IO(counter.await())
v <- ref.get
a <- v match {
case Left(e) => IO.fail(e)
@jdegoes
jdegoes / CLA.md
Created February 1, 2017 17:22
SlamData Contributor's License Agreement

Quasar Contributor License Agreement

Thank you for your interest in contributing to the Quasar open source project.

This contributor agreement ("Agreement") describes the terms and conditions under which you may Submit a Contribution to Us. By Submitting a Contribution to Us, you accept the terms and conditions in the Agreement. If you do not accept the terms and conditions in the Agreement, you must not Submit any Contribution to Us.

This is a legally binding document, so please read it carefully before accepting the terms and conditions. If you accept this Agreement, the then-current version of this Agreement shall apply each time you Submit a Contribution. The Agreement may cover more than one software project managed by Us.

1. Definitions

@jdegoes
jdegoes / example.ejson
Created January 19, 2017 18:45
EJSON: Extended JSON
{
"string-key": 1,
{1: "non-string-key"}: true,
"metadata-on-key" @ true : "metadata-on-value" @ [1, 2, "metadata-on-metadata" @ null]
}
@jdegoes
jdegoes / IOTask.scala
Last active November 11, 2017 17:02
Pedagogical synchronous and asynchronous effect monads in Scala.
case class IO[A](unsafePerformIO: () => A) { self =>
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 attempt: IO[Either[Throwable, A]] =
IO(() => try { Right(unsafePerformIO()) } catch { case t : Throwable => Left(t) })
def forkIO: Task[A] = Task(f => IO(unsafePerformIO = () => {
new java.lang.Thread {
override def run: Unit = {
@jdegoes
jdegoes / SeqPar.purs
Created October 27, 2016 20:38
Free applicative in free monad
-- A sequential series of parallel program fragments in `f`:
type SeqPar f a = Free (FreeAp f) a
liftFA :: forall f a. f a -> SeqPar f a
liftFA = pure >>> pure
liftSeq :: forall f a. Free f a -> SeqPar f a
liftSeq fa = foldFree fa liftFA
liftPar :: forall f a. FreeAp f a -> SeqPar f a

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x

Integers

// `Tuple[Natural, Natural]`
case class Integer(pos: Natural, neg: Natural) { self =>
  def unary_- = copy(neg, pos)
  def + (that: Integer): Integer = Integer(self.pos + that.pos, self.neg + that.neg)
  def - (that: Integer): Integer = this + (-that)
  def * (that: Integer): Integer = Integer(