SlideShare a Scribd company logo
1 of 22
Download to read offline
Letā€™s Code
Sameer Soni
@_sameersoni
ā€“C.A.R. Hoare, The 1980 ACM Turing Award Lecture
ā€œThere are two ways of constructing a software
design: One way is to make it so simple that there
are obviously no deļ¬ciencies and the other way is to
make it so complicated that there are no obvious
deļ¬ciencies.ā€
ā€“ E. W. Dijkstra
ā€œThe computing scientistā€™s main challenge is not to
get confused by the complexities of his own
making.ā€
Kata
ā€¢ is a Japanese word.
ā€¢ are detailed choreographed patterns of
movements practised either solo or in
pairs.
ā€¢ originally were teaching and training
methods by which successful combat
techniques were preserved and passed
on.
ā€¢ Practicing kata allowed a company of
persons to engage in a struggle using a
systematic approach, rather than as
individuals in a disorderly manner.
ā€¢ basic goal of kata is to preserve and
transmit proven techniques and to
practice self-defence.
ā€¢ 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.
- wiki
Ruby Functional Programming
Statement was incorrect until we were in school and
hadnā€™t been introduced to PROGRAMMING.
Expression is quite common in Imperative style of
programming.
But we donā€™t realise the side effects and pay heavy cost.
x = x + 1
Theory
Functional programming treats
computation as evolution of
mathematical functions and
avoids state and mutable data.
Promotes code with no side
effects, no change in value of
variables.
Discourages change of state.
Cleaner Code - Variables are not
modiļ¬ed once deļ¬ned.
Referential transparency - Expressions
can be replaced by functions, as for
same input always gives same output.
Advantages
Beneļ¬ts of RT:
1. Parallelization
2. Memoization
3. Modularization
4. Ease of debugging
Rules - Donā€™t update variables
No
ā€¢ indexes = [1,2,3]
ā€¢ indexes << 4
ā€¢ indexes # [1,2,3,4]
Yes
ā€¢ indexes = [1,2,3]
ā€¢ all_indexes = indexes + [4] #[1,2,3,4]
Donā€™t append to arrays or strings
No
ā€¢ hash = {a: 1, b: 2}
ā€¢ hash[:c] = 3
ā€¢ hash
Yes
ā€¢ hash = {a: 1, b: 2}
ā€¢ new_hash = hash.merge(c: 3)
Donā€™t update hashes
No
ā€¢ string = ā€œhelloā€
ā€¢ string.gsub!(/l/, 'z')
ā€¢ string # ā€œhezzo
Yes
ā€¢ string = "hello"
ā€¢ new_string = string.gsub(/l/, 'z') # "hezzo"
Donā€™t use bang methods which modify in place
No
ā€¢ number = gets
ā€¢ number = number.to_i
Here, we are not updating number but overriding the old values,
which is as bad as updating.
Rule is: Once deļ¬ned, its value should remain same in that scope
Yes
ā€¢ number_string = gets
ā€¢ number = number_string.to_i
Donā€™t reuse variables
Blocks as higher order functions
A block is an anonymous piece of code you can pass
around and execute at will.
No
ā€¢ dogs = []
ā€¢ ["milu", "rantanplan"].each do |name|
dogs << name.upcase
end
ā€¢ dogs # => ["MILU", "RANTANPLAN"]
Yes
ā€¢ dogs = ["milu", "rantanplan"].map do |name|
name.upcase
end # => ["MILU", "RANTANPLAN"]
init-empty + each + push = map
No
ā€¢ dogs = []
ā€¢ ["milu", "rantanplan"].each do |name|
if name.size == 4
dogs << name
end
end
ā€¢ dogs # => [ā€œmilu"]
Yes
ā€¢ dogs = ["milu", "rantanplan"].select do |name|
name.size == 4
end # => ["milu"]
init-empty + each + conditional push = select/reject
No
ā€¢ length = 0
ā€¢ ["milu", "rantanplan"].each do |dog_name|
length += dog_name.length
end
ā€¢ length # 14
Yes
ā€¢ length = ["milu", "rantanplan"].inject(0) do |accumulator, dog_name|
accumulator + dog_name.length
end # => 14
initialize + each + accumulate = inject
1st way:
hash = {}
input.each do |item|
hash[item] = process(item)
end
hash
How to create hash from an enumerable
2nd way:
Hash[input.map do |item|
[item, process(item)]
end]
input.inject({}) do |hash, item|
hash.merge(item => process(item))
end
# Way 1
if found_dog == our_dog
name = found_dog.name
message = "We found our dog #{name}!"
else
message = "No luck"
end
Everything is a expression
# Way 2
message = if (found_dog == my_dog)
name = found_dog.name
"We found our dog #{name}!"
else
"No luck"
end
Exercise
"What's the sum of the ļ¬rst 10 natural number
whose square value is divisible by 5?"
Ruby Functional way
Integer::natural.select { |x| x**2 % 5 == 0 }.take(10).inject(:+) #=> 275
Ruby Imperative way
n, num_elements, sum = 1, 0, 0
while num_elements < 10
if n**2 % 5 == 0
sum += n
num_elements += 1
end
n += 1
end
sum #=> 275
Source
ā€¢ wikipedia.com
ā€¢ code.google.com
Thanks

