0% found this document useful (0 votes)
51 views25 pages

Structured Programming - Lecture 5

This document discusses scopes, closures, and currying in functional programming. It defines lexical scoping as determining variable accessibility based on position within nested function scopes. Closures are created when a function references variables from an outer scope even when executed outside that scope. Currying transforms functions that take multiple arguments into a series of nested functions that each take a single argument. This allows partial application of functions. The document provides examples of scopes, closures, currying, and using curried functions to manipulate DOM elements.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
51 views25 pages

Structured Programming - Lecture 5

This document discusses scopes, closures, and currying in functional programming. It defines lexical scoping as determining variable accessibility based on position within nested function scopes. Closures are created when a function references variables from an outer scope even when executed outside that scope. Currying transforms functions that take multiple arguments into a series of nested functions that each take a single argument. This allows partial application of functions. The document provides examples of scopes, closures, currying, and using curried functions to manipulate DOM elements.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 25

SWE4205 – STRUCTURED

PROGRAMMING
A. A. Datti
COURSE OUTLINE
1. Introduction to Structured Programming
2. What is functional programming?
3. Functional vs. Object-oriented programming
4. Functions
5. Closures, Currying and Partial Application
6. Understanding your application’s control flow
7. Method chaining and Function chaining
8. Reasoning about your code
9. Learning to think recursively
SWE4205 - STRUCTURED PROGRAMMING 2
CLOSURES
SCOPE
When you define a variable, you want it to exist within some boundaries.
The accessibility of variables is managed by scope. You are free to access the
variable defined within its scope. But outside of that scope, the variable is
inaccessible.
In JavaScript, a scope is created by a function or a code block.
SCOPE
count is freely accessed within the scope of foo().
However, outside of the foo() scope, count is inaccessible. If you try to access count
from outside anyways, JavaScript throws ReferenceError: count is not defined.
SCOPES NESTING
Let's play a bit more with scopes, and nest one scope into another. For example, the
function innerFunc() is nested inside an outer function outerFunc()
How would the 2 function scopes interact with each other? Can I access the variable
outerVar of outerFunc() from within innerFunc() scope?
SCOPES NESTING
Indeed, outerVar variable is
accessible inside innerFunc() scope.
The variables of the outer scope are
accessible inside the inner scope.
THE LEXICAL SCOPE
How does JavaScript understand that outerVar inside innerFunc()
corresponds to the variable outerVar of outerFunc()?
JavaScript implements a scoping mechanism named lexical scoping (or static
scoping). Lexical scoping means that the accessibility of variables is
determined by the position of the variables inside the nested scopes.
Simpler, the lexical scoping means that inside the inner scope you can access
variables of outer scopes.
It's called lexical (or static) because the engine determines (at lexing time)
the nesting of scopes just by looking at the JavaScript source code, without
executing it
THE CLOSURE
Let's make a change: innerFunc() to be invoked outside of its lexical scope: in a
function exec(). Would innerFunc() still be able to access outerVar?
THE CLOSURE
Now innerFunc() is executed outside of its lexical scope, but
exactly in the scope of exec() function. And what's important:

innerFunc() still has access to outerVar from its lexical scope,


even being executed outside of its lexical scope.

In other words, innerFunc() is a closure because it closes over


the variable outerVar from its lexical scope.
THE CLOSURE
“A closure is the
combination of a function
bundled together
(enclosed) with references
to its surrounding state
(the lexical
environment).”

This "effect" of having


CLOSURE
CLOSURES
Closures are a way to implement data hiding (with private variables), which leads to
modules and other nice features.
The key concept of closures is that when you define a function, it can refer to not
only its own local variables but also to everything outside of the context of the
function.
We can write a counting function that will keep its own count by means of a closure:
CLOSURES

Even after newCounter() exits, the inner function still has access to count, but that
variable is not accessible to any other parts of your code.

This isn't a very good example of FP— a function (nc(), in this case) isn’t expected
to return different results when called with the same parameters
FUNCTION
FACTORIES
sayHi(), sayHello(), add5() and
add10() are all Closures.

makeAdder and greeting are function factories


CURRYING - DEFINITION
Currying is a process in functional programming in which we can transform a
function with multiple arguments into a sequence of nesting functions.
It is the process of transforming an m-ary function (that is, a function of arity m)
into a sequence of m unary functions, each of which receives one argument of the
original function, from left to right.
The first function receives the first argument of the original function, and returns a
second function that receives the second argument, and returns a third function that
receives the third argument, and so on.
Upon being called with an argument, each function produces the next one in the
sequence, and the last one does the actual calculations
CURRYING
CURRYING EXAMPLE
Currying is a transformation of functions that translates a function from callable as
f(a, b, c) into callable as f(a)(b)(c).
CURRYING
Suppose you had a function that calculated the Value-added Tax (VAT) for an
amount, as shown here
CURRYING
If you had to apply a single, constant rate, you could then curry the addVAT()
function, to produce a more specialized version that is always applied your given
rate.
For example, if your national rate was 6%, you could then have something like the
following:
BENEFITS OF CURRYING
For instance, we have the logging function log(date, importance, message) that
formats and outputs the information. In real projects such functions have many
useful features like sending logs over the network, here we’ll just use alert:

Let’s curry it!


BENEFITS OF CURRYING
After that log works normally:

…But also works in the curried form:

Now we can easily make a convenience function for current logs:


PARTIAL APPLICATION

Now logNow is log with fixed first argument, in other words “partially applied
function” or “partial” for short.

Partial application: a function is partially applied when it is given fewer arguments


than it expects and returns a new function expecting the remaining arguments
MANIPULATING DOM
ELEMENTS WITH CURRYING
What will be displayed on the Browser?
END

You might also like