Advanced Programming Language Concepts (CT006-3-3) Purity and Side Effects
Tutorial -3
Question:
1. Define the pure function/method. Compare it to impure function/method.
Pure function does not include mutable state Impure function include mutable state of data
of data
Pure function is easy to test , output is Testing in impure function is generally more
predictable , constant input always give complex , have to consider side effects /
constant output external dependenies.
Can be parallelized , as they don’t modify Concurrency issues may happen , as it include
state, don’t have shared state, multiple threads shared data, for example , multiple thread
can execute pure function without worrying access share data simultaneously , the data
data corruption being modify by one of the thread, then other
thread may get inconsistent value
No side effect, as they don’t alter the state of Have side effect , impure function can modify
data external state /data, such as global variable ,
db value,and such
2. Appraise the concept of “side effects”. Explain TWO (2) side effects in any programming
language with code examples.
3. Given the Java code as follows. Explain the effect of code design.
public class Demo {
public static void main(String[] args) {
//demo
Circle c1 = new Circle();
c1.setRadius( 10 );
System.out.println( "=== Area, Circumference, Diameter ===" );
c1.area();
System.out.println( "Area: " + c1.area);
c1.circumference();
System.out.println( "Circumference: " + c1.circumference);
c1.diameter();
System.out.println( "Diameter: " + c1.diameter);
}
class Circle{
int radius;
double area;
double circumference;
double diameter;
Level 3 Asia Pacific University of Technology and Innovation Page 1 of 2
Advanced Programming Language Concepts (CT006-3-3) Purity and Side Effects
//getter
public void setRadius(int radius) {
this.radius = radius;
}
public void area() {
if( radius > 0 ) {
this.area = Math.PI * Math.pow(radius, 2);
}
}
public void circumference() {
if( radius > 0 ) {
this.circumference = 2 * Math.PI * radius;
}
}
public void diameter() {
if( radius > 0 ) {
this.diameter = 2 * radius;
}
}
4. Justify the Math.random() from the perspective of functional programming paradigm.
5. Improve this code snippet in Java/Javascript.
let PI = 3.14;
const calculateAreaar = (radius) => radius * radius * PI;
console.log( calculateArea(10) );
const calculateArea = (radius) =>{
const circleArea = radius**2 * Math.Pi;
console.log(circleArea);
return circleArea;
};
Level 3 Asia Pacific University of Technology and Innovation Page 2 of 2
Advanced Programming Language Concepts (CT006-3-3) Purity and Side Effects
6. Discuss in group the impact of purity in computer programming.
Given an array list as follows. Write a Javascript statement for the following requirements.
[12,34,21,4,56,77,88,44,885,2,5,7,98,54]
7. Iterate and display through all elements of array list.
8. Search and display an element, ie., 885.
filter
9. Double up each array element by 2 and store them in another array list.
Map
Map & review divide & conquer
When do review can use parallel processing
Level 3 Asia Pacific University of Technology and Innovation Page 3 of 2