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
-- Inspired by http://www.csis.pace.edu/~bergin/patterns/ppoop.html
-- and by Rob Pike's response: https://plus.google.com/+RobPikeTheHuman/posts/hoJdanihKwb
import System.Info (os)
import Data.Map (Map, findWithDefault, fromList)
class AbstractOsObject a where
getOsString :: a -> String
coerce :: a -> BaseOsObject
@friedbrice
friedbrice / List.hs
Created November 25, 2016 03:17
Haskell `List` standard instances implemented in terms of `foldr`
data List a = Empty | Cons a (List a) deriving (Eq, Read, Ord, Show)
instance Foldable List where
foldr _ acc Empty = acc
foldr f acc (Cons x xs) = f x (foldr f acc xs)
instance Functor List where
fmap f xs = foldr (\x acc -> Cons (f x) acc) Empty xs
instance Traversable List where
@friedbrice
friedbrice / PreludeMonads.hs
Last active January 14, 2017 02:58
Naive psuedocode implementations of common Haskell monads
type [a] = [] | a : [a]
instance Monad (a ↦ [a]) where
fmap ∷ (a → b) → List a → List b
fmap _ [] = []
fmap f (x : xs) = (f x) : (fmap f xs)
pure ∷ a → List a
pure x = x : []
@friedbrice
friedbrice / classes.js
Last active December 14, 2016 08:17
ES5 Classes vs. ES6 Classes (vs. Java Classes vs. Scala Classes)
/* ES5-style class declaration */
function ParsedTime(hour, minute, second) {
this.hour = hour
this.minute = minute
this.second = second
this.toString = function () {
return JSON.stringify(this)
}
}
@friedbrice
friedbrice / fourier_tools.py
Created January 13, 2017 07:09
Work These Days
def fourier_series(n, samples):
"""
`fourier_series`
From a list of `samples` taken from an unknown periodic function,
returns the best-fit `n`-term sine/cosine approximation of the
unknown function.
Args:
param1 (int): The number of terms you want in your
@friedbrice
friedbrice / ServerConfig.scala
Last active January 14, 2017 02:41
Server Config Object (hypothetical implementation and usage)
package local
import scalaz.Monoid._
object ServerConfig {
case class Port(get: Int = 8080)
case class Zookeeper(get: String = "default_zookeeper")
case class Environemnt(get: String = "default_env")
@friedbrice
friedbrice / ADTs.scala
Created January 15, 2017 04:28
Some ADTs in Scala.
sealed trait Either[S, T] {
def fold[X]: (S => X) => (T => X) => X
def map[X]: (T => X) => Either[S, X]
def bind[X]: (T => Either[S, X]) => Either[S, X]
}
object Either {
import System.Environment (getArgs)
main = getArgs >>= (putStrLn . triangle . read . head)
where
triangle n = unlines $ map mkline $ [1..n]
mkline i = unwords $ take i $ drop (i `mod` 2) $ altern
altern = "0" : "1" : altern
@friedbrice
friedbrice / Either.java
Last active May 16, 2017 22:04
Either.java
import java.util.Optional;
import java.util.function.Function;
public abstract class Either<L, R> {
public static <L0, R0> Either<L0, R0> left(L0 value) {
return new Left<L0, R0>(value);
}
public static <L0, R0> Either<L0, R0> right(R0 value) {
@friedbrice
friedbrice / Fibs.hs
Created June 11, 2017 07:06
Two Efficient Fibonacci Algorithms
data Mat a = M !a !a !a !a deriving (Eq, Read, Show)
-- 2x2 matrix multiplication
matMult :: Num a => Mat a -> Mat a -> Mat a
matMult (M x11 x12 x21 x22) (M y11 y12 y21 y22) = M z11 z12 z21 z22
where
z11 = x11*y11 + x12*y21
z12 = x11*y12 + x12*y22
z21 = x21*y11 + x22*y21
z22 = x21*y12 + x22*y22