More Related Content

Similar to Let's Code

Introduction of c_language
Introduction of c_languageIntroduction of c_language
Introduction of c_language
SINGH PROJECTS
Ā 

Similar to Let's Code (20)

Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
Ā 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
Ā 
Oz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptxOz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptx
Ā 
Python lab basics
Python lab basicsPython lab basics
Python lab basics
Ā 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptx
Ā 
c-programming
c-programmingc-programming
c-programming
Ā 
Python programming
Python programmingPython programming
Python programming
Ā 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loops
Ā 
Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Who go Types in my Systems Programing!
Who go Types in my Systems Programing!
Ā 
Algorithm week2(technovation)
Algorithm week2(technovation)Algorithm week2(technovation)
Algorithm week2(technovation)
Ā 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
Ā 
Tuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingTuples, Dicts and Exception Handling
Tuples, Dicts and Exception Handling
Ā 
Algorithms and Data Structures
Algorithms and Data StructuresAlgorithms and Data Structures
Algorithms and Data Structures
Ā 
Concept of Algorithm.pptx
Concept of Algorithm.pptxConcept of Algorithm.pptx
Concept of Algorithm.pptx
Ā 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
Ā 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
Ā 
Introduction of c_language
Introduction of c_languageIntroduction of c_language
Introduction of c_language
Ā 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
Ā 
Brixton Library Technology Initiative Week0 Recap
Brixton Library Technology Initiative Week0 RecapBrixton Library Technology Initiative Week0 Recap
Brixton Library Technology Initiative Week0 Recap
Ā 
Unit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxUnit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptx
Ā 

Recently uploaded

