Skip to content

Instantly share code, notes, and snippets.

View arwagner's full-sized avatar

Andrew Wagner arwagner

  • Northern Virginia
View GitHub Profile
;;;;;;;; unify.clj
(ns building-problem-solvers)
(defn unify ... )
;;;;;;; unify_test.clj
(load-file "unify.clj")
(ns building-problem-solvers
(:use clojure.test))
class Foo
def initialize
@x = 3
end
def fiddle &block
instance_eval &block
end
end
@arwagner
arwagner / gist:1403089
Created November 29, 2011 02:26
proc/block gotcha i'm missing?
instance_eval { 3 }
x = lambda { 3 }
instance_eval &x # wrong number of arguments, 1 for 0
fact("x"); query("x"); #=> ["x"]
fact("x"); query("y"); #=> []
fact("is_cool(fred)"); query("is_cool(fred)"); #=> ["is_cool(fred)"]
fact("is_cool(parent_of(fred),on(saturdays))"); query("is_cool(parent_of(fred),on(saturdays))"); #=> ["is_cool(parent_of(fred),on(saturdays))"]
fact("x"); query(":y"); #=> [{":y"=>"x"}]
fact("is_cool(fred)"); query("is_cool(:y)"); #=> [{":y"=>"fred"}]
fact("is_cool(parent_of(fred),on(saturdays))"); query("is_cool(parent_of(fred),:time)"); #=> [{":time"=>"on(saturdays)"}]
fact("is_cool(fred,saturdays)"); query("is_cool(fred)"); #=> []
fact("is_cool(fred,saturdays)"); query("is_cool(fred,:time)"); #=> [{":time"=>"saturdays"}]
class Application
def fact s
end
end
module RFact
def fact fact_as_string
Application.new.fact fact_as_string
end
end
class Application
def fact s
end
end
module RFact
def fact fact_as_string
Application.new.fact fact_as_string
end
end
class Foo
def not
3
end
end
Foo.new.instance_eval { not } #=> syntax error, unexpected '}'
Foo.new.instance_eval { not() } #=> true
Foo.new.send :not #=> 3
@arwagner
arwagner / gist:1323952
Created October 29, 2011 01:13
Combinatorial explosion and testing
I found http://groups.google.com/group/growing-object-oriented-software/browse_thread/thread/47695af2c6b5adda fascinating, so I decided to make it a little more concrete. It's also in ruby using rspec, sorry about that. Here's the code under test:
class EligibleForDiscountPolicy
def initialize transaction, user
@transaction = transaction
@user = user
end
def decide
return true if @user.is_gold_member?
@arwagner
arwagner / gist:1308151
Created October 24, 2011 01:06
Genetic algorithm
class ProceduralFiveDigits
def initialize
@new_population = Array.new(100) { generate_individual }
reset
end
def evolve
1000.times do
100.times do
@parent_a, @parent_b = [pick_individual, pick_individual]
/Users/andrewwagner> which ruby
ruby not found
/Users/andrewwagner> rvm list
rvm rubies
ruby-1.9.2-p290 [ i386 ]
/Users/andrewwagner> rvm use 1.9.2
Using /Users/andrewwagner/.rvm/gems/ruby-1.9.2-p290