Skip to content

Instantly share code, notes, and snippets.

View friedbrice's full-sized avatar
🔵
You have unread notifications

Daniel P. Brice friedbrice

🔵
You have unread notifications
View GitHub Profile
@friedbrice
friedbrice / CLArgs.scala
Created June 27, 2017 08:32
Zero-dependency Scala Command Line Parser
object CommandLineDemo extends App {
val clargs: CLArgs = CLArgs(args)
val all: Option[Unit] = clargs.flag('a', "all")
val list: Option[Unit] = clargs.shortFlag('l')
val ctl: Option[Unit] = clargs.longFlag("show-control-chars")
val tab: Option[String] = clargs.value('T', "tabsize")
println(s"$all-$list-$ctl-$tab")
@friedbrice
friedbrice / EitherMonad.scala
Last active September 28, 2017 20:36
Scala Error Handling with Either
import scala.collection.generic.CanBuildFrom
import scala.collection.{TraversableLike, mutable}
import scala.reflect.ClassTag
object EitherMonad {
trait Error[E] extends ClassTag[E] {
def getDefault: E = fromMessage("")
def fromMessage(msg: String): E
def fromThrowable(err: Throwable): E = fromMessage(err.toString)
@friedbrice
friedbrice / 1-tomagachi.ts
Last active October 3, 2017 18:08
Patterns without Interfaces
interface Pet {
left();
right();
}
class Dog extends Pet {
left() { bark(); }
right() { run(); }
bark() { ... }
@friedbrice
friedbrice / teaching-folds.scala
Last active October 16, 2017 16:40
Teaching Folds for Understanding
// Prerequisits:
// - pattern matching (first half of Ch. 4 in LYAHFGG),
// - tail recursion vs non-tail recursion (first half of
// Ch. 1 of SICP).
// 1. Clearly and unambiguously define the problem
// (including the symbol `List').
sealed abstract class List[A]
case class Empty[A]() extends List[A]
@friedbrice
friedbrice / sample.graphql
Created October 28, 2017 02:29
Sample GraphQL Spec
type Query {
allPersons(last: Int): [Person!]!
allPosts(last: Int): [Post!]!
}
type Mutation {
createPerson(name: String!, age: String!): Person!
updatePerson(id: ID!, name: String!, age: String!): Person!
deletePerson(id: ID!): Person!
createPost(title: String!): Post!
@friedbrice
friedbrice / maintainable.scala
Last active October 31, 2017 15:43
My Attempt at "Refactor for Maintainability" by S. Shubin (https://www.youtube.com/watch?v=b8EB31voC9U)
import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.{Path, Paths}
import java.time.{Duration, Instant}
object Lib {
def app(
filename: String,
getTime: Unit => Instant,
readFile: Path => Array[Byte],
@friedbrice
friedbrice / TailRecursiveTreeFold.scala
Created November 16, 2017 01:18
Tail Recursive Tree Fold
object TailRecursiveTreeFold {
sealed trait Tree[A]
case class Leaf[A](value: A) extends Tree[A]
case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]
def fold[A, B](tree: Tree[A],
f: A => B,
g: (B, B) => B): B = {
import math
def f(T,Y):
"""An example first-order ODE, in the form y' = f(t,y).
Arguments:
T -- the independent variable
Y -- the dependent variable
"""
return math.exp(T)*math.sin(Y)
@friedbrice
friedbrice / EulerApproximations.hs
Created November 17, 2017 01:55
Euler Approximations
module EulersMethod where
type R = Double
type ODE = (R, R) -> R
next :: ODE -> R -> (R, R) -> (R, R)
next y' dx (x, y) = (x + dx, y + y' (x, y) * dx)
eulers :: ODE -> R -> (R, R) -> [(R, R)]
eulers y' dx (x, y) = (x, y) : eulers y' dx (next y' dx (x, y))
@friedbrice
friedbrice / EulersMethod.hs
Last active November 17, 2017 05:54
Euler's Method
module EulersMethod where
type R = Double
type ODE = (R, R) -> R
----
-- Direct Approach
----
euler1 :: ODE -> (R, R) -> R -> Int -> [(R, R)]