Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Programação funcional para fazer Ruby melhor

Programação funcional para fazer Ruby melhor

Nessa palestra falei sobre algumas ideias de programação funcional para fazer ruby melhor. Apresentado no 21º encontro do GURU-SP, em Janeiro de 2012.

Avatar for Vinicius Baggio Fuentes

Vinicius Baggio Fuentes

January 30, 2012
Tweet

More Decks by Vinicius Baggio Fuentes

Other Decks in Programming

Transcript

  1. Clojure Erlang Scala Haskell Scheme O-Caml Prolog Lisp . .

    . segunda-feira, 30 de janeiro de 12
  2. (defn fib [n] (if (<= n 1) 1 (+ (fib

    (- n 1)) (fib (- n 2))))) * Implementação bem simplista segunda-feira, 30 de janeiro de 12
  3. (defn fib [n] (if (<= n 1) 1 (+ (fib

    (- n 1)) (fib (- n 2))))) * Implementação bem simplista LISP segunda-feira, 30 de janeiro de 12
  4. (println (reduce + (filter #(= (mod % 2) 0) [1

    2 3 4 5 6 7 8 9 10 11 12]))) segunda-feira, 30 de janeiro de 12
  5. LIÇÃO 1: Funções pequenas (defn mark ([all n limit] (mark

    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
  6. LIÇÃO 1: Funções pequenas (defn mark ([all n limit] (mark

    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
  7. (use '[clojure.set :only (difference)]) (defn mark ([all n limit] (mark

    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
  8. (use '[clojure.set :only (difference)]) (defn mark ([all n limit] (mark

    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)))) (let [times (Integer/parseInt (read-line))] (dotimes [_ times] (let [line (map #(Integer/parseInt %) (re-seq #"\w+" (read-line)))] (println (reduce #(format "%s\n%d" %1 %2) (apply primes line))) (printf "\n")))) LIÇÃO 1: Funções pequenas segunda-feira, 30 de janeiro de 12
  9. def description_text(body) strip_text(utf8(information_layer(body).inner_text)) end private def information_layer(text) @body.css('#Layer1 font[size="2"]').first end

    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
  10. LIÇÃO 2: Composição de funções a = [1, 2, 3,

    4, 5, 6, 7, 8, 9, 10, 11, 12] a.select {|a| a % 2 == 0}. reduce(0) { |a,sum| sum += a } segunda-feira, 30 de janeiro de 12
  11. [1,2,3,4]. map { |a| a * 2 }. each_cons(2). with_index

    {|v,i| puts "Group #{i}: #{v}" } "Group 0: [2, 4]" "Group 1: [4, 6]" "Group 2: [6, 8]" LIÇÃO 2: Composição de funções segunda-feira, 30 de janeiro de 12
  12. def description_text(body) strip_text(utf8(information_layer(body).inner_text)) end private def information_layer(text) @body.css('#Layer1 font[size="2"]').first end

    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
  13. LIÇÃO 2: Composição de funções class Teste def iterate_grouped [1,2,3,4].

    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
  14. LIÇÃO 3: Imutabilidade data = { first_name: ' JOHN', last_name:

    ' SMITH', } Person.new(data) segunda-feira, 30 de janeiro de 12
  15. LIÇÃO 3: Imutabilidade class Person def initialize(data) @first_name = data[:first_name]

    @last_name = data[:last_name] @first_name.strip! @last_name.strip! end end segunda-feira, 30 de janeiro de 12