java 函数式编程 示例_R编程语言中的函数(带有示例)

java 函数式编程 示例

R语言功能 (R language functions)

As in the other programming languages like C, C++, Java, Python, etc., we find the usage of the Functions in the R language too. But what exactly do these functions mean? How do they help the programmers in the process of developing the best code for a given problem?

与其他编程语言(如CC ++JavaPython等)一样,我们也发现R语言中Function的用法。 但是这些功能到底是什么意思? 在给定问题的最佳代码开发过程中,它们如何帮助程序员?

Going forward let us define what exactly the word function means?

展望未来,让我们定义一下功能一词到底意味着什么?

In general, the functions are nothing but a set of statements that are well organized concurrently to perform a specific task. One must appreciate the beauty of the R language. This is because the R language provides a comfortable surrounding for the users to work on any kind of problem. For this purpose, several functions are inbuilt in the R language. Thus, the programmer will get a chance to expose himself or herself to the wide platform of the R language where one can witness the usage of numerous functions.

通常,这些功能不过是一组语句,这些语句可以很好地并行组织以执行特定任务 。 人们必须欣赏R语言的美。 这是因为R语言为用户提供了一个解决任何问题的舒适环境。 为此,R语言内置了几个功能。 因此,程序员将有机会将自己暴露于R语言的广阔平台,在此平台上,人们可以见证大量功能的使用。

Traditionally, the functions that are widely used in the R language are considered to be objects. Having such an assumption it becomes easier for the R interpreter to pass the control in the program. Also, the interpreter enables the users with the option of passing the arguments when the control is transferred to other blocks of code in the entire program. Since the option of passing the arguments makes the programmer very convenient while extending the basic program to a big project.

传统上,R语言中广泛使用的功能被视为对象。 有了这样的假设,R解释器将更容易在程序中传递控件。 同样,当控制权转移到整个程序中的其他代码块时,解释器使用户可以选择传递参数。 由于传递参数的选项使程序员在将基本程序扩展到大型项目时非常方便。

On the other hand, when the functions are used in the program when that particular statement gets executed then the transfer of control takes place. But eventually, the functions take the responsibility of returning the control to the place from where it is transferred once after the specific action is done. Thus, the function retrievals control to the interpreter once the main task is accomplished. Thereby the final result will be stored with the help of objects.

另一方面,当执行该特定语句时在程序中使用功能时,就会发生控制权的转移。 但是最终,这些功能负责在完成特定操作后将控件返回到从其转移的位置。 因此,一旦完成主要任务, 功能便将控制权返回给解释器。 因此,最终结果将在对象的帮助下存储。

函数声明/定义的语法 (Syntax of function declaration/definition)

A function in the R language is normally defined with the help of a keyword called function.

R语言中的函数通常是通过称为function的关键字来定义的 。

The general syntax followed for creating a function is as follows:

创建函数遵循的常规语法如下:

    function_name 

Types of Function Components

When a function is created then the user needs to take a look over the different parameters that eventually give rise to the well-structured function in a program. The following are the prime parameters that need to exemplify while working with the creation of a function in the program concerning any programming language.

They are:

  1. Function Name

  2. Arguments

  3. Function Body

  4. Return Value

a. Function name

It is the actual name given to a function while declaring it. It is mostly stored in the environment, which is available in the R language in the form of an object under the name given to it

b. Arguments

Arguments are defined as placeholders in simple terms. In general, according to the requirements of the programmer, the arguments are passed before the function is invoked. Of course, these arguments are optional too. On the other hand, these arguments can also possess the default values when executed.

c. Function Body

The function body is the phase of a program where there are several statements that are needed to be executed when the function is called.

d. Return Value

The return value is the last stage of any function. In this particular phase, this statement tells about the value that is being returned to the place where the function is called.

In the R language, there are numerous built-in functions. Thus, these can be directly used without defining at the beginning of the program. On the other hand, there is another sub-branch under these functions, i.e., User-defined functions. These types of functions are commonly created, declared and thus finally used according to the requirements of the user.

i) Built-In Functions

Most commonly used functions and of course, the built-in functions in the R language are as follows:

  • seq()

    seq()

  • mean()

    意思()

  • max()

    max()

  • sum(x)

    总和(x)

  • paste()

    糊()

The above functions are directly called by the programmer in the program.

以上功能由程序员在程序中直接调用。

Here is one of the simplest codes that demonstrates the usage of some of the above-stated functions.

