in Education by
While working through a problem found in 'Functional Programming in Scala': Implement isSorted, which checks whether an Array[A] is sorted according to a given comparison function: def isSorted[A](as: Array[A], ordered: (A,A) => Boolean): Boolean While comparing my answer to the below solution provided by the author(s): // Exercise 2: Implement a polymorphic function to check whether // an `Array[A]` is sorted def isSorted[A](as: Array[A], gt: (A,A) => Boolean): Boolean = { @annotation.tailrec def go(n: Int): Boolean = if (n >= as.length-1) true else if (gt(as(n), as(n+1))) false else go(n+1) go(0) } I am getting confused by the following line of code: else if (gt(as(n), as(n+1))) false I thought when using a function as a parameter in Scala, one would need to actually define a separate function outside the scope of the current function (i.e. have another function defined that does what gt would need to do)? I don't see gt defined anywhere else, so how is it able to provide a Boolean value to use in isSorted? Are my assumptions correct, or am I completely something missing here with using functions as parameters? A detailed explanation is much appreciated. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
In this case, gt is a function passed to isSorted as an argument; Let's compare it to any other argument, say a: Int. Let's look at the following function: def increaseByOne(a: Int): Int = a + 1 Your question about gt is equivalent to asking: "How can increaseByOne use a if a is never assigned a value?". Obviously, the caller of the increaseByOne method would have to pass some value as the value of a, but the very concept of arguments is naming some value that is provided into the method. The same goes for gt: it's an argument of isSorted. In the code you presented, we don't see the caller of isSorted, so we don't see any example of what gt might be - but obviously the implementation of isSorted need not assume anything about how and where gt is created, it is passed as an argument and therefore can be used in isSorted. We can imagine a usage such as: isSorted(Array(1,3,6), (a: Int, b: Int) => a > b) Here, (a: Int, b: Int) => a > b would be the value of gt.

Related questions

0 votes
    Why does a sorted array get processed faster than an unsorted array, even though the size of the arrays are ... processed faster. Why? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Here is a piece of C++ code that shows some very peculiar behavior. For some strange reason, sorting the data ... = static_cast(clock()-start) / CLOCKS_PER_SEC; std::cout...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    Write a program to store 10 names in an array. Arrange these in alphabetical orderby sorting. Print the sorted list. ... , LUCY, etc. Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    Considering the following sorted array and the search key as 89, what will be the sequence of keys in the array ... searching for 89? Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Write a program to eliminate duplicates in a sorted array. in python Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    We are building sentiment analysis application and we converted our tweets dataframe to an array. We created another array ... .txt").getLines.toArray var happyCount=0 for (e...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    We are building sentiment analysis application and we converted our tweets dataframe to an array. We created another array ... .txt").getLines.toArray var happyCount=0 for (e...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I was trying to solve this hiring contest problem (now closed) Lexicographic Rows You are given a matrix of characters. In one ... { //Current is valid if (input[i - 1][j]...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    read the data from employee. text file and sort on age using counting sort,merge sort,quick sort and write ... sortedemponage.txt'. Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    What is the best method to store row data in a sorted order ? (1)use a clustering key (2)minimize the number of partions. (3)partition (4)use a primary key...
asked May 6, 2021 in Technology by JackTerrance
0 votes
    Which type of Key-value datastore DB has its key and value sorted? (1)Solid-State Key-Value datastore ( ... Ordered Key-Value datastore (4)Eventually Consistent Key-Value datastore...
asked Apr 23, 2021 in Technology by JackTerrance
0 votes
    Sorted Column store would provide higher compression ratio by representing each column as ________ compared to the preceding one....
asked Jan 26, 2021 in Technology by JackTerrance
0 votes
    How will you print 10 random numbers in a sorted order in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    What is the purpose of sorted method of stream in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Let's say that I declare a C struct called foo, which has an int field called bar and a char * ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
...