Introduction to functional programming (In Arabic)
Functional programming is a declarative programming paradigm where programs are built around mathematical functions and immutable data transformation (1). Key aspects include using pure functions that always return the same output for the same input and avoid side effects, immutable data that does not change state, representing everything as expressions rather than statements, and treating functions as data that can be passed into other functions or returned from them (2). These characteristics allow functional programs to be deterministic, avoid bugs from mutable state, and more easily write parallel and distributed programs (3).
Hello!
I am OmarAbdelhafith from Jordan
I live in Dublin
I write blogs on http://nsomar.com
I work with computers @Zendesk
I write mainly Swift, Haskell and Elixir
On twitter @ifnottrue
On github @oarrabi
I organize Swift and Elixir meetups (in Dublin)
Intro to functionalprogramming
• What topics to discuss?
• lambda calculus
• practical category theory
• functors, monad, monoids
• promises, futures, next
• frp, reactive
• fp langs
FP is avery wide field
The definition can vary from
lenient to rigorous
14.
Lenient Definition
Graham Hutton:
Functionalprogramming is style of programming in which the
basic method of computation is the application of functions to
arguments;
• No mention of immutability
15.
In-the-middle Definition
From Haskellwiki:
In functional programming, programs are executed by
evaluating expressions, ... Functional programming typically
avoids using mutable state.
... (it) requires that functions are first-class, ...(to end of quote)
• Mathematical functions is implied
16.
Super Rigorus Definition
Well,Not really a definition. But,
Standarized ladder of functional programming (Lambda Conf)
Lambda calculus
• AlonzoChurch In 1930 invented λ-calculus
• Calculation is done by lambda abstraction (definition)
and binding (application)
• There are no assignments of variables
25.
• John McCarthycreated LISP in the 1950s
• Inspired from lambda calculus
• LISP is the first FP lang
• Just a bit after Fortran
• Introduced lots of concepts (macros, tree ds)
• Still alive today (Common Lisp, Dialects, etc..)
27.
• Created byan International comittee in 1987
• Pure functional
• lazy
• in 90s they implemented ideas from Category Theory
• functors, monads, monoids
Programming paradigms
• Imperative(C, C++, Java, Go):
• state and control (if, for, while)
• How to do
• Declarative (Haskell, SQL)
• logic but no control
• What to do
Function composition
• multiplyBy2:: Int -> Int
• convertToString :: Int -> String
• x = multiplyBy2(2)
• convertToString(x)
• convertToString(multiplyBy2(2))
• (convertToString . multiplyBy2)(2)
82.
Example in FPlanguage
Let's delay it after high order functions
83.
Decompose
• Declarative programmingparadigm ✅
• Mathematical functions ✅
• Immutable data
• Everything is an expression
• Functions are data
84.
Immutable data
• Datadoes not change
• it gets copied when passed
• In FP, all data is immutable
Decompose
• Declarative programmingparadigm ✅
• Mathematical functions ✅
• Immutable data ✅
• Everything is an expression ✅
• Functions are data ✅
107.
Recap
• Declarative: Whatto do and not how to do it
• Mathematical functions: Pure, Total, Composable
• Immutable data: Dont change the state
• Everything is an expression: and they are composable
• Functions are data: Can be passed and returned from
functions
108.
Why are weseeing a rise in FP
• Multi core and paralellism
• Determinism, testability
• They align well with cloud computing and microservices