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
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 |
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 |
// 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` | |
} |
{-# 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 () |
#!/usr/bin/env stack | |
-- stack --resolver lts-10.3 --install-ghc runghc | |
import System.Environment (getArgs) | |
main = getArgs >>= traverse (\name -> putStrLn $ "Hello, " ++ name ++ "!") |
import Control.Monad (join) | |
---- | |
-- Domain Objects | |
---- | |
data Foo = Foo String deriving (Eq, Show) | |
data Bar = Bar Int deriving (Eq, Show) | |
---- |
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 |
{-# 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 |
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 |