Friday, December 8, 2017

Elixir: Anonymous Functions - A Taste of Functional Programming

An anonymous function is composed of an optional parameter list, a body enclosed by fn -> and end. It returns a function definition and the potential for the function to be executed. Quite simply the anonymous function can be assigned to a variable which then can be subsequently called.

Define an anonymous function:,
greet = fn ->
  IO.puts "Hello World"
end
Of course that does nothing unless we call it! We can do that by using this dot notation -
[function name].([optional parameters])
Like this:
greet.()
Where the output is: Hello World

Anonymous functions with parameters

add = fn(num1, num2) ->
  num1 + num2
end

IO.puts add.(10, 15)
IO.puts add.(12, 15)
IO.puts add.(14, 19)

subtract = fn num1, num2 -> num1 - num2 end
IO.puts subtract.(15, 10)
Output:
25
27
33
5

I hope these examples ignite an interest for you to further learn functional programming using Elixir. Cheers!

Tuesday, December 5, 2017

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