Skip to content

Instantly share code, notes, and snippets.

@rrees
rrees / MockRandomExample.py
Created January 12, 2009 12:37
Example of how to use Mock to check interactions with the Random module
from mock import Mock, patch_object
import random
mock = Mock()
class MyTest(unittest.TestCase):
@patch_object(random, 'shuffle', mock)
def test_shuffling(self):
thing_that_should_shuffle()
class MyClass {
void tweeter() {
println "Tweet, tweet"
}
}
def my_object = new MyClass()
class MyClass {
Closure aMethod
MyClass() {
aMethod = {println "Default"}
}
}
def myObject = new MyClass(aMethod: { println "Hello"})
class MyClass
def my_method
puts "Hello"
end
end
def with_method(object, method_name, replacement_method, &block)
old_method = object.method(method_name)
object.class.class_eval { define_method method_name, replacement_method}
(map #(intern *ns* %1 (fn [] println %2)) ['north 'south] ["North" "South"])
import java.util.Random
random = Random.new
def useRandom
puts random.nextInt(5)
end
class MyClass {
def methodMissing (String name, Object args) {
println("Method missing was invoked for the call ${name}")
}
}
g = new MyClass()
g.attr()
g.attr
@rrees
rrees / gist:1490542
Created December 17, 2011 15:49
Clojure code to solve largest increasing sequence 4Clojure problem
(fn
([seq] (find-inc-seq [] [] seq))
([cur-seq cur-longest-seq a-seq]
(cond
(empty? a-seq)
(let [ret (if (> (count cur-seq) (count cur-longest-seq)) cur-seq cur-longest-seq)]
(if (> (count ret) 1) ret []))
(empty? cur-seq) (find-inc-seq (conj [] (first a-seq)) cur-longest-seq (rest a-seq))
:default (if (> (first a-seq) (last cur-seq))
@rrees
rrees / gist:2819932
Created May 28, 2012 16:19
Event reducer example
(def example-story-events [
{
:title "Funding cut again"
:tags [:science]
:author ["Chuck Newton"]
}
{
:tags [:politics]
}
{
@rrees
rrees / gist:2850527
Created June 1, 2012 08:58
Core logic example
(ns euroclojure.logic
(:use [clojure.core.logic]))
(defn query []
(run 1 [q]
(membero q [1 2 3])))
;; 1
(defn query-all []