Abortion Pills In Pretoria ](+27832195400*)[ šŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ šŸ„ Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ šŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ šŸ„ Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
Ā 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
Ā 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
Ā 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
Ā 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Bert Jan Schrijver
Ā 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
Ā 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
Ā 

Recently uploaded (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
Ā 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
Ā 
Abortion Pills In Pretoria ](+27832195400*)[ šŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ šŸ„ Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ šŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ šŸ„ Women's Abortion Clinic In Pre...
Ā 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
Ā 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
Ā 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Ā 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
Ā 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Ā 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
Ā 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
Ā 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Ā 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
Ā 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
Ā 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
Ā 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
Ā 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
Ā 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
Ā 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Ā 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
Ā 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
Ā 

Let's Code

  • 2. ā€“C.A.R. Hoare, The 1980 ACM Turing Award Lecture ā€œThere are two ways of constructing a software design: One way is to make it so simple that there are obviously no deļ¬ciencies and the other way is to make it so complicated that there are no obvious deļ¬ciencies.ā€
  • 3. ā€“ E. W. Dijkstra ā€œThe computing scientistā€™s main challenge is not to get confused by the complexities of his own making.ā€
  • 4. Kata ā€¢ is a Japanese word. ā€¢ are detailed choreographed patterns of movements practised either solo or in pairs. ā€¢ originally were teaching and training methods by which successful combat techniques were preserved and passed on. ā€¢ Practicing kata allowed a company of persons to engage in a struggle using a systematic approach, rather than as individuals in a disorderly manner. ā€¢ basic goal of kata is to preserve and transmit proven techniques and to practice self-defence. ā€¢ 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. - wiki
  • 5. Ruby Functional Programming Statement was incorrect until we were in school and hadnā€™t been introduced to PROGRAMMING. Expression is quite common in Imperative style of programming. But we donā€™t realise the side effects and pay heavy cost. x = x + 1
  • 6. Theory Functional programming treats computation as evolution of mathematical functions and avoids state and mutable data. Promotes code with no side effects, no change in value of variables. Discourages change of state. Cleaner Code - Variables are not modiļ¬ed once deļ¬ned. Referential transparency - Expressions can be replaced by functions, as for same input always gives same output. Advantages Beneļ¬ts of RT: 1. Parallelization 2. Memoization 3. Modularization 4. Ease of debugging
  • 7. Rules - Donā€™t update variables
  • 8. No ā€¢ indexes = [1,2,3] ā€¢ indexes << 4 ā€¢ indexes # [1,2,3,4] Yes ā€¢ indexes = [1,2,3] ā€¢ all_indexes = indexes + [4] #[1,2,3,4] Donā€™t append to arrays or strings
  • 9. No ā€¢ hash = {a: 1, b: 2} ā€¢ hash[:c] = 3 ā€¢ hash Yes ā€¢ hash = {a: 1, b: 2} ā€¢ new_hash = hash.merge(c: 3) Donā€™t update hashes
  • 10. No ā€¢ string = ā€œhelloā€ ā€¢ string.gsub!(/l/, 'z') ā€¢ string # ā€œhezzo Yes ā€¢ string = "hello" ā€¢ new_string = string.gsub(/l/, 'z') # "hezzo" Donā€™t use bang methods which modify in place
  • 11. No ā€¢ number = gets ā€¢ number = number.to_i Here, we are not updating number but overriding the old values, which is as bad as updating. Rule is: Once deļ¬ned, its value should remain same in that scope Yes ā€¢ number_string = gets ā€¢ number = number_string.to_i Donā€™t reuse variables
  • 12. Blocks as higher order functions A block is an anonymous piece of code you can pass around and execute at will.
  • 13. No ā€¢ dogs = [] ā€¢ ["milu", "rantanplan"].each do |name| dogs << name.upcase end ā€¢ dogs # => ["MILU", "RANTANPLAN"] Yes ā€¢ dogs = ["milu", "rantanplan"].map do |name| name.upcase end # => ["MILU", "RANTANPLAN"] init-empty + each + push = map
  • 14. No ā€¢ dogs = [] ā€¢ ["milu", "rantanplan"].each do |name| if name.size == 4 dogs << name end end ā€¢ dogs # => [ā€œmilu"] Yes ā€¢ dogs = ["milu", "rantanplan"].select do |name| name.size == 4 end # => ["milu"] init-empty + each + conditional push = select/reject
  • 15. No ā€¢ length = 0 ā€¢ ["milu", "rantanplan"].each do |dog_name| length += dog_name.length end ā€¢ length # 14 Yes ā€¢ length = ["milu", "rantanplan"].inject(0) do |accumulator, dog_name| accumulator + dog_name.length end # => 14 initialize + each + accumulate = inject
  • 16. 1st way: hash = {} input.each do |item| hash[item] = process(item) end hash How to create hash from an enumerable 2nd way: Hash[input.map do |item| [item, process(item)] end] input.inject({}) do |hash, item| hash.merge(item => process(item)) end
  • 17. # Way 1 if found_dog == our_dog name = found_dog.name message = "We found our dog #{name}!" else message = "No luck" end Everything is a expression # Way 2 message = if (found_dog == my_dog) name = found_dog.name "We found our dog #{name}!" else "No luck" end
  • 18. Exercise "What's the sum of the ļ¬rst 10 natural number whose square value is divisible by 5?"
  • 19. Ruby Functional way Integer::natural.select { |x| x**2 % 5 == 0 }.take(10).inject(:+) #=> 275
  • 20. Ruby Imperative way n, num_elements, sum = 1, 0, 0 while num_elements < 10 if n**2 % 5 == 0 sum += n num_elements += 1 end n += 1 end sum #=> 275