27
loading...
This website collects cookies to deliver better user experience
map
.map
, it is stated that the map
function takes in another function and an iterable as input parameters and returns an iterator that yields the results [4].scala.collections
and its subsets contain the map
method that is defined by the following function signatures on ScalaDoc [5]:def map[B](f: (A) => B): Iterable[B] // for collection classes
def map[B](f: (A) => B): Iterator[B] // for iterators that access elements of a collection
map
takes a function input parameter f
, and f
transforms a generic input of type A
to a resulting value of type B
.def square(x):
return x * x
def main(args):
collection = [1,2,3,4,5]
# initialize list to hold results
squared_collection = []
# loop till the end of the collection
for num in collection:
# square the current number
squared = square(num)
# add the result to list
squared_collection.append(squared)
print(squared_collection)
squared
variable holding the result returned from the square
function; andmap
function can be used to “map” each element in the collection to a new collection with the same number of elements as the input collection - by applying the square operation to each element and collecting the results into the new collection.def square(x):
return x * x
def main(args):
collection = [1,2,3,4,5]
squared = list(map(square, collection))
print(squared)
object MapSquare {
def square(x: Int): Int = {
x * x
}
def main(args: Array[String]) {
val collection = List[1,2,3,4,5]
val squared = collection.map(square)
println(squared)
}
}
map
function accepts an input function that is applied to each element in a collection of values and returns a new collection containing the results. As map
has the property of accepting another function as a parameter, it is a higher-order function.map
vs Scala map
: An iterable function such as list
is needed to convert the iterator returned from the Python map
function into an iterable. In Scala, there is no need for explicit conversion of the result from the map
function to an iterable, as all methods in the Iterable
trait are defined in terms of an abstract method, iterator
, which returns an instance of the Iterator
trait that yields the collection’s elements one by one [6].return
keyword is used in Python to return a function result, the return
keyword is rarely used in Scala. Instead, the last line within a function declaration is evaluated and the resultant value is returned when defining a function in Scala. In fact, using the return
keyword in Scala is not good practice for functional programming as it abandons the current computation and is not referentially transparent [7-8].lambda
keyword and wraps a single expression without using def
or return
keywords. For example, the square
function in the previous example in Python can be expressed as an anonymous function in the map
function, where the lambda expression lambda x: x * x
is used as a function input parameter to map
:def main(args):
collection = [1,2,3,4,5]
squared = map(lambda x: x * x, collection)
print(squared)
=>
notation - where the function arguments are defined to the left of the =>
arrow and the function expression is defined to the right of the =>
arrow. For example, the square
function in the previous example in Scala can be expressed as an anonymous function with the (x: Int) => x * x
syntax and used as a function input parameter to map
:object MapSquareAnonymous {
def main(args: Array[String]) {
val collection = List[1,2,3,4,5]
val squared = collection.map((x: Int) => x * x)
println(squared)
}
}
for
loop, and using while
loop.def factorial_for(n):
# initialize variable to hold factorial
fact = 1
# loop from n to 1 in decrements of 1
for num in range(n, 1, -1):
# multiply current number with the current product
fact = fact * num
return fact
def factorial_while(n):
# initialize variable to hold factorial
fact = 1
# loop till n reaches 1
while n >= 1:
# multiply current number with the current product
fact = fact * n
# subtract the number by 1
n = n - 1
return fact
def factorial(n):
# base case to return value
if n <= 0: return 1
# recursive function call with another set of inputs
return n * factorial(n-1)
def factorial(n: Int): Long = {
if (n <= 0) 1 else n * factorial(n-1)
}
factorial(5)
if (5 <= 0) 1 else 5 * factorial(5 - 1)
5 * factorial(4) // factorial(5) is added to call stack
5 * (4 * factorial(3)) // factorial(4) is added to call stack
5 * (4 * (3 * factorial(2))) // factorial(3) is added to call stack
5 * (4 * (3 * (2 * factorial(1)))) // factorial(2) is added to call stack
5 * (4 * (3 * (2 * (1 * factorial(0))))) // factorial(1) is added to call stack
5 * (4 * (3 * (2 * (1 * 1)))) // factorial(0) returns 1 to factorial(1)
5 * (4 * (3 * (2 * 1))) // factorial(1) return 1 * factorial(0) = 1 to factorial(2)
5 * (4 * (3 * 2)) // factorial(2) return 2 * factorial(1) = 2 to factorial(3)
5 * (4 * 6) // factorial(3) return 3 * factorial(2) = 6 to factorial(4)
5 * 24 // factorial(4) returns 4 * factorial(3) = 24 to factorial(5)
120 // factorial(5) returns 5 * factorial(4) = 120 to global execution context
def factorialTailRec(n: Int): Long = {
def fact(n: Int, product: Long): Long = {
if (n <= 0) product
else fact(n-1, n * product)
}
fact(n, 1)
}