Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
davidallsopp / flip_text.py
Last active August 29, 2015 14:06
Flipping lines of text in a functional style, in Python 2. E.g. for reflecting ASCII art left-to-right.
import functools
def compose(*functions):
"Compose multiple functions together. See https://mathieularose.com/function-composition-in-python/"
return functools.reduce(lambda f, g: lambda x: f(g(x)), functions)
# More limited case of just two functions:
#def compose2(f, g):
# return lambda x: f(g(x))
@davidallsopp
davidallsopp / mapm.hs
Last active August 29, 2015 14:07
A brief REPL session explaining MapM and MapM_, adapted from LYAH (http://learnyouahaskell.com/input-and-output)
-- Let's say that we want to print out each of a list of items:
λ>map print [1,2,3]
<interactive>:13:1:
No instance for (Show (IO ())) arising from a use of ‘print’
In a stmt of an interactive GHCi command: print it
-- That doesn't work!...
@davidallsopp
davidallsopp / fibonacci.hs
Created October 11, 2014 21:23
A GHCi session showing the classic lazy recursive Fibonacci implementation in Haskell.
λ> let fib = 0: 1: zipWith (+) fib (tail fib)
λ> take 10 fib
[0,1,1,2,3,5,8,13,21,34]
λ> fib !! 100
354224848179261915075
λ> fib !! 1000
434665576869374564356885276750406258025646605173717804024817290895365554179490518904038798400792551692959225930803226347
@davidallsopp
davidallsopp / ComposeFunctors.hs
Last active April 23, 2017 10:34
Composing functors using Data.Functor.Compose - thanks to Tony Morris for his presentation on Monad Transformers at http://vimeo.com/73648150
λ> import Data.Functor.Compose
-- List of lists
λ> getCompose $ fmap (+1) $ Compose [[],[1],[2,3],[4,5,6]]
[[],[2],[3,4],[5,6,7]]
-- List of Maybe
λ> getCompose $ fmap (+1) $ Compose [Just 1, Just 2, Nothing, Just 3]
@davidallsopp
davidallsopp / ReaderMonad.hs
Created October 24, 2014 14:50
Simple example of the Reader Monad (in Haskell)
module ReaderMonad where
import Control.Monad.Reader
stuff :: Reader Int String
stuff = do
s <- ask
return (show s ++ " green bottles")
main :: IO ()
@davidallsopp
davidallsopp / WriterMonad.hs
Created October 24, 2014 14:52
Simple example of the Writer Monad (in Haskell), adapted from LYAH (http://learnyouahaskell.com)
module WriterMonad where
-- From http://learnyouahaskell.com/for-a-few-monads-more
-- This example no longer works without tweaking - see
-- http://stackoverflow.com/questions/11684321/how-to-play-with-control-monad-writer-in-haskell
-- just replace the data constructor "Writer" with the function "writer" in the line marked "here"
-- That changed with mtl going from major version 1.* to 2.*, shortly after LYAH and RWH were written
import Control.Monad.Writer
@davidallsopp
davidallsopp / TrivialMonad.hs
Last active August 29, 2015 14:08
Implementing the Trivial Monad in haskell, including Applicative and Functor instances (required by recent GHC versions)
module TrivialMonad where
-- See also http://hackage.haskell.org/package/transformers-0.4.1.0/docs/src/Data-Functor-Identity.html#Identity
import Control.Applicative (Applicative(..)) -- import type and all exported constructors using '(..)'
-- How is that different from "import Control.Applicative"
import Control.Monad (liftM, ap)
data Trivial a = Trivial a deriving (Show) -- wrapper type
@davidallsopp
davidallsopp / FindFiles.hs
Created November 10, 2014 10:35
Recursively finding files in (cross-platform) Haskell
module FindFiles where
import System.FilePath.Find
-- Can't install FileManip on Windows because it depends on "unix", which (surprise!) can't be installed
-- on Windows without Cygwin etc. Instead, install filemanip not FileManip (obviously ?!)
-- which depends on unix-compat, which is fine on Windows
main :: IO ()
main = do
@davidallsopp
davidallsopp / ListZipContents.hs
Last active August 29, 2015 14:09
List the content of a ZIP archive using the Haskell zip-archive package. However, zip-archive seems to have some major problems with large archives (see -- https://www.haskell.org/pipermail/haskell-cafe/2010-August/081772.html and https://groups.google.com/forum/?hl=en#!topic/haskell-cafe/xGkjShpReI), and generally seems to have been abandoned i…
module ZipExample where
-- https://hackage.haskell.org/package/zip-archive
-- https://hackage.haskell.org/package/zip-archive-0.2.3.5/docs/Codec-Archive-Zip.html
-- Example usage: https://github.com/jgm/zip-archive/blob/master/Zip.hs
import Codec.Archive.Zip
import qualified Data.ByteString.Lazy as B
import Control.Applicative ( (<$>) )
@davidallsopp
davidallsopp / ZipArchive1.hs
Last active August 29, 2015 14:09
Listing entries in a ZIP archive using zip-conduit in Haskell (https://hackage.haskell.org/package/zip-conduit-0.2.2.1/docs/Codec-Archive-Zip.html)
module ZipConduit1 where
-- requires zip-conduit package
-- slightly adapted from the docs at:
-- https://hackage.haskell.org/package/zip-conduit-0.2.2.1/docs/Codec-Archive-Zip.html
import Codec.Archive.Zip
main :: IO ()
main = do