In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what are the high-order functions in the Scala language", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the high-order functions in the Scala language" this article.
Explanation of common functions
-- > map acts on every element in the list
/ / define a list val list= List (1, i:Int 2, 3, 4, 5, 6, 7, 8, 9) / / all the elements in the list list are multiplied by 2list.map ((i:Int) = > iTun2) / / do the same operation with foreach, and foreach will not return the value list.foreach ((i:Int) = > iTun2)
-> filter filters elements with a return value of False
Val list = List (1, Int, 3, 5, 6, 7, 8, 9) list.filter ((i:Int) = > I% 2) / / the returned result is: List [Int] = List (2, 4, 6, 8) / / can also be written as num.filter (_% 2).
-- > zip merges two arrays
List (1 Int 2) .zip (List 4) / / the output is List [(Int, Int)] = List ((1) 4), (2) 5), (3))
-> partition put the elements that meet the filtering rules in one partition and those that do not comply with the filtering rules in another partition
List ((i:Int) = > I% 2) / / output result is: (List [Int], List [Int]) = (List (2,4,6,8), List (1,3,5,7,9))
-> find found the first element in the list that conforms to the rule
-> flatten flattening operation, converting a three-dimensional array into a two-dimensional array or a two-dimensional array into an one-dimensional array. It should be noted that each element must be extracted again during the conversion, otherwise an error will be reported.
List (List (1) list (2)), List (List (5) 6), List (8). Flatten// if you want to continue with the flattening operation List (List), List (List), List (8)). Flatten.flattenerror::12: error: No implicit view available from Any = > scale.GenTraversableOnce [B] .List (List), List (List (5pm 6), List (8)). Flatten.flatten// to continue the flattening operation The processing needs to be modified by List (1), List), List (List (5 Int 7), List (8)). The flatten.flatten// result is: List [Int] = List
-> flatMap is equivalent to the combination of Map and flatten
Val list = List (List (1 x.map 2), List) / / the x here is equivalent to the result of flattening the source data list.flatMap (x = > x.map (_ * 2)) / / this statement is equivalent to the following two-step operation val x = list.flattenx.map (_ * 2).
Nesting of closure functions
-- > define ordinary functions:
Def myfun (x:Int, y:Int): Int = x * y
-> adopt closure
/ / define a closure function, where x is the multiplier factor, that is, the multiple of multiplication (which I myself understand as the multiplier), and y is the parameter def myfun (x:Int) = (y:Int) = > x * y y:Int / the first step is to define your own function model val mytest = myfun (2) / use the self-defined function model, pass in the parameter mytest (20)
The essence of the name of a mathematician is to convert a multi-parameter function into a chain of functions, with a single parameter function on each node.
For example:
Def add (x:Int, y:Int): Int = x + ydef add (x:Int) (y:Int) = x + y are all the contents of the article "what are the higher-order functions in the Scala language?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.