Skip to content

Instantly share code, notes, and snippets.

View tbrown1979's full-sized avatar

Taylor William Brown tbrown1979

View GitHub Profile
@tbrown1979
tbrown1979 / frontend-ws-connection.ts
Created October 11, 2019 14:53 — forked from jsdevtom/frontend-ws-connection.ts
kubernetes-ingress websockets with nodejs
export const ws = webSocket<WebsocketMessage>(`wss://${location.hostname}:${location.protocol === 'https:' ? 443 : 80}/ws/`);
export const wsObserver = ws
.pipe(
retryWhen(errors =>
errors.pipe(
delay(1000)
)
)
);
@tbrown1979
tbrown1979 / example.md
Created November 2, 2017 15:58 — forked from mpilquist/example.md
Properly scheduling effect evaluation in FS2

TL;DR - Use fs2.time.sleep_[Task](delay) ++ Stream.eval(effect) instead of Stream.eval(effect.schedule(delay)).

FS2 never interrupts evaluation of an effect. This can lead to surprising behavior when using the schedule method on Task. Consider this test driver:

def testInterruption[A](effect: Stream[Task, A]): Stream[Task, A] = {
  val logStart = Stream.eval_(Task.delay(println("Started: " + System.currentTimeMillis)))
  val logFinished = Stream.eval_(Task.delay(println("Finished: " + System.currentTimeMillis)))
  val interruptSoonAfterStart =
 Stream.eval(async.signalOf[Task,Boolean](false)).flatMap { cancellationSignal =&gt;
@tbrown1979
tbrown1979 / ShapelessCoproduct.scala
Created May 19, 2017 16:02 — forked from fancellu/ShapelessCoproduct.scala
Examples with Shapeless 2.0, easier to understand from examples. Taken from docs, more examples/comments added etc
// Coproduct is extension of Either concept, to N multually exlusive choices
type ISB = Int :+: String :+: Boolean :+: CNil
val isb = Coproduct[ISB]("foo") //> isb : qaaz.ISB = foo
isb.select[Int] //> res0: Option[Int] = None
isb.select[String] //> res1: Option[String] = Some(foo)
@tbrown1979
tbrown1979 / FSM.scala
Created April 29, 2017 23:24 — forked from knutwalker/FSM.scala
Simple Encoding of a purely functional Finite State Machine using Scala and scalaz
package example.statemachine
import scalaz.{State, Scalaz}, Scalaz._
object FSM {
def apply[I, S](f: PartialFunction[(I, S), S]): FSM[I, S] =
new FSM((i, s) => f.applyOrElse((i, s), (_: (I, S)) => s))
private def states[S, O](xs: List[State[S, O]]): State[S, List[O]] =
xs.sequence[({type λ[α]=State[S, α]})#λ, O]
@tbrown1979
tbrown1979 / MonadADT.scala
Created April 28, 2017 02:34 — forked from travisbrown/MonadADT.scala
Sequencing through Monad with ADT
// Given the following ADT:
sealed trait Status
case object One extends Status
case object Two extends Status
case object Three extends Status
// They represent states. The natural progression, for this
// example, is : One -> Two -> Three
@tbrown1979
tbrown1979 / Flip.scala
Created April 17, 2017 01:33 — forked from PaulAtBanno/Flip.scala
Tasks to Task of ValidationNel
import scalaz._, Scalaz._
import scalaz.concurrent.Task
object flip {
def flip: String = {
val rnd = new java.util.Random()
if (rnd.nextInt(2) == 1) {
"Heads"
} else {
throw new RuntimeException("Tails")
@tbrown1979
tbrown1979 / .ctags.conf
Created October 23, 2016 23:53 — forked from fgeller/.ctags.conf
ctags regexs for scala
--langdef=Scala
--langmap=Scala:.scala
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*class[ \t]+([a-zA-Z0-9_]+)/\4/c,classes/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*object[ \t]+([a-zA-Z0-9_]+)/\4/c,objects/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*case class[ \t]+([a-zA-Z0-9_]+)/\4/c,classes/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*case object[ \t]+([a-zA-Z0-9_]+)/\4/c,objects/
--regex-scala=/^[ \t]*((abstract|final|sealed|implicit|lazy)[ \t]*)*(private|protected)?[ \t]*trait[ \t]+([a-zA-Z0-9_]+)/\4/t,traits/
--regex-scala=/^[ \t]*type[ \t]+([a-zA-Z0-9_]+)/\1/T,types/
--regex-scala=/^[ \t]*(override)?[ \t]*(private|protected)?[ \t]*((abstract|final|sealed|implicit|lazy)?[ \t]*)def[ \t]+([a-zA-Z0-9_]+)/\5/m,methods/

Advanced Functional Programming with Scala - Notes

Copyright © 2017 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
package freedom
import scala.collection.JavaConverters._
import java.awt.Color
import java.awt.image.BufferedImage
import cats.data.Coproduct
import cats.free.{ Free, Inject }
@tbrown1979
tbrown1979 / gist:6440126
Created September 4, 2013 17:30
IntSet from Functional Programming Coursera. Help me understand the union function!
abstract class IntSet {
def incl(x: Int): IntSet
def contains(x: Int): Boolean
def union(other: IntSet): IntSet
}
class Empty extends IntSet {
def incl(x: Int): IntSet = new NonEmpty(x, new Empty, new Empty)
def contains(x: Int): Boolean = false