Js4ap 7.0 Intro To Functional Programming
Js4ap 7.0 Intro To Functional Programming
In broad terms, the world of computer programming is split into various, overlapping schools of
thought. These schools of thought differ primarily on the priority they assign to different aspects of
software design.
The (very incomplete) diagram below shows two of the main schools of thought in programming and
some of their more widely known subdivisions.
Imperative Declarative
Pascal C ABAP Assembler Java JavaScript C++ ABAP Clojure Erlang JavaScript Prolog
The divisions between these categories are not hard and fast, and some languages such as ABAP
and JavaScript can belong to multiple categories.
Imperative Declarative
Pascal C ABAP Assembler Java JavaScript C++ ABAP Clojure Erlang JavaScript Prolog
Here we will be looking at using JavaScript in both the Imperative and Functional styles.
Imperative Declarative
Pascal C ABAP Assembler Java JavaScript C++ ABAP Clojure Erlang JavaScript Prolog
No matter which programming language or style is used, the statements in all computer programs can
be viewed as some combination of three basic control structures:
The major differences between imperative and functional programming lie in how you arrange the
statements of your program within these three control structures.
The “imperative programming” school of thought is by far the most widely used style of coding and is
implicitly assumed to be the “normal” way to program a computer.
This style of programming arose from the computer architecture defined by John von Neumann in
1945 and is best suited to situations in which a known computation must be performed on a
predictable dataset.
In this style of coding, the programmer is focused primarily on:
• Transforming the computer’s state (I.E. the data in memory) by means of a precisely defined set of
instructions.
• Making explicit use of beneficial side-effects to achieve the desired result:
• The use of global or shared memory as the repository for the evolving state of the data
• Interacting with persistent storage (E.G a database)
• Interacting with peripheral devices (printer, network, camera, accelerometer etc)
Although “Imperative programming” is a broad term that includes many other styles of coding (E.G.
procedural, modular, object oriented etc.), the programmer’s job is essentially:
To create a set of instructions that define the exact
sequence in which the computer’s state should be modified
Yes Do this
Do this Do that Is this true? Stop
No Do that
A major consequence of this approach is that as a programmer, you are often focused on the timeline
of how the computer’s state evolves. Something along the lines of:
Now that state “A” has been reached, I can start the processing to create state “B”,
and this must complete before the processing to create state “C” can start.
Imperative programming languages give the programmer the necessary tools for explicitly defining
the three fundamental control structures:
Imperative programming is strongly focused on the mechanical process of how a program arrive at
the required solution. As a consequence of this emphasis, a programmer must spend a large amount
of time and effort specifying in precise detail when the program needs to do what it does.
In functional programming however, there is a very different focus. As much as possible, functional
programming languages emphasise what a program should accomplish and are less concerned with
exactly how that objective is achieved.
Functional programming is focused on:
• Understanding and then defining the set of functions needed to transform the input data into the
output data
• Writing those functions such that the principle of “Referential Transparency” is closely followed.
This means that each function is structured such that it:
• Receives one or more parameters
• Is (as much as possible) free from side-effects.* I.E. the return value is derived only from the
values received as parameters.
• Returns a result to the calling function (where that result will often be another function)
* There are certain cases where side-effects are valid and legitimate (such as interacting with persistent storage
or a peripheral device). Depending on your choice of functional programming language, but these situations are
either handled explicitly by the special language constructs, or as Monads.
© 2015 SAP AG. All rights reserved. 13
Functional Programming: A Simple Example 1/3
Here’s a simple functional program written in Clojure that calculates the factorial* value of a number.**
* factorial(n) = n * n-1 * n-2 * … * 1. ** This program is not 100% correct because it does not
So factorial(4) or 4! = 4 * 3 * 2 * 1 = 24 handle the special case that 0! = 1, but let’s not worry about
© 2015 SAP AG. All rights reserved.
that…
14
Functional Programming: A Simple Example 2/3
Here’s a simple functional program written in Clojure that calculates the factorial value of a number.
Here’s a simple functional program written in Clojure that calculates the factorial value of a number.
user> (factorial 5)
120
user>
reduce and range are not reserved words in this programming language - they are other functions
that we assume can be invoked.
The fundamental difference here is that we’ve created a program that says what needs to be done in
order to calculate the factorial of a number, and we’ve left it up to Clojure to care about exactly how
the reduce and range functions are implemented.
Functional programming languages still arrange their statements according to the three basic control
structures defined by the Böhm-Jacopini Theorem, but this often happens as an implicit consequence
of the functional programming style, rather than the explicit choice of the programmer.
Functional programming is strongly focused on creating a solution whose steps avoid (or in some
languages, prohibit) the use of side-effects.
For example, a language such as Haskell is known as a “pure functional” programming language
because it explicitly prevents side-effects such as the manipulation of global or shared memory.
When there is a genuine need for side-effect (E.G. writing to persistent storage), then a construct
called a Monad is used.
© 2015 SAP AG. All rights reserved. 17
© 2015 SAP AG. All rights reserved.
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express Google App Engine, Google Apps, Google Checkout, Google Data API, Google Maps, Google Mobile Ads,
permission of SAP AG. The information contained herein may be changed without prior notice. Google Mobile Updater, Google Mobile, Google Store, Google Sync, Google Updater, Google Voice,
Google Mail, Gmail, YouTube, Dalvik and Android are trademarks or registered trademarks of Google Inc.
Some software products marketed by SAP AG and its distributors contain proprietary software components of
other software vendors. INTERMEC is a registered trademark of Intermec Technologies Corporation.
Microsoft, Windows, Excel, Outlook, PowerPoint, Silverlight, and Visual Studio are registered trademarks of Wi-Fi is a registered trademark of Wi-Fi Alliance.
Microsoft Corporation.
Bluetooth is a registered trademark of Bluetooth SIG Inc.
IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System
Motorola is a registered trademark of Motorola Trademark Holdings LLC.
z10, z10, z/VM, z/OS, OS/390, zEnterprise, PowerVM, Power Architecture, Power Systems, POWER7,
POWER6+, POWER6, POWER, PowerHA, pureScale, PowerPC, BladeCenter, System Storage, Storwize, Computop is a registered trademark of Computop Wirtschaftsinformatik GmbH.
XIV, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, AIX, Intelligent Miner, WebSphere, Tivoli,
Informix, and Smarter Planet are trademarks or registered trademarks of IBM Corporation. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer, StreamWork,
SAP HANA, and other SAP products and services mentioned herein as well as their respective logos are
Linux is the registered trademark of Linus Torvalds in the United States and other countries. trademarks or registered trademarks of SAP AG in Germany and other countries.
Adobe, the Adobe logo, Acrobat, PostScript, and Reader are trademarks or registered trademarks of Adobe Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web
Systems Incorporated in the United States and other countries. Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their
respective logos are trademarks or registered trademarks of Business Objects Software Ltd. Business Objects
Oracle and Java are registered trademarks of Oracle and its affiliates.
is an SAP company.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase products and
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or services mentioned herein as well as their respective logos are trademarks or registered trademarks of Sybase
registered trademarks of Citrix Systems Inc. Inc. Sybase is an SAP company.
HTML, XML, XHTML, and W3C are trademarks or registered trademarks of W3C®, World Wide Web Crossgate, m@gic EDDY, B2B 360°, and B2B 360° Services are registered trademarks of Crossgate AG
Consortium, Massachusetts Institute of Technology. in Germany and other countries. Crossgate is an SAP company.
Apple, App Store, iBooks, iPad, iPhone, iPhoto, iPod, iTunes, Multi-Touch, Objective-C, Retina, Safari, Siri, All other product and service names mentioned are the trademarks of their respective companies. Data
and Xcode are trademarks or registered trademarks of Apple Inc. contained in this document serves informational purposes only. National product specifications may vary.
IOS is a registered trademark of Cisco Systems Inc. The information in this document is proprietary to SAP. No part of this document may be reproduced, copied,
or transmitted in any form or for any purpose without the express prior written permission of SAP AG.
RIM, BlackBerry, BBM, BlackBerry Curve, BlackBerry Bold, BlackBerry Pearl, BlackBerry Torch, BlackBerry
Storm, BlackBerry Storm2, BlackBerry PlayBook, and BlackBerry App World are trademarks or registered
trademarks of Research in Motion Limited.