How to resolve the algorithm Apply a callback to an array step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Apply a callback to an array step by step in the Scala programming language

Table of Contents

Problem Statement

Take a combined set of elements and apply a function to each element.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Apply a callback to an array step by step in the Scala programming language

Source code in the scala programming language

val l = List(1,2,3,4)
l.foreach {i => println(i)}

l.foreach(println(_))

val a = Array(1,2,3,4)
a.foreach {i => println(i)}
a.foreach(println(_))  '' // same as previous line''

def doSomething(in: int) = {println("Doing something with "+in)}
l.foreach(doSomething)

for(val i <- a) println(i)

val squares = l.map{i => i * i} ''//squares is''  List(1,4,9,16)

val squares = for (val i <- l) yield i * i

  

You may also check:How to resolve the algorithm Vector step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Bell numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm De Bruijn sequences step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Number reversal game step by step in the C programming language
You may also check:How to resolve the algorithm LZW compression step by step in the Nim programming language