Skip to main content

A Taste of Functional Programming

Objectives

  • Ignite an interest in those who have never explored functional programming before
  • Exposure to functional concepts
  • Use functional parts of your existing language of choice you've never used before 
  • Lead you to pursue a functional language more in-depth

Paradigm Evolution

  • Mathematics
  • Computer Science
  • Spawn of languages
    • functional
    • procedural
    • imperative
    • declarative
    • object-oriented programming

OOP Limitations

"We’re going to be living in a multicore, distributed, concurrent — all the buzz words — world. The conventional models we’ve been doing, the OO stuff… is not going to survive in that kind of environment." - Dave Thomas
"OOP promised a cure for the scourge of software complexity. …its weaknesses have become increasingly apparent. Spreading state all over the place leads to concurrency issues and unpredictable side effects." - Dave Thomas

Thinking in Functions

  • Functions
    • Easy to reason about
    • Reliable
    • Pure
      • Don't modify variables outside of scope
      • No side effects
      • Deterministic (reproducible results)
  • Data transformation
    • ie. Unix pipes - cat foo.log | grep bar | wc -l
  • No side-effects
    • Side effects are:
      • modifying state
      • has observable interaction with external functions
  • Immutability
    • Immutable data is known data
    • Data that is created is not changed
    • Copy and alter
      • compilers can perform optimizations because of this
      • garbage collectors are smart about this
  • Higher-order Functions
    • Functions can receive functions as arguments and return functions
  • Where is my for loop?
    • recursion
    • map, reduce, filter, reject, take, etc.

Some (impure and pure) functional languages

  • LISP
  • Scheme
  • Clojure
  • Erlang
  • Scala
  • Ocaml
  • Haskell
  • F#
  • Elm
  • Elixir

Elixir

"Elixir is a dynamic, functional language designed for building scalable and
maintainable applications. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.
" - http://elixir-lang.org

Here is an out line of topics to come:

  • Anonymous functions
  • Pattern matching
  • Multi-bodied functions
  • Higher order functions
  • Side effects and state
  • Composition
  • Enumerables
  • Partial function applications
  • Recursion
  • Concurrency
  • Transitioning from OOP to functional

Comments

Popular posts from this blog

Setting up Sinatra and DataMapper on Windows

I use a MacBook Pro for work and pleasure on a day-to-day basis. Recently, I was asked to teach web students at a local high school. These students know html/graphics/flash/etc. The advanced students were ready for some server-side programming and database integration. I wanted the students to be able to get up and going quickly (for motivation reasons) and to create useful apps (using a database). I felt Sinatra to be a great fit for this. I created a Sinatra app for my uncle and his business. It was a joy to work with it and I was able to deploy quickly using Heroku . My experience of using Sinatra on my Mac was straightforward. Like most things using Ruby and Mac: it just worked. However, I found out the students at the high school use MS Windows. Fortunately, I have a Windows XP virtual machine running in VMWare so I could prepare that way. I used to teach computer science and web development at Spokane Community College and am aware of teaching Ruby in a Windows lab environm

PHP and Laravel Development: A 17-part video series

 Enjoy!

Elixir: Pattern Matching - A Taste of Functional Programming

=  is a match operator After an initial assignment it then becomes a match assertion. num = 1 To check the match assertion you can try this and it will be valid 1 = num Furthermore, these are also valid: [1] = [1] [num] = [num] Destructuring [score1, score2, score3] = [89, 93, 87] IO.puts score1  # 89 IO.puts score2  # 93 IO.puts score3  # 87 {:ok, value} = {:ok, 1000} IO.puts value # 1000 {:error, message} = {:error, "Uh oh"} IO.puts message # "Uh oh" Error when matching: {:foo, value} = {:bar, "nope"} ** (MatchError) no match of right hand side value: {:bar, "nope"} Matching function arguments Various function definitions and subsequent calls: def sum_two_nums(num1, num2) do num1 + num2 end sum_two_nums(2,5) def sum_two_nums(%{num1: num1, num2: num2}) do num1 + num2 end sum_two_nums(%{num1: 2, num2: 5}) def sum_two_nums([num1, num2]) do num1 + num2 end sum_two_nums([2,5]) There's so much more you