Skip to content

Instantly share code, notes, and snippets.

@Newky
Newky / prolog.js
Created September 19, 2011 16:56
Bit of prolog like magic with JavaScript
/*Prolog Num succ and prev defined in some wierd ass JS */
var num= (function(num) {
num = num || 0;
var x = num;
return {
s: function() {
x+=1;
return x;
},
@Newky
Newky / xando.hs
Created December 22, 2011 23:10
Sample Paper Haskell Q4.
import Data.List
data Piece = X | O deriving (Ord, Eq, Show)
data Board = Board [(Int, Int, Piece)] deriving(Ord, Eq,Show)
winning_combinations = [[(a,0), (a,1), (a,2)] | a <- [0..2] ] ++ [[(0,a), (1,a), (2,a)] | a <- [0..2] ] ++ [[(0,0), (1,1), (2,2)], [(0,2), (1,1), (2,0)]]
over2 :: Board -> Bool
over2 (Board []) = False
over2 (Board x) = is_winner newx winning_combinations
@Newky
Newky / HaskellRandom.hs
Created January 3, 2012 10:59
Haskell random generator demonstrating state monad.
import System.Random
newtype State s a = State {
runState :: s -> (a, s)
}
returnState :: a -> State s a
returnState a = State (\s-> (a, s))
bindState :: State s a -> (a -> State s b) -> State s b
@Newky
Newky / state.hs
Created January 7, 2012 19:18
State Haskell
data State s a = State (s -> (a, s))
test :: Int -> State Int () -> State Int () -> State Int Bool
test n f g = (f `bindState` (\_ -> g) `bindState` (\_ -> State $ (\s -> ((s > n), s))))
runState (State t) s = t s
returnState :: a -> State s a
returnState a = State $ (\s -> (a, s))
@Newky
Newky / lines.py
Created May 22, 2012 15:13
Little script to tell you where you are going over the 80 char limit. omits docstrings
#!/usr/bin/env python
#usage: lines.py filenames seperated by space
#my personal way of using it: lines.py `git diff --no-ext-diff --name-only HEAD~1 HEAD | grep .py`
import sys
TAB_WIDTH = 8
LINE_LENGTH = 79
def count(line):
@Newky
Newky / gist:5151640
Created March 13, 2013 12:30
restoring cwd with a decorator.
def restore_cwd(func):
"""
decorator to restore the current working directory
"""
def wrapper(*args, **kwargs):
cwd = os.getcwd()
results = func(*args, **kwargs)
os.chdir(cwd)
return results
@Newky
Newky / roll.py
Created March 27, 2013 20:30
Probability rolls for gav guidance
import random
def roll_dice():
return random.randint(1, 6)
def sum_two_dice():
sum_one = roll_dice()
sum_two = roll_dice()
@Newky
Newky / NiceNumber.py
Created May 7, 2013 21:24
A fun little python class which allows you to write big numbers with commas for readability. Just for fun, not to be used for anything serious.
class NiceNumber(long):
def __new__(cls, *args):
x = 0
if long(args[0]) >= 0:
sign = 1
else:
sign = -1
for i in range(0, len(args)):
reverse_i = (len(args) - 1) - i
x += (1000 ** i) * abs(long(args[reverse_i]))
@Newky
Newky / dictbeingadick.py
Created May 11, 2013 17:35
Python dict weirdness.
# in settings.py
USERS: {
"alice": {
"alice_specific_information" : True
},
"bob": {
"bob_specific_information": True
}
}
@Newky
Newky / Hooked-usage.sh
Created May 21, 2013 21:02
Write-up for blog post.
$ hooked.py --help
Usage: hooked.py [options]
Options:
-h, --help show this help message and exit
--git-root=GITROOT the root directory of the git folder to inject hook
--clean Clean up after yourself
$ # lets make a new repo