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

Tutorial-4

The document contains a tutorial on advanced programming concepts focusing on higher-order functions and currying. It includes questions that require comparisons between functions, code implementations in Java and JavaScript, and discussions on functional programming techniques. Additionally, it provides exercises on applying currying and modifying existing code to demonstrate these concepts.

Uploaded by

Noval Ariandy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Tutorial-4

The document contains a tutorial on advanced programming concepts focusing on higher-order functions and currying. It includes questions that require comparisons between functions, code implementations in Java and JavaScript, and discussions on functional programming techniques. Additionally, it provides exercises on applying currying and modifying existing code to demonstrate these concepts.

Uploaded by

Noval Ariandy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Advanced Programming Language Concepts (CT006-3-3) HOF - Currying

Tutorial -4
Question:
1. Compare and contrast between function/method and higher order function/method.

2. Given the username as follows. Write a functional Java/Javascript code to find a username
called “salleh”. Display the name if found, otherwise, display a null.
String[] username = { "ali", "ahmad", "maria", "john", "derick", "salleh" };

3. With the use of lambda expression, modify the following code in Java to achieve the same
outcome.
class Joiner{
String join(String a, String b) { return a + b; }
}
Joiner joiner = new Joiner();
System.out.println( joiner.join(str1, str2) );

4. Consider the following code.


class TestComputer{
static int sum = 0;
static int compute(int x, int[] a) {
for (int i : a) {
sum += i;
}
return x * sum;
}
}

Discuss the outcome of the code execution. Review the source code by implementing
functional concept using Java/Javascript language.

5. Given the mathematical statement below. Explain how could currying technique be
applied.

f(20, 15) = 20 + 15

Demonstrate the functional code for curried function.

Level 3 Asia Pacific University of Technology and Innovation Page 1 of 1


Advanced Programming Language Concepts (CT006-3-3) HOF - Currying

6. Based on f(a, b, c, d) = (a + b) * (c + d). Adapt the following code to curried function.


double f(int a, int b, int c, int d) {
return (a+b)*(c+d);
}

7. Consider the Membership class and List object comprising 4 membership objects. Write a
functional code to top up the membership points by 10. After increasing the membership
points, populate all membership objects in a new List. Display the old (note: immutable
list) and new List containing Membership points.

class Membership{
constructor(pt){ this.point = pt; }
}

const lst = [
new Membership(100),
new Membership(200),
new Membership(300),
new Membership(400),
];

Level 3 Asia Pacific University of Technology and Innovation Page 2 of 1

You might also like