0% found this document useful (0 votes)
10 views

Js4ap 7.0 Intro To Functional Programming

Here are the key points of the functional programming approach: - Focus on defining functions that transform input values into output values without side effects - Functions should be referentially transparent - the return value depends only on input parameters - Avoid changing state and instead pass explicit values between functions - Rely on higher order functions like map, filter, and reduce to operate on sequences This example defines a factorial function that takes a parameter x, generates a range of numbers from x to 1, and uses reduce to multiply those numbers together, returning the factorial of the input value.
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Js4ap 7.0 Intro To Functional Programming

Here are the key points of the functional programming approach: - Focus on defining functions that transform input values into output values without side effects - Functions should be referentially transparent - the return value depends only on input parameters - Avoid changing state and instead pass explicit values between functions - Rely on higher order functions like map, filter, and reduce to operate on sequences This example defines a factorial function that takes a parameter x, generates a range of numbers from x to 1, and uses reduce to multiply those numbers together, returning the factorial of the input value.
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 18

JavaScript for ABAP Programmers

Chapter 7.0 - Introduction to Functional Programming


Chris Whealy / Technology RIG
ABAP JavaScript
Data typing Strong Weak
Syntax Similar to COBOL Derived mainly from Java with some influence from C
Variable Scope Block scope Lexical (or function) scope
“Function” No concept of a function as a Functions are 1st class citizens.
Concept data object That is, a function is an object with “an executable part”
Inheritance Model Class based Referential (or Prototypical)
Programming Style Strictly Imperative Both Imperative and Functional
Functional Programming
Different Schools of Thought
The Differences Between Imperative and Functional
Programming
Different Schools of Thought
Programming in General 1/3

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

Procedural/ Unstructured Object Functional Logic


Structured Oriented

Pascal C ABAP Assembler Java JavaScript C++ ABAP Clojure Erlang JavaScript Prolog

© 2015 SAP AG. All rights reserved. 5


Programming in General 2/3

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

Procedural/ Unstructured Object Functional Logic


Structured Oriented

Pascal C ABAP Assembler Java JavaScript C++ ABAP Clojure Erlang JavaScript Prolog

© 2015 SAP AG. All rights reserved. 6


Programming in General 3/3

Here we will be looking at using JavaScript in both the Imperative and Functional styles.

Imperative Declarative

Procedural/ Unstructured Object Functional Logic


Structured Oriented

Pascal C ABAP Assembler Java JavaScript C++ ABAP Clojure Erlang JavaScript Prolog

© 2015 SAP AG. All rights reserved. 7


The Böhm-Jacopini Theorem

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:

Sequence A simple sequence of “do this, then this, followed by that…”


Selection Alters the instruction flow based on the outcome of a condition
Iteration* Performs a task multiple times until some termination condition is reached

The major differences between imperative and functional programming lie in how you arrange the
statements of your program within these three control structures.

* From the Latin “ite” meaning “go”

© 2015 SAP AG. All rights reserved. 8


The Differences Between Imperative
and Functional Programming
Imperative Programming: Do exactly this and do it now!

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)

© 2015 SAP AG. All rights reserved. 10


Imperative Programming: Focusing on “How” and “When”

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.

© 2015 SAP AG. All rights reserved. 11


Imperative Programming: Summary

Imperative programming languages give the programmer the necessary tools for explicitly defining
the three fundamental control structures:

Sequence A set of instructions whose execution order is explicitly defined


Selection Implemented using statements such as if or switch or case that modify the
instruction flow based on the outcome of an explicitly defined condition
Iteration Implemented using statements such as do/while or for loops

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.

© 2015 SAP AG. All rights reserved. 12


Functional Programming: Focusing on “What”

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.**

(defn factorial [x] (reduce * (range x 0 -1)))

1) We define a function called factorial


that takes a single parameter x.

* 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.

(defn factorial [x] (reduce * (range x 0 -1)))

2) Create a descending range of numbers


from the parameter value x down to 1.

3) Use the reduce function to apply the multiply (*)


function to all the numbers in this range.

© 2015 SAP AG. All rights reserved. 15


Functional Programming: A Simple Example 3/3

Here’s a simple functional program written in Clojure that calculates the factorial value of a number.

(defn factorial [x] (reduce * (range x 0 -1)))

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.

© 2015 SAP AG. All rights reserved. 16


Functional Programming: Summary

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.

Sequence A set of instructions whose execution order is explicitly defined


Selection Often implemented by the language runtime using pattern matching, rather than by an
explicitly coded condition such as if or switch or case.
Iteration Implemented by means of recursion.

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.

© 2015 SAP AG. All rights reserved. 18

You might also like