Scala control abstraction

Source: Internet
Author: User

  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.