2. Agenda
â˘Functional programming (what and why)
â˘The basic concepts of functional programming:
⢠Immutability
⢠Memoization
⢠Recursion, Tail recursion
⢠Higher-order functions
⢠Currying and Partial application
⢠Pattern matching
2 | Functional Programming | 1 July 2013
3. Functional programming (what and why)
â˘What:
⢠no strict definition as opposed to OOP
⢠programming without mutable
variables, assignments, conditions, loops and other imperative
control structures
⢠functions should have no side-effects
⢠âFunctions as primary building blocksâ (first-class functions)
â˘Why:
⢠simpler reasoning principles (data abstraction, concurrency)
⢠better modularity
⢠much easier to exploit parallelism for multicore and cloud
computing
3 | Functional Programming | 1 July 2013
4. Immutability
â˘âNo notion of mutation and variablesâ
⢠variable as memory cell
⢠rather variable bindings
â˘Consequences:
⢠different programming style with collections (Cons Nil lists)
⢠usually more complicated collection implementation
⢠knowledge of implementation details of the underlying collection is
essential
⢠sometimes even necessary to refactor the code/algorithm when
switching to different collection implementation
â˘Datomic
⢠immutable internal database model
4 | Functional Programming | 1 July 2013
5. Memoization
â˘âFunction scope cachingâ
â˘Purely mathematical definition of a function
â˘Implementation details:
⢠hashmap-like data structure (Scala)
⢠not changing the value, but rather a reference (Racket, Clojure)
â˘Consequences:
⢠can lead to exponentially faster programs for recursive functions
⢠memory tradeoff
5 | Functional Programming | 1 July 2013
6. Recursion, tail recursion
â˘âWhen a function calls even indirectly itselfâ
â˘Call stacks and stack frames
â˘
java.lang.StackOverflowError
â˘âIf a function calls itself as its last action, the functionâs stack frame
can be reusedâ
â˘Consequences:
⢠JVM does not directly support tail-call optimizations
⢠Scala & @tailrec annotation, Clojure & recur form
⢠algorithms to convert recursive calls to tail recursive calls (e.g.
accumulation)
â˘Scala example
6 | Functional Programming | 1 July 2013
7. Higher-order functions
â˘âPassing or returning code/function as argumentâ
â˘Functions as first-class values
â˘OOP comparison:
⢠pretty much every OOP developer is doing âhigher-orderâ
programming
⢠design patterns: Strategy, Observer, ...
⢠practical reasons because of JVM design
â˘Scala example
7 | Functional Programming | 1 July 2013
8. Currying and Partial application
â˘âThe act of transforming a function of several arguments into a chain
of functions of one argument that will yield the same result when called
in sequence with the same argumentsâ
â˘f(x,y,z) = g(x)(y)(z)
â˘Consequences:
⢠functions because of currying can be partially applied
⢠language runtimes can be easier (Haskell)
â˘Scala example
8 | Functional Programming | 1 July 2013
9. Pattern matching
â˘âDecomposing data structures and ADTsâ
⢠Abstract Data Types (tuples)
⢠data structures (Cons Nil lists)
â˘Nothing to do with switch statement
⢠constant-time runtime lookup over constants
â˘Consequences:
⢠no missing cases
⢠no exceptions for wrong variants
â˘SML example
9 | Functional Programming | 1 July 2013
10. Wrap up
â˘Functional programming is not only, but also about:
⢠Immutability
⢠Memoization
⢠Recursion, Tail recursion
⢠Higher-order functions
⢠Currying and Partial application
⢠Pattern matching
10 | Functional Programming | 1 July 2013
11. Functional programming is much more stuffâŚ
â˘Influential people:
⢠Alonso Church
⢠Haskell Curry
⢠John McCarthy
â˘More advanced concepts:
⢠composition, functors, monads, ...
â˘Tons of languages:
⢠Clojure, F#, Lisp, OCaml, Python, Racket, Scala, Scheme, SML
11 | Functional Programming | 1 July 2013
12. Thank you for attention
Functional programming rocks
13. Disclaimer
CONFLICTS OF INTEREST BARCLAYS IS A FULL SERVICE INVESTMENT BANK. In the normal course of offering investment banking products and services to clients.
Barclays may act in several capacities (including issuer, market maker, underwriter, distributor, index sponsor, swap counterparty and calculation agent) simultaneously with
respect to a product, giving rise to potential conflicts of interest which may impact the performance of a product.
NOT RESEARCH This document is from a Barclays Trading and/or Distribution desk and is not a product of the Barclays Research department. Any views expressed may
differ from those of Barclays Research.
BARCLAYS POSITIONS Barclays, its affiliates and associated personnel may at any time acquire, hold or dispose of long or short positions (including hedging and trading
positions) which may impact the performance of a product.
FOR INFORMATION ONLY
NOT BINDING.
THIS DOCUMENT IS PROVIDED FOR INFORMATION PURPOSES ONLY AND IT IS SUBJECT TO CHANGE. IT IS INDICATIVE ONLY AND IS
NO OFFER Barclays is not offering to sell or seeking offers to buy any product or enter into any transaction. Any transaction requires Barclaysâ subsequent formal agreement
which will be subject to internal approvals and binding transaction documents.
NO LIABILITY Barclays is not responsible for the use made of this document other than the purpose for which it is intended, except to the extent this would be prohibited by
law or regulation.
NO ADVICE OBTAIN INDEPENDENT PROFESSIONAL ADVICE BEFORE INVESTING OR TRANSACTING. Barclays is not an advisor and will not provide any advice
relating to a product. Before making an investment decision, investors should ensure they have sufficient information to ascertain the legal, financial, tax and regulatory
consequences of an investment to enable them to make an informed investment decision.
THIRD PARTY INFORMATION
Barclays is not responsible for information stated to be obtained or derived from third party sources or statistical services.
PAST & SIMULATED PAST PERFORMANCE
Any past or simulated past performance (including back-testing) contained herein is no indication as to future performance.
OPINIONS SUBJECT TO CHANGE All opinions and estimates are given as of the date hereof and are subject to change. Barclays is not obliged to inform investors of any
change to such opinions or estimates.
NOT FOR RETAIL
This document is being directed at persons who are professionals and is not intended for retail customer use.
IMPORTANT DISCLOSURES
are unable to access.
For important regional disclosures you must read, click on the link relevant to your region. Please contact your Barclays representative if you
EMEA EMEA Disclosures APAC APAC Disclosures U.S. US Disclosures
IRS CIRCULAR 230 DISCLOSURE: Barclays does not provide tax advice. Please note that (i) any discussion of US tax matters contained in this communication (including any
attachments) cannot be used by you for the purpose of avoiding tax penalties; (ii) this communication was written to support the promotion or marketing of the matters addressed
herein; and (iii) you should seek advice based on your particular circumstances from an independent tax advisor.
CONFIDENTIAL
This document is confidential and no part of it may be reproduced, distributed or transmitted without the prior written permission of Barclays.
ABOUT BARCLAYS Barclays Bank PLC offers premier investment banking products and services to its clients through Barclays Bank PLC. Barclays Bank PLC is authorised
by the Prudential Regulation Authority and regulated by the Financial Conduct Authority and the Prudential Regulation Authority and is a member of the London Stock Exchange.
Barclays Bank PLC is registered in England No. 1026167 with its registered office at 1 Churchill Place, London E14 5HP.
COPYRIGHT
Š Copyright Barclays Bank PLC, 2014 (all rights reserved).
13 | Functional Programming | 1 July 2013