Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Learn a little bit of Scala anonymous function every day

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

1. Definition

If a function has no name, it is an anonymous function. It is only used once, and it is not called many times

2. Usage scenarios of anonymous functions

Assign anonymous functions directly to variables

Use anonymous functions as arguments to the function

3. How many representations of anonymous functions? Or the form of presentation

An anonymous function with no parameter input and no return value

An anonymous function with parameter input and no return value

Anonymous function with parameter input and return value

4. There are two forms of syntax rules for anonymous functions: [one has input parameters, the other has no input parameters]

(parameter name: parameter type) = > function body

Function body

Package com.xej.learning.nimingFunctionobject AnonymousFunction {def main (args: Array [String]): Unit = {/ / for anonymous functions with parameter input, you should also use parameter input oh kafkaName ("apache kafka") println ("-->:\ t" + flumeName ("apache flume")) sparkName / / anonymous function usage scenario-- 1, as a parameter, pass it to the higher-order function / / below In map,foreach It is the anonymous function Array (3 Int 2). Map {(x: println) = > x + 2} .foreach (x = > print (x + ")) Array ("\ nMutual-") Map {_ + 1} .foreach (x = > print (x + "))} / / usage scenarios of anonymous functions-- 2 Directly assign value to variable / / parameter: parameter input, function body: no return value, anonymous function / / function body, there may be many statements in the form of anonymous function / / function body, oh, / only the final result, is val kafkaName = (name: String) = > println ("--kafka-- >" + name) / / Parameter: parameter input, function body: return value Anonymous function val flumeName = (name: String) = > "--flume-- >" + name / / Parameter: no parameter input, function body: no return value, anonymous function val sparkName = println ("- this is spark-")}

Anonymous functions are widely used in spark source code and marathon source code.

The following is an example of the marathon source code

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report