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

Five Languages in a Moment

Five Languages in a Moment

Slides for my talk “Five Languages in a Moment” in Conferencia Rails 2011 (Madrid, 14 July)

Sergio Gil

April 09, 2012
Tweet

More Decks by Sergio Gil

Other Decks in Programming

Transcript

  1. 1

  2. > X = 1. > X. 1 > X =

    2. ** exception error
  3. > L = [1, 2, 3, 4]. > [H|T] =

    L. > H. 1 > T. [2, 3, 4]
  4. sum([]) -> 0; sum([H|T]) -> H + sum(T). map(_, [])

    -> []; map(F, [H|T]) -> [F(H) | map(F, T)].
  5. sum([]) -> 0; sum([H|T]) -> H + sum(T). map(_, [])

    -> []; map(F, [H|T]) -> [F(H) | map(F, T)]. ...
  6. area({rectangle, Width, Height}) -> Width * Height; area({square, X}) ->

    X * X; area({circle, R}) -> 3.14159 * R. > area({square, 3}). 9
  7. class Rectangle def initialize(width, height) @width = width @height =

    height end def area @width * height end end class Square def initialize(x) @x = x end def area @x * @x end end class Circle def initialize(r) @r = r end def area 3.14159 * @r end end
  8. def area(*args) case args[0] when :rectangle args[1] * args[2] when

    :square args[1] * args[1] when :circle 3.141519 * args[1] end end
  9. pmap(F, L) -> Parent = self(), Pids = map(fun(I) ->

    spawn(fun() -> Parent ! {self(), F(I)} end) end, L), gather(Pids).
  10. pmap(F, L) -> Parent = self(), Pids = map(fun(I) ->

    spawn(fun() -> Parent ! {self(), F(I)} end) end, L), gather(Pids). gather([]) -> []; gather([Pid|T]) -> receive {Pid, Result} -> [Result|gather(T)] end.
  11. xN

  12. 2

  13. data BookInfo = Book Int String [String] myBook = Book

    987987 "About Wadus" ["Fulano", "Mengano"]
  14. data Color = Red | Yellow | Green | RGB

    Int Int Int yellow = Yellow black = RGB 0 0 0
  15. data Shape = Circle Float | Rectangle Float Float |

    Square Float area (Circle r) = r * 3.14 area (Rectangle w h) = w * h area (Square x) = x * x
  16. data Shape = Circle Float | Rectangle Float Float |

    Square Float area (Circle r) = r * 3.14 area (Rectangle w h) = w * h area (Square x) = x * x circle = Circle 5 rectangle = Rectangle 2 3 square = Square 1.5
  17. data Shape = Circle Float | Rectangle Float Float |

    Square Float area (Circle r) = r * 3.14 area (Rectangle w h) = w * h area (Square x) = x * x circle = Circle 5 rectangle = Rectangle 2 3 square = Square 1.5 -- > area circle -- 15.700001
  18. data Shape = Circle Float | Rectangle Float Float |

    Square Float area (Circle r) = r * 3.14 area (Rectangle w h) = w * h area (Square x) = x * x circle = Circle 5 rectangle = Rectangle 2 3 square = Square 1.5 -- > area circle -- 15.700001 -- > :type area -- area :: Shape -> Float
  19. square_all list = map (\n -> n^2) list > square_all

    [1, 2, 5] [1,4,25] square_all list = map (^2) list
  20. square_all list = map (\n -> n^2) list > square_all

    [1, 2, 5] [1,4,25] square_all list = map (^2) list square_all = map (^2)
  21. @articles = Article.where(:status => 'published') <% @articles.each do |article| %>

    <h2><%= article.title %></h2> <p><%= article.body %></p> <% end %>
  22. @articles = Article.where(:status => 'published') <% @articles.limit(5).each do |article| %>

    <h2><%= article.title %></h2> <p><%= article.body %></p> <% end %>
  23. iforelse cond a b = if cond then a else

    b > iforelse True (1 + 2) (3 + 4) 3
  24. iforelse cond a b = if cond then a else

    b > iforelse True (1 + 2) (3 + 4) 3 > iforelse False (1 + 2) (3 + 4) 7
  25. 3

  26. “property of some programming languages, in which the primary representation

    of programs is also a data structure in a primitive type of the language itself”
  27. (1 2 3 ("wadus" 5) (1.3 1.7)) [1, 2, 3,

    ["wadus", 5], [1.3, 1.7]]
  28. (1 2 3 ("wadus" 5) (1.3 1.7)) (+ (* 3

    5) (/ 10 2)) CODE = DATA
  29. Valid Lisp code is composed of lists whose first element

    is the name of a function and the rest are the parameters passed
  30. 4

  31. (def whole-numbers (iterate inc 1)) (first whole-numbers) ; 1 (take

    5 whole-numbers) ; (1 2 3 4 5) (last whole-numbers) ; => CATACROKER!
  32. (defn fibonacci [] (map first (iterate (fn [[a b]] [b

    (+ a b)]) [0 1]))) (take 20 (fibonacci)) ; (0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181)
  33. STM

  34. (def fulano-balance (ref 100)) (def mengano-balance (ref 80)) (dosync (ref-set

    fulano-balance (+ @fulano-balance 20)) (ref-set mengano-balance (- @mengano-balance 20)))
  35. (def fulano-balance (ref 100)) (def mengano-balance (ref 80)) (dosync (ref-set

    fulano-balance (+ @fulano-balance 20)) (ref-set mengano-balance (- @mengano-balance 20))) @fulano-balance ; 120
  36. (def fulano-balance (ref 100)) (def mengano-balance (ref 80)) (dosync (ref-set

    fulano-balance (+ @fulano-balance 20)) (ref-set mengano-balance (- @mengano-balance 20))) @fulano-balance ; 120 @mengano-balance ; 60
  37. 5

  38. <

  39. >

  40. -

  41. ME!

  42. Fun