Skip to content

Instantly share code, notes, and snippets.

View noelwelsh's full-sized avatar
💭
Hacking on Doodle when I get time

Noel Welsh noelwelsh

💭
Hacking on Doodle when I get time
View GitHub Profile
@noelwelsh
noelwelsh / Enum.scala
Created April 30, 2016 06:18
Attempt at extracting the names of types of the leaves of an algebraic data type
package underscoreio
package enum
import shapeless._
import shapeless.labelled._
import shapeless.syntax.singleton._
// The intention of this code is to extract the names of the types at the leaves of an algebraic data type.
// E.g. for Color at the bottom of this file,
//
@noelwelsh
noelwelsh / reader.scala
Created July 26, 2016 15:02
Reader monad example
object ReaderExample {
import scalaz.Monad
import scalaz.syntax.monad._
type AccountId = Int
type Account = Int
type BetId = Int
type Bet = Int
final case class Config(
@noelwelsh
noelwelsh / gcounter.scala
Created July 27, 2016 16:01
GCounter CRDT implementation in Scala
import scalaz.Monoid
import scalaz.syntax.monoid._
import scalaz.syntax.traverse._
import scalaz.std.map._
import scalaz.std.anyVal._
import scalaz.std.string._
import scalaz.std.iterable._
import scala.language.higherKinds
/*
/* A Bounded Semi-lattice is a Monoid that is commutative and idempotent */
@noelwelsh
noelwelsh / chatbot.scala
Last active July 28, 2016 16:02
Chat room and bots using Scalaz Stream
import scalaz.stream._
import scalaz.concurrent.Task
import scalaz.stream.async
object ChatBot {
val queue = async.boundedQueue[String](10)
val reader =
new Thread {
override def run(): Unit = {
@noelwelsh
noelwelsh / eventstream.scala
Created July 29, 2016 11:28
Pull-based event stream case study ala fs2
// Push vs pull evaluation
// Interpreters
// Reification
// Generalised algebraic data types
import java.util.concurrent.ArrayBlockingQueue
import scalaz.Applicative
import scalaz.syntax.applicative._ // for |@|
final case class Sink[A,B](source: EventStream[A], initialValue: B, f: (B, A) => B) {
def run: B = {
@noelwelsh
noelwelsh / functions.scala
Created April 28, 2017 17:56
Parametric equations of fun
// Functions
import cats.Monoid
import cats.implicits._
implicit object pointInstance extends Monoid[Point] {
def empty = Point.zero
def combine(x: Point, y: Point): Point =
Point(x.x + y.x, x.y + y.y)
}
@noelwelsh
noelwelsh / Instructions.txt
Created May 5, 2017 17:26
Random circles
We going to create random circles inside a circle. (This is called "generative art". If you own a beret now is a good time to put it on.)
We're going to use `map` and a new tool, `flatMap`, in a different context. The idea is to broaden how we think about these tools.
Step 1.
- How could we create a point that is randomly located within a circle? There are many ways to do this. Think of one.
- Use the following methods to create random numbers (you'll need to `import doodle.random._`)
`Random.double`
`Random.natural`
`Random.int`
@noelwelsh
noelwelsh / Website.scala
Created May 12, 2017 16:19
Simple website login implemented using the free monad
object Website {
import cats.free.Free
import cats.Comonad
import scala.io._
final case class User(username: String)
sealed trait Page
final case object Welcome extends Page
final case object TryAgain extends Page
@noelwelsh
noelwelsh / dataframe.scala
Last active May 16, 2017 17:30
DataFrame
import java.util.Date
sealed trait DataFrame[A] {
import DataFrame._
def size: Int =
this.foldLeft(0)( (s, _) => s + 1 )
def foldLeft[B](z: B)(f: (B, A) => B): B =
this match {
@noelwelsh
noelwelsh / reader-monad.md
Last active October 23, 2020 14:27
Discovering the Reader Monad

The Reader Monad

The reader monad is one solution to "dependency injection". This document shows how we might discover the reader monad when we attempt to solve this problem.

Dependency Injection

Our working definition of the dependency injection problem will be this: we have a method or function that takes certain parameters that we don't want to specify every time we call it. For example, we might have a function that gets a user given an ID and also requires a database connection.

def getUser(db: DbConnection, id: Id): User =