Skip to content

Instantly share code, notes, and snippets.

John A. De Goes jdegoes

View GitHub Profile
View Hangman.scala
//
// EXERCISE 4
//
// Create a type class to describe `printLine` and `readLine`.
//
trait Console[F[_]] {
def printLine(line: String): F[Unit]
def readLine: F[String]
}
@jdegoes
jdegoes / fpmax.scala
Created Jul 13, 2018
FP to the Max — Code Examples
View fpmax.scala
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 Feb 12, 2019
Example code from Applied Functional Programming with Scala
View afp-examples.scala
package lambdaconf.introfp2
// import scalaz._
// import Scalaz._
object functions {
object totality {
///def f[A]: A = null
def g[A]: A = throw new Error
View GameWorld.scala
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 Nov 11, 2017
A sketch of an `Async ~> IO`
View AsyncToIO.scala
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 Feb 1, 2017
SlamData Contributor's License Agreement
View CLA.md

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 Jan 19, 2017
EJSON: Extended JSON
View example.ejson
{
"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 Nov 11, 2017
Pedagogical synchronous and asynchronous effect monads in Scala.
View IOTask.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 Oct 27, 2016
Free applicative in free monad
View SeqPar.purs
-- 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
View Applied-FP-with-Scala.md

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
You can’t perform that action at this time.