all n n limit)) ([all n step limit] (let [current (+ n step) unmarked (disj all current)] (if (> n limit) all (recur unmarked current step limit))))) segunda-feira, 30 de janeiro de 12
all n n limit)) ([all n step limit] (let [current (+ n step) unmarked (disj all current)] (if (> n limit) all (recur unmarked current step limit))))) (defn mark-all ([all limit] (mark-all all 2 limit)) ([all current limit] (let [unmarked (mark all current limit) marked (difference all unmarked) next-val (first (filter #(> % current) unmarked))] (if (> (* current current) limit) unmarked (mark-all unmarked next-val limit))))) segunda-feira, 30 de janeiro de 12
all n n limit)) ([all n step limit] (let [current (+ n step) unmarked (disj all current)] (if (> n limit) all (recur unmarked current step limit))))) (defn mark-all ([all limit] (mark-all all 2 limit)) ([all current limit] (let [unmarked (mark all current limit) marked (difference all unmarked) next-val (first (filter #(> % current) unmarked))] (if (> (* current current) limit) unmarked (mark-all unmarked next-val limit))))) (defn primes [start end] (let [all (apply sorted-set (range 2 (+ end 1)))] (filter #(>= % start) (mark-all all end)))) LIÇÃO 1: Funções pequenas segunda-feira, 30 de janeiro de 12
def utf8(str) str.to_s.encode("UTF-8") end def strip_text(description) description.gsub(/[\n\r\t]/, '').strip end LIÇÃO 1: Funções pequenas segunda-feira, 30 de janeiro de 12
def utf8(str) str.to_s.encode("UTF-8") end def strip_text(description) description.gsub(/[\n\r\t]/, '').strip end LIÇÃO 2: Composição de funções segunda-feira, 30 de janeiro de 12
map { |a| a * 2 }. each_cons(2).with_index(&method(:indexed)) end def indexed(group, index) puts "Group #{index}: #{group}" end end t = Teste.new t.iterate_grouped "Group 0: [2, 4]" "Group 1: [4, 6]" "Group 2: [6, 8]" segunda-feira, 30 de janeiro de 12