Privatedef fileshere = (NewJava.io.File (".") . Listfiles () def filesending (query:string)= for(File <-fileshere;iffile.getName.endsWith (query)) yield file def filescontaining (query:string)= for(File <-fileshere;iffile.getName.contains (query)) yield file def filesregex (query:string)= for(File <-fileshere;iffile.getName.matches (query)) yield file def filesmatching (query:string,matcher: (string,string)=>boolean) = for(File <-fileshere;ifMatcher (file.getname,query)) yield file//Simplifying Code//def filesending (query:string) = filesmatching (Query, _.endswith (_))//def filescontaining (query:string) = filesmatching (Query, _.contains (_))//def filesregex (query:string) = filesmatching (Query, _.matches (_))
The function Text _.endswith (_) used in the Filesending method is equivalent to (filename:string, query:string) = Filename.endswith (query)
The reason is that filesmatching has a function that requires two string arguments, but you do not need to specify the parameter type. Therefore, you can also write (fileName, query) = filename.endswith (query). Because the first parameter, FileName, is used first in the method body, the second argument, query, and second, you can also use the placeholder syntax: _.endswith (_). The first underscore is the first parameter, the placeholder for the file name, the second underscore is the second argument, and the placeholder for the query string.
Here's how to use this method to determine if an incoming list contains a negative number example:
def Containsneg (Nums:list[int]): Boolean =false for (num <-if (num < 0true exists}scala> Containsneg (List (1, 2, 3, 4false scala< /c10>> Containsneg (List (1, 2, 3, 4true
However, a more concise way to define this method is by calling the higher-order function on the incoming list exists
def containsneg (Nums:list[int] = nums.exists (_<0)
Curry of
Shows the same function after Curry. Instead of a list of two int parameters, you apply this function to each of the two list parameters
Apply on the first function in other words, invoking the first function and passing in to the second result in a secondary function, and applying 2 to the second function produces the result.
The first and second functions are just a demonstration of the curry process. They are not directly connected to the Curriedsum function. Nonetheless, there is still a way to get a reference to the "second" function that actually points to curriedsum. You can use the partial application function expression method, the placeholder label is used in the curriedsum.
Write a new control structure:
The type of OP is double = double, which means that it is a function with a double argument and returns another double.
Scala control abstraction