Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
davidallsopp / nanotest.py
Created June 25, 2015 22:03
Ultra-simple test harness for Python. Locates all functions in the current module that start with "test", and runs them.
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")
@davidallsopp
davidallsopp / dropEach.hs
Created June 24, 2015 21:59
dropEach - not original, probably taken from Stack Overflow somewhere. Returns all lists generated by removing one element from the input
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]]
@davidallsopp
davidallsopp / multiline.hs
Created June 20, 2015 20:11
Multi-line entry in GHCI
import Control.Monad.State
:{
let test :: State Int Int
test = do
put 3
modify (+1)
get
:}
@davidallsopp
davidallsopp / FieldLabels.hs
Created June 17, 2015 21:54
Constructor fields in Haskell - see https://www.haskell.org/tutorial/moretypes.html, section 6.2 Field Labels
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
@davidallsopp
davidallsopp / ArrowExamples.hs
Last active August 29, 2015 14:23
Simple examples of useful functions from Control.Arrow, via the very helpful https://en.wikibooks.org/wiki/Haskell/Understanding_arrows
module ArrowExamples where
import Control.Arrow
-- Based on https://en.wikibooks.org/wiki/Haskell/Understanding_arrows
-- function composition:
composed :: Int -> Int
composed = succ >>> (*3)
@davidallsopp
davidallsopp / d3-events.html
Created May 19, 2015 08:06
Animating HTML divs using D3 transitions and relative/absolute positioning
<!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 */
}
@davidallsopp
davidallsopp / repeat.js
Last active August 29, 2015 14:16
Repeating a function using setTimeout rather than setInterval
// 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
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 ]
@davidallsopp
davidallsopp / roman.hs
Last active February 8, 2017 14:26
Haskell version of the Roman numeral converter, using unfold. Taken from https://www.reddit.com/r/programming/comments/658ys/how_to_recognise_a_good_programmer/c02vm1j. There is a code walkthrough at http://billmill.org/roman.html. See also Scala version at https://gist.github.com/davidallsopp/6711935 and reverse program (roman numeral parser) i…
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
@davidallsopp
davidallsopp / MonadicIO.hs
Last active August 29, 2015 14:14
Starting from the assumption of pure IO, showing that the signature of bind (>>=) arises naturally as a consequence of chaining IO values together.
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