SlideShare uma empresa Scribd logo
1 de 25
Intro to Functional Programming ,[object Object],[object Object]
What is Functional Programming? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What’s the Point? Principle of Least Surprise Principle of Least Surprise ,[object Object],[object Object],[object Object],[object Object],Explicitly mention stateless.  Might also talk about theoretical statelessness of the web, but also why it isn’t truly stateless.
No assignments!  How do you loop? ,[object Object],[object Object],[object Object],[object Object],Need to mention that although you _can_ do recursive algorithms and modify state, that is usually the worst of both worlds.
Recursion Basics ,[object Object],[object Object],[object Object],[object Object]
Recursion Example: The Factorial Function The Factorial Function ,[object Object],[object Object],[object Object],def factorial(number)  if number == 1  #Base Case -- compute answer  return 1  else  #Get us closer to the base case  return number * factorial(number - 1)  endend Note that we didn’t do ANY assignment in the whole program -- we just passed parameters.  Function calls create new variables, so if we think we want to do assignment, in functional programming you usually want to call a function or create a different variable.
Selection Sort Example ,[object Object],[object Object],[object Object],[object Object]
Selection Sort Example def select_sort(values)  select_sort_loop([], values)enddef select_sort_loop(sorted_values, unsorted_values)  if unsorted_values.empty? #BASE CASE!  return sorted_values  else #FIND SMALLEST VALUE AND ITS INDEX  smallest_value_index = find_smallest_value_index(unsorted_values)  smallest_value = unsorted_values[smallest_value_index] #CREATE NEW SORTED AND UNSORTED ARRAYS  new_sorted_values = add_element(sorted_values, smallest_value)  new_unsorted_values = remove_element_index(unsorted_values, smallest_value_index) #LOOP WITH NEW VALUES  return select_sort_loop(new_sorted_values, new_unsorted_values)  endend select_sort([1,5,23,8,3,5,6,8,34,65,2,5,3])
Selection Sort:  Utility Functions Utility Functions def add_element(ary, val)  ary.dup.push(val)enddef remove_element_index(ary, idx)  idx == 0 ? ary[1..-1] :  (idx == (ary.size - 1)) ? ary[1..-2] :  ary.slice(0, idx - 1).concat(ary.slice(idx + 1, ary.size - 1))enddef find_smallest_value_index(ary)  smallest_idx = 0  ary.each_index do |idx|  if ary[idx] < ary[smallest_idx]  smallest_idx = idx  smallest_val = ary[idx]  end  end  return smallest_idxend
Basic Recursive Loop ,[object Object],[object Object],In this basic example, it looks like the standard loop is better.  But as the loop gets more complicated, knowing exactly what defines your variables makes all the difference between an understandable and a totally incoherent loop.
Basic Recursive Loop ,[object Object],[object Object],Loop Definition with Parameter Declaration Note that the recursive loop explicitly names its inputs, while the standard loop does not, meaning that to truly know what contributes to the standard loops functionality requires full inspection of the whole loop. Loop Definition
Basic Recursive Loop ,[object Object],[object Object],Loop Initialization Loop Initialization
Basic Recursive Loop ,[object Object],[object Object],Loop Invocation An explicit invocation step is not needed for standard loops, as the while statement invokes it implicitly.
Basic Recursive Loop ,[object Object],[object Object],Loop Condition Loop Condition
Basic Recursive Loop ,[object Object],[object Object],Loop Body Loop Body
Basic Recursive Loop ,[object Object],[object Object],Modification of Loop Variables Modification of Loop Variables Note that in the standard loop assignments can occur anywhere, while in recursive loops they only occur by passing new parameters as part of the loop iteration process.  Which do you think leads to easier bug detection in large loops? Recursive loops can create new definitions for new variables within the loop, but cannot modify already-assigned variables.
Basic Recursive Loop ,[object Object],[object Object],Control Variable Modification Control Variable Modification Note that since this is done as a function parameter, no assignment needs to be made.  The new value is placed into a new variable (with the same name) in the next function call.
Basic Recursive Loop ,[object Object],[object Object],Loop Iteration Loop Iteration Not e that since the loop is represented as a function, to get back to the beginning of the loop, all we need to do is a function call.  But remember that unless the function call is the last thing you do, the function will return back to the loop!
Basic Recursive Loop ,[object Object],[object Object],Loop Exit Note that the loop exit is explicit in the standard loop but implicit in the recursive loop.
Packaged Looping Constructs ,[object Object],[object Object],[object Object],[object Object],Should I give an example somewhere?
Packaged Looping Constructs ,[object Object],[object Object],[object Object],Include upto? Somewhere I need to talk about fold and unfold, but that is probably for a different talk.
Functional Looping Advantages ,[object Object],[object Object],[object Object],[object Object],[object Object],Note that these are the same ones we talked about for FP in general.  Also, in general this makes the programs easier to reason about.
General Functional Programming Issues ,[object Object],[object Object],[object Object],[object Object],[object Object],Mention Monads briefly Do I need to talk about stateful/stateless somewhere?
Practical Tips ,[object Object],[object Object],[object Object],[object Object],[object Object],On state modification - might mention how ruby uses ! to mark non-functional versions of traditionally functional functions. When a task requires both computation and state modification, it is often best to separate them into two functions - one to perform the computation and one to actually perform the state change (usually with the state-change function calling the computational function, but not always).
References ,[object Object],[object Object],[object Object],[object Object]

Mais conteúdo relacionado

Mais procurados

Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
WebF
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
Mario Fusco
 

