Skip to content

Instantly share code, notes, and snippets.

View sordina's full-sized avatar

Lyndon Maydwell sordina

View GitHub Profile
import Database.HDBC
import Database.HDBC.Sqlite3
import qualified Data.Convertible.Base as C
get :: forall a. C.Convertible SqlValue a => [[SqlValue]] -> [[a]]
get sql = map f sql
where f row = map fromSql row
main = do
connection <- connectSqlite3 "development.sqlite3"
Application_user = Settings[:authentication][:user]
Application_pass = Settings[:authentication][:password]
def protected! username = Application_user, password = Application_pass
unless authorized? username, password
response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth")
throw(:halt, [401, "Not authorized\n"])
end
end
class Matrix m a | -- * Voodoo goes here?
where
-- required
fromRows :: [[a]] -> m a
rows :: m a -> Integer
columns :: m a -> Integer
at :: m a -> Integer -> Integer -> a
-- defaults
toList :: m a -> [a]
{-# LANGUAGE MultiParamTypeClasses, FlexibleContexts #-}
{- -- Has the error:
Matrix.hs:33:49:
Kind error: `m' is applied to too many type arguments
In the type `m (Maybe a)'
In the type `Integer -> m (Maybe a)'
In the type `Integer -> Integer -> m (Maybe a)'
--}
module Matrix (
Matrix,
fromRows,
toList,
toListWithPos,
toRows,
rows,
columns,
at,
neighbours,
require 'rubygems'
require 'rack'
require 'sinatra'
disable :show_errors
not_found do
status 200
"not found"
end
fac 0 = 1
fac n = n * fac (n - 1)
@sordina
sordina / N_eq_NP.hs
Created January 31, 2011 16:05
Complete Bijective of Naturals and Natural Pairs
import Control.Monad.Logic
import Data.Maybe
import Data.List
import Data.Numbers.Primes
import Test.QuickCheck
-- This program demonstrates a mapping between the pairs of natural numbers and a (nonstrict?) subset of co-primes.
-- The property should hold for all sized lists, not just pairs. Ordering is preserved.
-- Question: Are we able to compress the range to create a bijection?
-- Answer: Yes! We can use the breadth-wise indces of the products, rather than the products themselves.
@sordina
sordina / Sewn Threads
Created February 4, 2011 09:35
Run a list of IO actions simultaneously, then wait for them all to finish, returning a list of their results.
import Control.Concurrent
import Random
main = print =<< threadAndJoin (replicate 10 randomAction)
randomAction = do
x <- randomRIO (0::Double,2)
threadDelay $ floor $ x * (10 ** 6)
print x >> return x
@sordina
sordina / ANKoS - Rule 110
Created February 13, 2011 16:39
An OpenGL rendering of Wolfram's 1D celular automaton example on page 32 of A New Kind of Science - Rule 110
import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
import Data.IORef
main = do
pixels <- newIORef automata
getArgsAndInitialize
createWindow "1D Finite Automata"
initialDisplayMode $= [DoubleBuffered]
initialWindowSize $= Size 400 400