This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_example(): | |
assert False, "A failing test" | |
if __name__ == "__main__": | |
tests = [(k, v) for k, v in locals().items() if k.startswith("test")] | |
print("%s TESTS FOUND" % len(tests)) | |
for k, v in tests: | |
print(k) | |
v() | |
print("TESTS COMPLETE") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> import Data.List | |
Prelude Data.List> let dropEach xs = zipWith (++) (inits xs)(tail $ tails xs) | |
Prelude Data.List> dropEach [1,2,3,4,5] | |
[[2,3,4,5],[1,3,4,5],[1,2,4,5],[1,2,3,5],[1,2,3,4]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad.State | |
:{ | |
let test :: State Int Int | |
test = do | |
put 3 | |
modify (+1) | |
get | |
:} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> data Point = Point {x :: Int} deriving Show | |
Prelude> Point 1 | |
Point {x = 1} | |
Prelude> :t x | |
x :: Point -> Int | |
Prelude> :t Point | |
Point :: Int -> Point |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ArrowExamples where | |
import Control.Arrow | |
-- Based on https://en.wikibooks.org/wiki/Haskell/Understanding_arrows | |
-- function composition: | |
composed :: Int -> Int | |
composed = succ >>> (*3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<style> | |
.spacer { | |
height: 50px; /* Just to confirm our positions are relative to the event list not the window */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Poll repeatedly. Use setTimeout rather than setInterval | |
// to ensure previous execution has completed before enqueing a new one | |
(function repeat() { | |
do_something(); | |
setTimeout(repeat, 2000); | |
})(); | |
// The repeat function's scope is limited, so one can have multiple blocks like this | |
// without them interfering - and can wrap this in a helper function in the style of |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List (isPrefixOf) | |
numerals = [("M",1000),("CM",900),("D",500),("CD",400),("C",100),("XC",90), | |
("L",50),("XL",40),("X",10),("IX",9),("V",5),("IV",4),("I",1)] | |
toArabic :: String -> Int | |
toArabic "" = 0 | |
toArabic str = num + toArabic xs | |
where (num, xs):_ = [ (num, drop (length n) str) | (n,num) <- numerals, isPrefixOf n str ] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List (find, unfoldr) | |
import Control.Arrow (second) | |
import Control.Applicative ((<$>)) -- for second version | |
numerals :: [(String, Int)] | |
numerals = [("M",1000),("CM",900),("D",500),("CD",400),("C",100),("XC",90), | |
("L",50),("XL",40),("X",10),("IX",9),("V",5),("IV",4),("I",1)] | |
romanize :: Int -> String | |
romanize = concat . unfoldr next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MonadicIO where | |
-- For pure FP, functions must be pure | |
-- So IO-related functions must return a value rather than having a side-effect | |
-- e.g. reading a line: getLine gives us an IO String (an IO that, when executed, would produce a String) | |
myGetLine :: IO String | |
myGetLine = getLine | |
-- e.g. writing a line: putStrLn accepts a String, and produces an IO () that, when executed, has an effect |