2. Функционални Закачки

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    2. Функционални Закачки - Presentation Transcript

    1. speaker { [“Stefan”, “Kanev”].join(“ “) } speaker { “Nikolay” + “ “ + “Bachiyski” } on_date { @lecture_date || “13‐10‐2008” } 2. Функционални закачки
    2. speaker { [“Stefan”, “Kanev”].join(“ “) } speaker { “Nikolay” + “ “ + “Bachiyski” } on_date { @lecture_date || “13‐10‐2008” } 2. Функционални закачки
    3. Хиперкуб
    4. 3. 1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679 8214808651 3282306647 0938446095 5058223172 5359408128 4811174502 8410270193 8521105559 6446229489 5493038196 4428810975 6659334461 2847564823 3786783165 2712019091 4564856692 3460348610 4543266482 1339360726 0249141273 7245870066 0631558817 4881520920 9628292540 9171536436 7892590360 0113305305 4882046652 1384146951 9415116094 3305727036 5759591953 0921861173 8193261179 3105118548 0744623799 6274956735 1885752724 8912279381 8301194912 9833673362 4406566430 8602139494 6395224737 1907021798 6094370277 0539217176 2931767523 8467481846 7669405132 0005681271 4526356082 7785771342 7577896091 7363717872 1468440901 2249534301 4654958537 1050792279 6892589235 4201995611 2129021960 8640344181 5981362977 4771309960 5187072113 4999999837 2978049951 0597317328 1609631859 5024459455 3469083026 4252230825 3344685035 2619311881
    5. Domain Specific Language
    6. mind.refresh
    7. 3.14 1_729 \"Who is John Galt?\" [18, :f, \"Sofia\"] {:jazz => [\"Thelonious Monk\", \"Bill Evans\"], :baroque => [\"J.S. Bach\", \"Henry Purcell\"], :classical => [\"L.V. Beethoven\", \"W.A. Mozart\"] }
    8. if hungry? eat else sleep until hungry? end work until job.done? write_code(:ruby) while me.unhappy? eat(food) if hungry? write_code unless tired?
    9. функции
    10. функционално програмиране
    11. приема неща връща неща
    12. приема неща връща неща Обекти
    13. order, change = use_machine(number, money)
    14. Неща order, change = use_machine(number, money) coffee, change = use_machine(42, 1.50)
    15. cup, cash = order(\"Cappuccino\", 3) do |pref| pref.make_short pref.add_sugar 2 pref.add_extra :cream end
    16. приема неща връща неща
    17. приема неща приема инструкции връща неща
    18. приема неща приема инструкции връща неща Код
    19. cup, cash = order(\"Cappuccino\", 3) do |pref| pref.make_short pref.add_sugar 2 pref.add_extra :cream end
    20. >> numbers = [2, 3, 5, 7] >> numbers.map { |x| x ** 2 } [4, 9, 25, 49] >> numbers.select { |x| x > 4 } [5, 7]
    21. извикване do |аргументи| код end извикване { |аргументи| код }
    22. File.open(\"spendings\").each_line do |line| next unless well_formed?(line) date, category, amount = line.split category_sum[category] += amount.to_i end points.all? { |x,y| saved.include? [y,x] }
    23. Щипки Улей points.all? { |x,y| saved.include? [y,x] } Итератор Блок / Корутина
    24. Пич, извикай корутината
    25. def twice yield yield end twice { puts “Ruby rocks!” }
    26. accumulate
    27. def accum(start, items) result, items = start, items.dup yield(result, items.pop) until items.empty? result end numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] puts accum(0, numbers) { |a, b| a + b } puts accum(1, numbers) { |a, b| a * b }
    28. def with_primes(max) 2.upto(max) do |n| yield n unless (2...n).any? { |c| n % c == 0 } end end with_primes(10) { |x| puts x }
    29. 2.upto(10) do |n|
    30. (2..10) (2…10)
    31. any? all?
    32. students.any? { |s| s.cheated? } numbers.all? { |x| x % 2 == 0 }
    33. def with_primes(max) 2.upto(max) do |n| yield n unless (2...n).any? { |c| n % c == 0 } end end with_primes(10) { |x| puts x }
    34. return
    35. def magic_roots(numbers) numbers.map do |number| return number ** 0.5 if number < 0 (‐number) ** 0.5 end end magic_roots([100, ‐25, ‐9, 16])
    36. def return end
    37. next break
    38. n, found = 1, 0 while true n += 1 next unless (2...n).any? { |a| n % a == 0 } found += 1 puts \"#{n} squared is #{n * n}\" break if found >= 50 end
    39. lucky = [] users.each do |user| next unless user.submitted_homework? lucky << user break if lucky.size >= 5 end
    40. next(arg) break(arg)
    41. item = items.first_item found = until item.nil? break item if item.nice? and item.shiny? item = item.successor end
    42. def magic_roots(numbers) numbers.map do |number| next number ** 0.5 if number < 0 (‐number) ** 0.5 end end magic_roots([100, ‐25, ‐9, 16])
    43. Щипки Улей points.all? { |x,y| saved.include? [y,x] } Итератор Блок / Корутина
    44. next излез от блока в итератора break излез от итератора
    45. lucky = [] users.each do |user| next unless user.submitted_homework? lucky << user break if lucky.size >= 5 end
    46. Enumerable Array Hash
    47. collect select inject
    48. 001:0> numbers = [1, 2, 3, 4, 5, 6, 7] => [1, 2, 3, 4, 5, 6, 7] 002:0> numbers.collect { |n| n ** 2 } => [1, 4, 9, 16, 25, 36, 49] 003:0> numbers.select { |n| n % 2 == 0 } => [2, 4, 6] 004:0> numbers.inject { |a, b| a + b } => 28
    49. emails = [] users.each do |user| emails << user.email if user.active? end return emails users.select { |u| u.active? }.map { |u| u.email }
    50. alias :collect, :map alias :select, :find_all
    51. 001:0> people = [\"Coltrane\", \"Monk\", \"Davis\", \"Evans\"] 002:0> people.all? { |x| x =~ /^[A‐Z][a‐z]+$/ } => true 003:0> people.any? { |name| name.include? 'trane' } => true 004:0> people.sort => [\"Coltrane\", \"Davis\", \"Evans\", \"Monk\"] 005:0> people.sort { |a, b| a.size <=> b.size } => [\"Monk\", \"Evans\", \"Davis\", \"Coltrane\"] 006:0> people.sort_by { |name| name.size } => [\"Monk\", \"Evans\", \"Davis\", \"Coltrane\"] 007:0> people.each_with_index { |n, i| puts \"#{i}. #{n}\" } 0. Coltrane 1. Monk 2. Davis 3. Evans 008:0> people.detect { |name| name.include? \"s\" } => \"Davis“ 009:0> people.zip([\"John\", \"Thelonious\", \"Miles\", \"Bill\"]) => [[\"Coltrane\", \"John\"], [\"Monk\", \"Thelonious\"], [\"Davis\",  \"Miles\"], [\"Evans\", \"Bill\"]]
    52. collect! select!
    53. :symbol
    54. symbol != char
    55. Symbol.similar_to String
    56. { :host => \"fmi.rails‐bg.net\", :port => 3306, :username => \"website\", :password => \"gould\", :database => \"website\", }
    57. wazup?!!!?!??1:!!?? def wazup?(a) puts \"#{a} dat!\" end
    58. def greet(name = \"Unknown\") puts \"Greetings, #{name}\" end greet greet \"Ruby\"
    59. connect :db, :host => \"172.0.0.1\", :port => 3306 connect(:db, {:host => \"172.0.0.1\", :port => 3306}) def connect(what, options = {})
    60. form_for(url, html_options) form_for {:action => \"show\", :id => 1}, :class => \"horizontal\" form_for do :action => \"show\", :id => 1 end, :class => \"horizontal\"
    61. 001:0> hash, string = {\"John\" => \"Coltrane\",  \"Eddie\" => \"Vedder\"}, \"lar\\nodi\" 002:0> puts hash.to_s JohnColtraneEddieVedder 003:0> puts hash.inspect {\"John\"=>\"Coltrane\", \"Eddie\"=>\"Vedder\"} 004:0> puts string.to_s lar odi 005:0> puts string.inspect \"larodi\"
    62. to_s inspect
    63. p foo \"It is: %p\" % foo
    64. lambda
    65. square = lambda { |x| x ** 2 } puts square(7) # Не работи puts square.call(7) puts square[7]
    66. seek = lambda do |needle, haystack| haystack.each do |item| return true if item.equal?(needle) end false end
    67. square = ‐> (x) { x ** 2 } 1.9
    68. Proc.new
    69. root = Proc.new { |x| x ** 0.5 } positivep = Proc.new { |x| x > 0 } puts root[x] if positivep[x]
    70. def my_map(list, operation) result = [] list.each do |item| result << operation[item] end result end p my_map([2, 3, 5, 7], Proc.new { |x| x ** 2})
    71. def my_map(list, operation) result = [] list.each do |item| result << yield(item) end result end p my_map([2, 3, 5, 7]) { |x| x ** 2}
    72. def my_map(list, operation) result = [] list.each do |item| result << yield(item) end result end operation = Proc.new { |x| x ** 2 } p my_map([2, 3, 5, 7]) ... ???
    73. proc → block
    74. def my_map(list, operation) result = [] list.each do |item| result << yield(item) end result end operation = Proc.new { |x| x ** 2 } p my_map([2, 3, 5, 7], &operation)
    75. block → proc
    76. def my_map(list, &operation) result = [] list.each do |item| result << operation[item] end result end p my_map([2, 3, 5, 7]) { |x| x ** 2 }
    77. block_given?
    78. def can_has_block? return block_given? end puts can_has_block? # false puts can_has_block? { “plz } # true
    79. def my_proc(&block) block end square = my_proc { |x| x ** 2 } puts square.call(7) puts square[42]
    80. (‐>_{_%_})[\"(‐>_{_%%_})[%p]\"]
    81. def raise_to_power(numbers, power) numbers.map { |x| x ** power } end p raise_to_power([2, 3, 5, 7], 3)
    82. def raise_to_power(numbers, power) numbers.map { |x| x ** power } end p raise_to_power([2, 3, 5, 7], 3)
    83. def make_stamp(owner) time = Time.now lambda do |thing| \"#{time}: #{thing} is owned by #{owner}\" end end sony = make_stamp(\"Sony\") puts sony[\"Playstation\"] sleep 1 microsoft = make_stamp(\"Microsoft\") puts microsoft[\"X‐box\"] puts sony[\"Playstation\"]
    84. Mon Oct 13 16:37:22 +0300 2008: Playstation is owned by Sony Mon Oct 13 16:37:23 +0300 2008: X‐box is  owned by Microsoft Mon Oct 13 16:37:22 +0300 2008: Playstation is owned by Sony
    85. def cuckoo n = 0 tick = lambda { n += 1 } tack = lambda { puts n } return tick, tack end one, two = cuckoo two[] # 0 one[] two[] # 1 one[] three, four = cuckoo four[] # 0 two[]  # 2
    86. can_haz_keushunz?

    + Stefan KanevStefan Kanev, 2 years ago

    custom

    783 views, 0 favs, 0 embeds more stats

    Втора лекция от курса във ФМ more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 783
      • 783 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 149
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories