Skip to content

Instantly share code, notes, and snippets.

View Ejhfast's full-sized avatar

Ethan Fast Ejhfast

View GitHub Profile
(defn rec-map [lst]
(if (sequential? lst)
(map #(if (map? %1)
(let [mytag (str (%1 :tag))
con (%1 :content)
domatch (match mytag [":div" ":html" ":p" ":body"])
newc (if domatch con "")]
(trampoline rec-map newc))
(if (sequential? %1)
(trampoline rec-map %1)
(defn graph-words [url nums nume step]
(let [raw (sortmap (count-words url))
rng (range nums (+ nume 1) step)
totake (/ 10 (length rng))
data (map #(interesting raw %1 totake) rng)
words (map #(map (fn [x] (first x)) %1) data)
numbers (map #(map (fn [x] (second x)) %1) data)
bigwords (reduce concat words)
bignums (reduce concat numbers)
size (length (first words))
(defn run-ga
[func-map setting-map]
(let [ipop ((:init-fn func-map) (:pop-sz setting-map))]
(loop [pop ipop
num (:gen setting-map)]
(if (zero? num)
(do
(println (first (sort-by (:fit-fn func-map) > pop ))))
(let [total-left (- (:pop-sz setting-map)
(:children setting-map))]
int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
{
struct update_callback_data data;
struct rev_info rev;
init_revisions(&rev, prefix);
setup_revisions(0, NULL, &rev, NULL);
rev.prune_data = pathspec;
rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
rev.diffopt.format_callback = update_callback;
data.flags = flags;
(defn list-crossover
"A generic crossover function for simple lists. You may need to write your own."
[[s1 s2]]
(let [point (rand-int
(min (count s1)
(count s2)))]
(concat (take point s1)
(drop point s2))))
@Ejhfast
Ejhfast / meta.rb
Created December 29, 2010 15:14
Fun with Metaprogramming in Ruby
class FStore
attr_accessor :dynamic_methods
def initialize
@dynamic_methods = []
end
def mix(s1, s2, s3, &block)
name = "#{s1.to_s}_#{s2.to_s}_#{s3.to_s}".to_sym
self.send(name) {|a,b| send(s1, send(s2,a), send(s3,b))}
end
def method_missing(symbol, &block)
class FStore
attr_accessor :dynamic_methods
def initialize
@dynamic_methods = []
end
def mix(s1, s2, s3, &block)
name = "#{s1.to_s}_#{s2.to_s}_#{s3.to_s}".to_sym
self.send(name) {|a,b| send(s1, send(s2,a), send(s3,b))}
end
def method_missing(symbol, &block)
@Ejhfast
Ejhfast / mix.rb
Created December 29, 2010 16:46
mix and fstore
def mix(s1, s2, s3, &block)
name = "#{s1.to_s}_#{s2.to_s}_#{s3.to_s}".to_sym
self.send(name) {|a,b| send(s1, send(s2,a), send(s3,b))}
end
@Ejhfast
Ejhfast / fstore-mix.rb
Created December 29, 2010 16:14
mix in fstore
# Mix a few methods
e.mix(:plus, :double, :triple)
# => create: def plus_double_triple a,b ; plus (double a) (triple b) ; end
# We can call the new method
e.plus_double_triple 2, 3 #=> (plus (double 2) (triple 3)) => 13
@Ejhfast
Ejhfast / method-missing.rb
Created December 29, 2010 16:32
method missing
def method_missing(symbol, &block)
if block_given?
self.class.send :define_method, symbol, &block
else
bdy = proc {|*x| symbol}
self.class.send :define_method, symbol, bdy
end
@dynamic_methods << symbol
symbol
end