Skip to content

Instantly share code, notes, and snippets.

View sordina's full-sized avatar

Lyndon Maydwell sordina

View GitHub Profile
#!/usr/bin/env ruby
class Array
def transpose
width = self.map {|e| e.length}.max
trans = []
(0..width - 1).each do |col|
self.each do |row|
element = row[col]
(trans[col] ||= []) << element
@sordina
sordina / The Power of Arrows
Created May 17, 2011 08:07
How many arrow laws am I breaking?
{-# Language TypeOperators #-}
import Prelude hiding ((.), id)
import Control.Arrow
import Control.Category
{-
> let math = ("add 2" :-: (+2)) . ("times 3" :-: (*3)) >>> ("invert" :-: (1/))
> math
(invert) . ((add 2) . (times 3))
@sordina
sordina / Closable Chan Example.hs
Created May 24, 2011 17:38
Example of closable channels using forkJoin and random sleeps.
import Closable
import ForkJoin
import Control.Concurrent
import System.Random
import Control.Monad
main = do
c <- newChan :: IO (Chan (Maybe String))
forkIO $ getChanWhileJust c >>= mapM_ print
@sordina
sordina / dragon.hs
Created June 30, 2011 07:18
Dragon Curve ASCII
{-
Plotting the dragon curve
=========================
Console Version
http://en.wikipedia.org/wiki/Lindenmayer_system
-}
import Control.Monad
import Control.Arrow
@sordina
sordina / dragon2.hs
Created July 1, 2011 09:49
Dragon Curve Image
{-
Plotting the dragon curve
=========================
Image Version
http://en.wikipedia.org/wiki/Lindenmayer_system
-}
import Control.Arrow
import Diagrams.AST hiding (X,Y)
@sordina
sordina / dragon_game.hs
Created July 1, 2011 11:57
Dragon Curve ASCII Game
{-
Plotting the dragon curve
=========================
Console Version
http://en.wikipedia.org/wiki/Lindenmayer_system
-}
import System
import System.Random
@sordina
sordina / spiral.hs
Created July 13, 2011 14:56
Spiral
import Diagrams.AST
import Control.Arrow
import Data.List
main = outputImage "Foo.png" 800 800 image
image = Images $ Atop spiral background
spiral = Modifier (Changes [Align C, Foreground red, LineWidth 0]) $ Shape $ Path Closed $ Offsets poly
@sordina
sordina / circle.hs
Created July 20, 2011 13:22
Circle Animation
import Diagrams.AST
import Control.Monad
main = forM_ [0..359] render
render i = outputImage ("image_" ++ show i ++ ".png") 100 100 $ animate (fromIntegral i)
animate t = Images $ Layers [
Modifier (Align C) $ Images $ Layers [
Modifier (Rotate $ Degrees t) shape
@sordina
sordina / git-history.bash
Created August 2, 2011 11:51
View all revisions of a git versioned file at once, marked by revision and date
#!/bin/bash
file="$1"
# Date flag
if [[ "$file" == "-v" ]]
then
file="$2"
verbose=1
fi
@sordina
sordina / Mandelbrot.hs
Created August 10, 2011 12:50
Mandelbrot Image Generator using DevIL
import Data.Complex
import Codec.Image.DevIL
import Data.Array.Unboxed
import System.Directory
import Control.Monad
main = ilInit
>> doesFileExist path
>>= flip when (removeFile path)
>> writeImage path image