Mais procurados (20)

Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
 
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Lazy java
Lazy javaLazy java
Lazy java
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
 
Functional programming in JavaScript
Functional programming in JavaScriptFunctional programming in JavaScript
Functional programming in JavaScript
 
Python functions
Python functionsPython functions
Python functions
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: Notes
 
An Introduction to Functional Programming - DeveloperUG - 20140311
An Introduction to Functional Programming - DeveloperUG - 20140311An Introduction to Functional Programming - DeveloperUG - 20140311
An Introduction to Functional Programming - DeveloperUG - 20140311
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 

Destaque

Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Andreas Pauley
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
pramode_ce
 
From Imperative to Functional Programming (for Absolute Beginners)
From Imperative to Functional Programming (for Absolute Beginners)From Imperative to Functional Programming (for Absolute Beginners)
From Imperative to Functional Programming (for Absolute Beginners)
Alex Bunardzic
 
Functional programming
Functional programmingFunctional programming
Functional programming
edusmildo
 
Describe professional programing languages and talks
Describe professional programing languages and talks Describe professional programing languages and talks
Describe professional programing languages and talks
Ed Bray
 
Csc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigmCsc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigm
IIUM
 

Destaque (20)

Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Functional programming for optimization problems in Big Data
Functional programming for optimization problems in Big DataFunctional programming for optimization problems in Big Data
Functional programming for optimization problems in Big Data
 
Functional Programming Principles & Patterns
Functional Programming Principles & PatternsFunctional Programming Principles & Patterns
Functional Programming Principles & Patterns
 
那些 Functional Programming 教我的事
那些 Functional Programming 教我的事那些 Functional Programming 教我的事
那些 Functional Programming 教我的事
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
From Imperative to Functional Programming (for Absolute Beginners)
From Imperative to Functional Programming (for Absolute Beginners)From Imperative to Functional Programming (for Absolute Beginners)
From Imperative to Functional Programming (for Absolute Beginners)
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculusComputability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Describe professional programing languages and talks
Describe professional programing languages and talks Describe professional programing languages and talks
Describe professional programing languages and talks
 
Introduction of Functional Programming
Introduction of Functional ProgrammingIntroduction of Functional Programming
Introduction of Functional Programming
 
Uses of Cupcakes For Any Occasion in Hyderabad!
 Uses of Cupcakes For Any Occasion in Hyderabad! Uses of Cupcakes For Any Occasion in Hyderabad!
Uses of Cupcakes For Any Occasion in Hyderabad!
 
Csc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigmCsc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigm
 
Core FP Concepts
Core FP ConceptsCore FP Concepts
Core FP Concepts
 
Programming Languages
Programming LanguagesProgramming Languages
Programming Languages
 
Oop project briefing sem 1 2015 2016
Oop project briefing  sem 1 2015 2016Oop project briefing  sem 1 2015 2016
Oop project briefing sem 1 2015 2016
 
Generations Of Programming Languages
Generations Of Programming LanguagesGenerations Of Programming Languages
Generations Of Programming Languages
 

Semelhante a Introduction To Functional Programming

Notes5
Notes5Notes5
Notes5
hccit
 
Notes2
Notes2Notes2
Notes2
hccit
 

Semelhante a Introduction To Functional Programming (20)

Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
 
Loops_in_Rv1.2b
Loops_in_Rv1.2bLoops_in_Rv1.2b
Loops_in_Rv1.2b
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Pythonlearn-04-Functions (1).pptx
Pythonlearn-04-Functions (1).pptxPythonlearn-04-Functions (1).pptx
Pythonlearn-04-Functions (1).pptx
 
Python Learn Function with example programs
Python Learn Function with example programsPython Learn Function with example programs
Python Learn Function with example programs
 
Notes5
Notes5Notes5
Notes5
 
The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
Python Session - 4
Python Session - 4Python Session - 4
Python Session - 4
 
Notes2
Notes2Notes2
Notes2
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
The Ring programming language version 1.8 book - Part 9 of 202
The Ring programming language version 1.8 book - Part 9 of 202The Ring programming language version 1.8 book - Part 9 of 202
The Ring programming language version 1.8 book - Part 9 of 202
 
Functions-.pdf
Functions-.pdfFunctions-.pdf
Functions-.pdf
 
DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

Introduction To Functional Programming

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. Selection Sort Example def select_sort(values) select_sort_loop([], values)enddef select_sort_loop(sorted_values, unsorted_values) if unsorted_values.empty? #BASE CASE! return sorted_values else #FIND SMALLEST VALUE AND ITS INDEX smallest_value_index = find_smallest_value_index(unsorted_values) smallest_value = unsorted_values[smallest_value_index] #CREATE NEW SORTED AND UNSORTED ARRAYS new_sorted_values = add_element(sorted_values, smallest_value) new_unsorted_values = remove_element_index(unsorted_values, smallest_value_index) #LOOP WITH NEW VALUES return select_sort_loop(new_sorted_values, new_unsorted_values) endend select_sort([1,5,23,8,3,5,6,8,34,65,2,5,3])
  • 9. Selection Sort: Utility Functions Utility Functions def add_element(ary, val) ary.dup.push(val)enddef remove_element_index(ary, idx) idx == 0 ? ary[1..-1] : (idx == (ary.size - 1)) ? ary[1..-2] : ary.slice(0, idx - 1).concat(ary.slice(idx + 1, ary.size - 1))enddef find_smallest_value_index(ary) smallest_idx = 0 ary.each_index do |idx| if ary[idx] < ary[smallest_idx] smallest_idx = idx smallest_val = ary[idx] end end return smallest_idxend
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.