这是最简单的代码之一,它演示了上述某些功能的用法。

# Create a sequence of numbers from 25 to 30.
print(seq(25,30))

# Find the mean of numbers from 15 to 21.
print(mean(15:21))

# Find the sum of numbers from1 to 10.
print(sum(1:10))

Output

输出量

[1] 25 26 27 28 29 30
[1] 18
[1] 55

ii)用户定义功能 (ii) User Defined Function)

Even the R language supports the creation of user-defined functions. The user-defined functions demonstrate the actual need of the programmer. Thus, as per the specific need, the users can go for these user-defined functions. Once the user-defined functions are developed then from the next second they can be used as the built-in functions when the programs are written.

甚至R语言也支持创建用户定义的函数。 用户定义的函数演示了程序员的实际需求。 因此,根据特定需要,用户可以使用这些用户定义的功能。 一旦开发了用户定义的函数,则从下一秒开始,它们就可以在编写程序时用作内置函数。

# Create a function to print squares of numbers in sequence.
new.function <- function(c) {
   for(i in 1:c) {
      b <- i^2
      print(b)
   }
}

通话功能 (Calling Function)

Below is the code that demonstrates the concept of calling a function in the R language:

下面的代码演示了用R语言调用函数的概念:

# Create a function to print squares of numbers in sequence.
new.function <- function(d) {
   for(i in 1:d) {
      b <- i^2
      print(b)
   }
}

# Call the function new.function supplying 10 as an argument.
new.function(10)

Output

输出量

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
[1] 49
[1] 64
[1] 81
[1] 100

在不带参数的情况下调用函数 (Calling a Function without an Argument)

Consider the following code,

考虑以下代码,

# Create a function without an argument.
new.function <- function() {
   for(i in 1:6) {
      print(i^2)
   }
}  

# Call the function without supplying an argument.
new.function()

Output

输出量

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36

调用带有参数值的函数(按位置和名称) (Calling a Function with Argument Values (By Position and By Name))

The arguments that are mentioned during the function call are supplied to the function in the same sequence as that declared at the time when it is called.

函数调用期间提到的参数以与调用时声明的顺序相同的顺序提供给函数。

# Create a function with arguments.
new.function <- function(a,b,c) {
   result <- a * b + c
   print(result)
}

# Call the function by position of arguments.
new.function(2,1,4)

# Call the function by names of the arguments.
new.function(a = 9, b = 1, c = 3)

Output

输出量

[1] 6
[1] 12

使用默认参数调用函数 (Calling a Function with Default Arguments)

Also, we have a provision where the user can enter the values of the parameters that are passed during the function definition. Besides that, a function can also be called without passing any arguments in it. Thus, such types of functions fall under the category of calling a function with the default argument.

另外,我们有一项规定,用户可以输入在函数定义期间传递的参数值。 除此之外,还可以在不传递任何参数的情况下调用函数。 因此,此类函数属于使用默认参数调用函数的类别。

# Create a function with arguments.
new.function <- function(a = 2, b = 4) {
   result <- a * b
   print(result)
}

# Call the function without giving any argument.
new.function()

# Call the function by giving new values of the argument.
new.function(2,5)

Output

输出量

[1] 8
[1] 10

On the other hand, there is another type of evaluation for any function. The arguments that are passed in functions mostly undergo a lazy evaluation. This word may surprise you, people, right? In simple terms, it can be explained as commonly the arguments in the function body get evaluated only in case when they are needed to be. The below code will help you in understanding the concept of lazy evaluation of the function.

另一方面,对任何功能都有另一种评估。 在函数中传递的参数大多经过惰性计算。 这个词可能会让您惊讶,对吧? 简单来说,可以将其解释为通常仅在需要时才对函数主体中的参数进行求值的情况。 以下代码将帮助您理解该函数的延迟求值的概念。

# Create a function with arguments.
new.function <- function(a, b) {
   print(a^2)
   print(a)
   print(b)
}

# Evaluate the function without supplying one of the arguments.
new.function(5)

Output

输出量

Error(s), warning(s):
Error in print(b) : argument "b" is missing, with no default
Calls: new.function -> print
Execution halted

[1] 25
[1] 5

Thus, the above is the description of the functions and their usage in the R language.

因此,以上是R语言中的功能及其用法的描述。

翻译自: https://www.includehelp.com/r/functions-in-r-programming-language-with-examples.aspx

java 函数式编程 示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值