Tutorial 4
Tutorial 4
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 {
//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;
//getter
public void setRadius(int radius) {
this.radius = radius;
}
let PI = 3.14;
const calculateAreaar = (radius) => radius * radius * PI;
console.log( calculateArea(10) );
return circleArea;
};
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]