Question: You are given a piece of procedural code that processes a list of integers. Refactor it to use lambda expressions and higher - order functions.
You are given a piece of procedural code that processes a list of integers. Refactor it to use lambda expressions and higherorder functions.
fun main
val numbers listof
Calculate the square of each number
val squares mutableListof
for number in numbers
squares. add number number
Filter even numbers
val evenSquares mutableListOf
for square in squares
if square
evenSquares. add square
printinEven squares: SevenSquares"
Your Task: Refactor the above code using:
Lambda expressions for calculating squares.
Higherorder functions like map and filter.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
