Skip to content

Instantly share code, notes, and snippets.

@erantapaa
erantapaa / redirect-io.hs
Created September 8, 2015 17:38
Redirect stdout for an IO action
module Misc
( withOutputTo
) where
import System.IO
import GHC.IO.Handle
import Control.Exception
-- | Run an IO action with stdout redirected to a file.
-- Restores stdout upon completion of the action.
@erantapaa
erantapaa / SplitFile.hs
Created September 8, 2015 18:00
split line oriented file into equal parts on line boundaries
module SplitFile
( splitFile, splitFileOn, splitHandle, splitHandleOn, findNext, findNewLine
) where
import System.IO
import qualified Data.ByteString.Char8 as B
splitFileOn :: (Char -> Bool) -> Int -> FilePath -> IO [Integer]
splitFileOn find parts path = do
h <- openBinaryFile path ReadMode
@erantapaa
erantapaa / 0_reuse_code.js
Last active September 8, 2015 18:02
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@erantapaa
erantapaa / parser-ghc-7.10.hs
Created September 8, 2015 20:24
Parser Combinators by Hutton and Meijer
-- Updated for GHC 7.10
{---------------------------------------------------------------------
A HASKELL LIBRARY OF MONADIC PARSER COMBINATORS
17th April 1997
Graham Hutton Erik Meijer
University of Nottingham University of Utrecht
@erantapaa
erantapaa / gnuplot-example.hs
Created September 17, 2015 23:29
using Graphics.Gnuplot.Simple
module Lib
where
import Graphics.Gnuplot.Simple
import qualified Graphics.Gnuplot.Terminal.SVG as SVG
foo = do
let zs = [ (x,x) | x <- [1..10] ]
plotList [Key Nothing
,YRange (0,maximum (map snd zs) + 1)
@erantapaa
erantapaa / EightSisters.java
Created September 18, 2015 06:21
EightSisters marriage assignment problem
// Solution for [2015-09-11] Challenge #231 [Hard] Eight Husbands for Eight Sisters
//
// https://www.reddit.com/r/dailyprogrammer/comments/3kj1v9/20150911_challenge_231_hard_eight_husbands_for/
//
// NOTE: This is ugly Java - lots of global variables, etc.
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
@erantapaa
erantapaa / python-2.7-unicode.md
Last active September 23, 2015 16:20
python 2.7 unicode

My quick summary of Python 2.7 strings and Unicode.

Overview

Python has two string types:

  • type str
  • type unicode

Examples:

<html>
<body>
<script>
function toChar(ch) {
return (ch == '' ? ' ' : ch)
}
function next(grid) {
@erantapaa
erantapaa / enigma.lhs
Last active May 10, 2021 15:04
enigma in Haskell
Emulating an Enigma machine
===
In this gist I'll go over the development of an Enigma machine encoder
in Haskell.
Here are some useful background info about how the Enigma machine works:
- Enigma Simulator: http://enigmaco.de/enigma/enigma.html
- "How Enigma Machines Work" http://enigma.louisedade.co.uk/howitworks.html
@erantapaa
erantapaa / modular.hs
Created September 28, 2015 17:06
modular arithmetic example using Data.Reflection
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FlexibleContexts #-}
import Data.Reflection
import Data.Proxy
data M a s = M a -- Note the phantom comes *after* the concrete
-- In `normalize` we're tying the knot to get the phantom types to align
-- note that reflect :: Reifies s a => forall proxy. proxy s -> a