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 / Continuations.scala
Last active December 31, 2017 17:42
Exception Control Flow - Scala
import Spec._
import Undefined._
object Continuations {
def getUser(req: Request)(cont: User => Response): Response = {
val token = req.header.get("Authorization")
if (token.isEmpty) noToken else
if (`malformed token?`) malformedToken(token.get) else
if (`user not found?`) noUser(token.get) else
@friedbrice
friedbrice / Continuations.hs
Last active January 2, 2018 22:34
Exception Control Flow - Haskell
module Continuations where
import Project
getUser :: Request -> (User -> IO Response) -> IO Response
getUser (Request _ _ _ header) cont =
case lookup "Authorization" header of
Nothing -> return noToken
Just token ->
if is_malformed_token then return (malformedToken token) else
@friedbrice
friedbrice / failure-handling.scala
Last active December 31, 2017 17:37
Three Models of Failure Handling
// exception-passing style:
// wherein we pass an Exception back to the caller
// instead of the thing we promised them (surprise!).
class SomeFailure() extends Exception
def handleSomeFailure(err: SomeFailure): C = {
`appropriate C for this error`
}
@friedbrice
friedbrice / waterflow.hs
Last active January 18, 2018 10:18
Twitter Waterflow Problem
{-# LANGUAGE BangPatterns #-}
import Data.List (foldl')
(ex1, ans1) = ([2, 5, 1, 2, 3, 4, 7, 7, 6], 10)
(ex2, ans2) = ([2, 5, 1, 3, 1, 2, 1, 7, 7, 6], 17)
(ex3, ans3) = ([2, 5, 1, 3, 1, 2, 1, 4, 4, 3], 12)
assert :: String -> Bool -> IO ()
assert _ True = return ()
@friedbrice
friedbrice / haskell-script.hs
Last active March 2, 2018 00:21
Script Templates
#!/usr/bin/env stack
-- stack --resolver lts-10.3 --install-ghc runghc
import System.Environment (getArgs)
main = getArgs >>= traverse (\name -> putStrLn $ "Hello, " ++ name ++ "!")
@friedbrice
friedbrice / different-approach.hs
Last active May 6, 2018 20:20
Haskell I/O Unit Testing (without a PhD)
import Control.Monad (join)
----
-- Domain Objects
----
data Foo = Foo String deriving (Eq, Show)
data Bar = Bar Int deriving (Eq, Show)
----
@friedbrice
friedbrice / Memo.hs
Last active April 10, 2018 17:38
Simple memoization of Haskell functions
module Memo where
-- From package [lazyset](https://github.com/happyherp/lazyset).
-- The bounds in _lazyset.cabal_ are restrictive, so you won't be
-- able to build this with cabal or stack. You need to clone the
-- repo, change the bound on base from `base >=4.9 && <4.10` to
-- `base >= 4.9`, and then `cabal install` it locally.
import Data.Map.Lazier (Map)
import Data.Map.Lazier as Map
@friedbrice
friedbrice / Nat.hs
Last active May 3, 2018 06:01
Grokking RankNTypes
{-# LANGUAGE MultiParamTypeClasses, RankNTypes #-}
-- | RankNTypes allows one to do with values things that previously
-- required type classes. For example, here we abstract the concept
-- of a natural transformation between two functors, and provide
-- a method of producing a witness value representing a natural
-- transformation whenever an appropriate type class is in scope.
module Nat where
-- | construct a `ClassNat` with the `instance` keyword
@friedbrice
friedbrice / denotational-semantics.md
Last active May 18, 2018 05:57
Denotational Semantics in a nutshell.

Denotational Semantics in a Nutshell

First off, I am not a programming language theorist, so please comment below if I say anything that's completely off-base or misleading. I'll be happy to make the necessary corrections.

Clears throat

Operational and Logical Semantics

@friedbrice
friedbrice / Delimit.hs
Created June 3, 2018 02:36
Delimit and Rejoin List
module Delimit where
import Data.Char (isLetter)
delimit :: (a -> Bool) -> [a] -> ([a], [[a]])
delimit p = foldr step ([], [[]]) where
step c (ds, w:ws) = if p c then (c:ds, []:w:ws) else (ds, (c:w):ws)
rejoin :: [a] -> [[a]] -> [a]
rejoin ds ws = foldr (\(w, d) acc -> w ++ d:acc) [] (zip ws ds) ++ last ws