Js4ap 7.1 A FP Example
Js4ap 7.1 A FP Example
This shift of emphasis from how to what is far more than an academic detail; it fundamentally alters
the way you think about software design!
A programmer familiar with the architecture of coding used in imperative languages such as ABAP or
Java is strongly focused on exactly how the instructions in their program should be structured and
executed. A related emphasis in imperative programming is that you are also strongly focused on the
timeline of the exact order which instructions are executed.
However in functional programming, the emphasis is on defining what steps are required to transform
the input data into the desired output, and is much less concerned with how these steps have been
implemented.
This is best illustrated with a small JavaScript program written in both the imperative and functional
styles.
Here is a simple JavaScript function that calculates the nth number in the Fibonacci sequence (using
the formula fibn = fibn-2 + fibn-1)
// Calculate nth Fibonacci number: imperative style
function fib_imperative(n) {
var a = 0, b = 1, sum = 0;
while (n>1) {
sum = a + b;
a = b;
b = sum;
n = n - 1;
}
return sum;
}
fib_imperative(8); // 21
Here is a simple JavaScript function that calculates the nth number in the Fibonacci sequence (using
the formula fibn = fibn-2 + fibn-1)
// Calculate nth Fibonacci number: imperative style Function fib_imperative receives a single
function fib_imperative(n) {
var a = 0, b = 1, sum = 0; parameter n and uses this value to control a simple
while loop.
while (n>1) {
sum = a + b; Inside the loop, three variables (a, b and sum) are
a = b; used to maintain the state of the calculation.
b = sum;
n = n - 1;
}
return sum;
}
fib_imperative(8); // 21
Here is a simple JavaScript function that calculates the nth number in the Fibonacci sequence (using
the formula fibn = fibn-2 + fibn-1)
// Calculate nth Fibonacci number: imperative style Function fib_imperative receives a single
function fib_imperative(n) {
var a = 0, b = 1, sum = 0; parameter n and uses this value to control a simple
while loop.
while (n>1) {
sum = a + b; Inside the loop, three variables (a, b and sum) are
a = b; used to maintain the state of the calculation.
b = sum;
n = n - 1; The final value is accumulated in variable sum which
}
then becomes the return value.
return sum;
}
Notice that as the loop progresses, the value of
variable sum continually changes.
fib_imperative(8); // 21
This is a classic example of the imperative coding
style in which the execution of the program causes
the computer’s state to evolve towards the desired
result.
The first thing to notice here is that the program now uses functions as the basic structural unit.
Also notice that familiar statements such as if and while are missing.
// Calculate nth Fibonacci number: functional style
function fib_functional(n) {
return do_fib(0,1,n);
}
function do_fib(a,b,n) {
return (n === 1) ? b : do_fib(b,a+b,n-1);
}
fib_functional(8); // 21
The actual calculation is performed by a function called do_fib that takes 3 parameters a, b and n.
Parameter a is fibn-2, b is fibn-1 and n is the desired number from the Fibonacci series.
// Calculate nth Fibonacci number: functional style
function fib_functional(n) {
return do_fib(0,1,n);
}
function do_fib(a,b,n) {
return (n === 1) ? b : do_fib(b,a+b,n-1);
}
fib_functional(8); // 21
There are several important differences about the coding style here:
function do_fib(a,b,n) {
return (n === 1) ? b : do_fib(b,a+b,n-1);
}
fib_functional(8); // 21
There are several important differences about the coding style here:
function do_fib(a,b,n) {
return (n === 1) ? b : do_fib(b,a+b,n-1);
}
fib_functional(8); // 21
2. Within function do_fib, notice that no internal variables are declared (no var statements).
The actual calculation of the next number in the Fibonacci sequence is performed at the time the parameters
are passed to the recursive do_fib call.
3. Use of the while keyword is no longer required because iteration is now controlled by recursion.
Recursion stops when the control value n reaches 1, by which time the required value will be in variable b.
There are several important differences about the coding style here:
function do_fib(a,b,n) {
return (n === 1) ? b : do_fib(b,a+b,n-1);
}
fib_functional(8); // 21
4. Function do_fib is not exposed directly to the end user because in order to call it, you would need to have
some internal knowledge of its three parameter values.
Therefore, these internal implementation details are hidden by calling do_fib from within the wrapper function
fib_functional which acts as the public API
There are several important differences about the coding style here:
fib_functional(8); // 21
The functional solution is now much simpler than the imperative solution in that it has been reduced down to two
simple, side-effect free functions.
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.