Let's code

Download Let's code

Post on 13-Jul-2015




0 download

Embed Size (px)


<ul><li><p>Lets CodeSameer Soni care@redpanda.co.in</p></li><li><p>C.A.R. Hoare, The 1980 ACM Turing Award Lecture</p><p>There are two ways of constructing a software design: One way is to make it so simple that there </p><p>are obviously no deficiencies and the other way is to make it so complicated that there are no obvious </p><p>deficiencies. </p></li><li><p> E. W. Dijkstra</p><p>The computing scientists main challenge is not to get confused by the complexities of his own </p><p>making. </p></li><li><p>Kata is a Japanese word. are detailed choreographed patterns of </p><p>movements practised either solo or in pairs. </p><p> originally were teaching and training methods by which successful combat techniques were preserved and passed on. </p><p> Practicing kata allowed a company of persons to engage in a struggle using a systematic approach, rather than as individuals in a disorderly manner. </p><p> basic goal of kata is to preserve and transmit proven techniques and to practice self-defence. </p><p> is a term used by some Software Craftsmen, who write small snippets of code to build muscle memory and practise craft much like soldier, musician, dancer or doctor. </p><p> - wiki</p></li><li><p>Ruby Functional Programming</p><p>Statement was incorrect until we were in school and hadnt been introduced to PROGRAMMING. </p><p>Expression is quite common in Imperative style of programming. </p><p>But we dont realise the side effects and pay heavy cost.</p><p>x = x + 1 </p></li><li><p>TheoryFunctional programming treats computation as evolution of mathematical functions and avoids state and mutable data. </p><p>Promotes code with no side effects, no change in value of variables. </p><p>Discourages change of state. </p><p>Cleaner Code - Variables are not modified once defined. </p><p>Referential transparency - Expressions can be replaced by functions, as for same input always gives same output. </p><p>Advantages</p><p>Benefits of RT: 1. Parallelization 2. Memoization 3. Modularization 4. Ease of debugging</p></li><li><p>Rules - Dont update variables</p></li><li>No indexes = [1,2,3] indexes </li><li><p>No hash = {a: 1, b: 2} hash[:c] = 3 hash </p><p>Yes hash = {a: 1, b: 2} new_hash = hash.merge(c: 3) </p><p>Dont update hashes </p></li><li><p>No string = hello string.gsub!(/l/, 'z') string # hezzo </p><p>Yes string = "hello" new_string = string.gsub(/l/, 'z') # "hezzo"</p><p>Dont use bang methods which modify in place </p></li><li><p>No number = gets number = number.to_i </p><p>Here, we are not updating number but overriding the old values, which is as bad as updating. Rule is: Once defined, its value should remain same in that scope </p><p>Yes number_string = gets number = number_string.to_i</p><p>Dont reuse variables </p></li><li><p>Blocks as higher order functionsA block is an anonymous piece of code you can pass around and execute at will.</p></li><li><p>No dogs = [] ["milu", "rantanplan"].each do |name| </p><p> dogs ["MILU", "RANTANPLAN"] </p><p>Yes dogs = ["milu", "rantanplan"].map do |name| </p><p> name.upcase end # =&gt; ["MILU", "RANTANPLAN"]</p><p>init-empty + each + push = map </p></li><li><p>No dogs = [] ["milu", "rantanplan"].each do |name| </p><p> if name.size == 4 dogs [milu"] </p><p>Yes dogs = ["milu", "rantanplan"].select do |name| </p><p> name.size == 4 end # =&gt; ["milu"]</p><p>init-empty + each + conditional push = select/reject </p></li><li><p>No length = 0 ["milu", "rantanplan"].each do |dog_name| </p><p> length += dog_name.length end </p><p> length # 14 </p><p>Yes length = ["milu", "rantanplan"].inject(0) do |accumulator, dog_name| </p><p> accumulator + dog_name.length end # =&gt; 14</p><p>initialize + each + accumulate = inject </p></li><li><p>1st way: hash = {} input.each do |item| hash[item] = process(item) end hash</p><p>How to create hash from an enumerable </p><p>2nd way: Hash[input.map do |item| [item, process(item)] end] </p><p>input.inject({}) do |hash, item| hash.merge(item =&gt; process(item)) end</p></li><li><p># Way 1 </p><p>if found_dog == our_dog name = found_dog.name message = "We found our dog #{name}!" else message = "No luck" end</p><p>Everything is a expression </p><p># Way 2 </p><p>message = if (found_dog == my_dog) name = found_dog.name "We found our dog #{name}!" else "No luck" end</p></li><li><p>Exercise</p><p>"What's the sum of the first 10 natural number whose square value is divisible by 5?"</p></li><li><p>Ruby Functional wayInteger::natural.select { |x| x**2 % 5 == 0 }.take(10).inject(:+) #=&gt; 275</p></li><li><p>Ruby Imperative way</p><p>n, num_elements, sum = 1, 0, 0 while num_elements &lt; 10 if n**2 % 5 == 0 sum += n num_elements += 1 end n += 1 end sum #=&gt; 275</p></li><li><p>Source wikipedia.com code.google.com</p></li><li><p>Thanks</p></li></ul>