Skip to content

Instantly share code, notes, and snippets.

@HeinrichApfelmus
HeinrichApfelmus / RandomAccessParser.hs
Last active March 21, 2020 13:11
Parsing a random-access format into a pure data structure. Parsing will be lazy: parts of the data structure will be parsed on demand.
import Data.Word
import qualified Data.ByteString as B
type ByteString = B.ByteString
data Tree = Leaf [Word8] | Branch Tree Tree deriving (Eq,Show)
parse :: ByteString -> Tree
parse xs = case view xs of
Cons 0 xs -> case view xs of
Cons length xs -> Leaf . B.unpack $ B.take (fromIntegral . toInteger $ length) xs
@HeinrichApfelmus
HeinrichApfelmus / GameLoop.hs
Created October 2, 2012 16:49
Game loop in reactive-banana
{------------------------------------------------------------------------------
reactive-banana
Implementation of an "industry strength" game loop with fixed time step
and variable fps.
See also http://gafferongames.com/game-physics/fix-your-timestep/
-------------------------------------------------------------------------------}
{-# LANGUAGE NoMonomorphismRestriction #-}
module Main where
@HeinrichApfelmus
HeinrichApfelmus / 01_ExampleMonadic.hs
Last active October 11, 2018 14:05
Chart library examples — monadic vs pure API
plotSpectrum1 lattice n m = toFile def file $ do
layout_title .= "Spectrum of '" ++ name ++ "'"
layout_x_axis . laxis_title .= "momentum k (" ++ show m ++ " points)"
layout_y_axis . laxis_title .= "energy E"
setColors $ colorGradient blue (length rows)
forM_ rows $ \row -> plot (line "band" [row])
where
...
@HeinrichApfelmus
HeinrichApfelmus / gist:2187593
Created March 24, 2012 20:42
BIjection between Twan van Laarhoven's Pipe and the data types from Conduit
-- http://www.reddit.com/r/haskell/comments/rbgvz/conduits_vs_pipes_using_void_as_an_input_or/
import Control.Monad
data Pipe m i o r =
NeedInput (i -> Pipe m i o r) (Pipe m () o r)
| HaveOutput (Pipe m i o r) (m ()) o
| Finished (Maybe i) r
| PipeM (m (Pipe m i o r)) (m r)
@HeinrichApfelmus
HeinrichApfelmus / ipython.log
Created January 20, 2014 14:17
Log of a failed installation of IPython when running `IHaskell notebook`.
------------------------------------------------------------
/Users/apfelmus/.ihaskell/ipython/bin/pip run on Sun Jan 19 23:56:41 2014
Obtaining ipython from git+https://github.com/ipython/ipython.git@9c922f54af799704f4000aeee94ec7c74cada194#egg=ipython-dev
Cloning https://github.com/ipython/ipython.git (to 9c922f54af799704f4000aeee94ec7c74cada194) to /Users/apfelmus/.ihaskell/ipython/src/ipython
Found command 'git' at '/opt/local/bin/git'
Running command /opt/local/bin/git clone -q https://github.com/ipython/ipython.git /Users/apfelmus/.ihaskell/ipython/src/ipython
Running command /opt/local/bin/git submodule update --init --recursive -q
Running command /opt/local/bin/git tag -l
@HeinrichApfelmus
HeinrichApfelmus / DynamicEventSwitching.hs
Created October 2, 2013 09:54
Create new behaviors and switch between them.
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Traversable (sequenceA)
import Reactive.Banana
import Reactive.Banana.Frameworks
newtype Wrapper = Wrapper (forall t. Moment t (Event t () -> Behavior t Integer))
main :: IO ()
@HeinrichApfelmus
HeinrichApfelmus / InputElement.hs
Created September 9, 2013 13:41
Implementation of an input element adhering to the three principles laid out in http://apfelmus.nfshost.com/blog/2012/03/29-frp-three-principles-bidirectional-gui.html
{-# LANGUAGE RecordWildCards #-}
import Data.Monoid
import Control.Monad
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny
data Input = Input
@HeinrichApfelmus
HeinrichApfelmus / SnapDebugLogChrome
Last active December 17, 2015 09:59
Web servers fail to serve large files on MacOS X.
-- debug log for querying http://localhost:100000/video.mp4 with Chrome
Listening on http://0.0.0.0:10000/
Can't open log file "log/access.log".
Exception: log/access.log: openFile: does not exist (No such file or directory)
Logging to stderr instead. **THIS IS BAD, YOU OUGHT TO FIX THIS**
Can't open log file "log/error.log".
Exception: log/error.log: openFile: does not exist (No such file or directory)
@HeinrichApfelmus
HeinrichApfelmus / ForkLift.hs
Created June 7, 2012 14:26
Forklift - a pattern for performing monadic actions in a worker thread
-- see also
-- http://apfelmus.nfshost.com/blog/2012/06/07-forklift.html
module ForkLift where
import Control.Concurrent
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.State
@HeinrichApfelmus
HeinrichApfelmus / CurrencyConverter.js
Created October 19, 2012 15:09
Program compiled with haste that fails with a JavaScript error
/* Eval
Evaluate the given thunk t into head normal form.
If the "thunk" we get isn't actually a thunk, just return it.
*/
function E(t) {
if(t instanceof Thunk) {
if(t.f) {
t.x = t.f();
t.f = 0;
}