Skip to content

Instantly share code, notes, and snippets.

@mattyw
mattyw / fib.clj
Created October 31, 2010 17:47
An example from Bruce Tate's 7 languages in 7 weeks, Fib numbers in clojure. Try running this with (nth-fib 500). Very fast
(defn fib-pair [[a b]] [b (+ a b)])
(defn nth-fib [n]
"Return the nth fib number"
(nth (map first (iterate fib-pair [0 1])) n))
@mattyw
mattyw / multi_proc_test.py
Created November 3, 2010 17:01
This simple module will start three processes that just count up forever, they will each run in a separate interpreter so won't be affected by the GIL. When you stop the main process the sub processes will die (after a delay of 5 seconds). I'm trying o
import time
import sys
from multiprocessing import Process, Pipe
def test_proc(name, conn):
x = 0
while True:
#print x
x += 1
if conn.poll(10):
conn.recv()
@mattyw
mattyw / Python Fizzbuzz
Created July 16, 2011 11:51
The Fizzbuzz kata in Python
import unittest
def fizzbuzz(value):
if value % 5 == 0 and value % 3 == 0:
return 'fizzbuzz'
elif value % 5 == 0:
return 'buzz'
elif value % 3 == 0:
return 'fizz'
else:
@mattyw
mattyw / fact_recur.py
Created July 21, 2011 20:46
When you're bored, do some recursion
def fact(x):
if x == 0:
return 0
elif x == 1:
return 1
else:
return x * fact(x-1)
for x in range(0,10):
print x, fact(x)
@mattyw
mattyw / about.md
Created August 9, 2011 19:12 — forked from jasonrudolph/about.md
Programming Achievements: How to Level Up as a Developer
@mattyw
mattyw / fizzbuzz.hs
Created September 13, 2011 19:49
Fizzbuzz in Haskell using guards
import Test.HUnit
fizzbuzz :: Int -> String
fizzbuzz x
| x `mod` 15 == 0 = "fizzbuzz"
| x `mod` 3 == 0 = "fizz"
| x `mod` 5 == 0 = "buzz"
| True = show x
@mattyw
mattyw / shelving.py
Created September 14, 2011 20:36
Python Shelving
def add(x, y):
return x + y
def subtract(x, y):
return x - y
import shelve
d = shelve.open('shelve_state.dat')
@mattyw
mattyw / one_liners.io
Created October 8, 2011 20:45
10 Io one liners
// 1. Multiply Each Item in a List by 2
list(1,2,3,4,5) map(v, v*2) println
// 2. Sum a List of Numbers
list(1,2,3,4,5) reduce(+) println
// 3. Verify if a Word Exists in a String
@mattyw
mattyw / sieve.dart
Created October 10, 2011 10:27
Sieve of Eratosthenes in Dart. It's a first pass and in desperate need of refactoring but it works for now.
var expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113];
compareLists(lista, listb) {
if (lista.length == listb.length) {
for(var i = 0; i < lista.length; i ++) {
if (lista[i] != listb[i]) {
return false;
}
}
return true;
;; mattyw's solution to Reverse a Sequence
;; https://4clojure.com/problem/23
(defn reverse-func [seq]
(reduce (fn [a b] (cons b a)) '() seq))
(println (= (reverse-func [1 2 3 4 5]) [5 4 3 2 1]))