scala 部分应用函数_Scala中的部分函数

scala 部分应用函数

Writing functions and methods that process the input data and produce some output is the very core of any type of programming. When diving deeper into different types of functions in Scala, there is one type that differs from others — partial function. As the element of Scala’s standard library, it is appropriate to know what it is and where it should be used. Let’s dive in!

编写处理输入数据并产生一些输出的函数和方法是任何类型编程的核心。 当深入研究Scala的不同类型的功能时,有一种与其他类型不同的功能-部分功能。 作为Scala标准库的元素,了解它是什么以及应该在哪里使用是适当的。 让我们潜入吧!

什么是偏函数? (What is Partial Function?)

A partial function of type PartialFunction[A, B] is a unary function where the domain does not necessarily include all values of type A.

类型 PartialFunction [A,B] 的局部函数 是一元函数,其中域不一定包含类型 A的所有值

Scala Standard Library

Scala标准库

The definition above, taken straight from the Scala lang standard library docs may seem quite obfuscated. Fear not though, as the concept of partial function is very easy to understand.

上面的定义直接取自Scala lang标准库文档,可能看起来很模糊。 不用担心,因为部分功能的概念非常容易理解。

In essence, a partial function is a function that accepts only a specific set of data as the input. This doesn’t mean that the function takes only integers and not strings — this behaviour is a standard element of any function. The partial function that accepts integer parameter as the input, can specify what exact integers are accepted.

本质上,部分函数是仅接受一组特定数据作为输入的函数。 这并不意味着该函数仅接受整数而不是字符串-此行为是任何函数的标准元素。 接受整数参数作为输入的部分函数可以指定要接受的确切整数。

To better understand the concept, let’s see some code examples.

为了更好地理解这个概念,让我们看一些代码示例。

如何编写偏函数 (How to write Partial Function)

First, let’s write a standard function that accepts any input of type Int:

首先,让我们编写一个接受任何Int类型输入的标准函数:

val standardFunction = (x: Int) => x % 10

The standardFunction is like any other - takes a parameter and returns the remainder when dividing by 10. Now, what if a developer passes zero as the parameter? Or if we need even more refined restrictions regarding the parameter? One way would be to write a long block of ifs and elses, but that is not what you would want to do. Instead, let's write a partial function, like this:

standardFunction就像其他函数一样-接受参数并除以10后返回余数。 或者,如果我们需要对参数进行更精细的限制? 一种方法是编写很长的ifs和else,但这不是您想要的。 相反,让我们编写一个局部函数,如下所示:

val partialFunction: PartialFunction[Int, Int] = {
case 1 => 100
case 2 => 120
case 3 => 130
}

The partialFunction above is defined using the PartialFunction[Int, Int] trait. The type parameters say that both input and the output will be of type Int. As you can see, this function accepts only three parameters: either 1, 2 or 3. For any other parameter, an exception will be thrown.

上面的partialFunction是使用PartialFunction[Int, Int]特性定义的。 类型参数表示输入和输出均为Int类型。 如您所见,此函数仅接受三个参数:1、2或3。对于任何其他参数,将引发异常。

The construction of a partial function is quite easy, but the power comes with the different operators you can use with them. Let’s look at some of them:

局部函数的构造非常容易,但是功能强大的是您可以与它们一起使用的不同运算符。 让我们看看其中的一些:

partialFunction.isDefinedAt(67)

isDefinedAt() - this method allows to check if the partial function will accepts a certain parameter - in our case, 67 will not be accepted, and the method will return false.

isDefinedAt() -此方法允许检查部分函数是否将接受某个参数-在本例中,将不接受67,并且该方法将返回false

partialFunction.lift

lift - this method wraps the return type into an Option. For the our partialFunction, that will be Option[Int].

lift此方法将返回类型包装到Option 。 对于我们的partialFunction ,它将是Option[Int]

partialFunction orElse otherPartialFunction

orElse - this one is very neat - it allows to chain multiple partial function, if the first one doesn't accept the input, or in different words - it's isDefinedAt method returns false.

orElse这个非常整洁-如果第一个不接受输入,或者如果使用不同的单词,则它允许链接多个部分函数isDefinedAt方法返回false

There is plethora of other methods available — be sure to check out the official docs.

还有许多其他可用的方法-请务必查看官方文档

MapCollect (Map vs Collect)

One other interesting behaviour of partial functions can be observed when using it for mapping a collection. Let’s try to use it as if it was a standard function:

使用分函数映射集合时,可以观察到另一有趣的分函数行为。 让我们尝试将其当作标准函数来使用:

List(1, 2, 3, 4, 5).map(partialFunction)

The first three parameters are fine — the partialFunction have a defined output for them. But what happens with parameters 4 and 5. The answer is: MatchError. The map function is not able to determine the output and fails.

前三个参数很好partialFunction为它们提供了定义的输出。 但是参数4和5会发生什么。答案是: MatchErrormap功能无法确定输出并且失败。

The solution to this is to use the collect method. This method uses the isDefinedAt method before applying the mapping function, therefore avoiding the MatchError.

解决方案是使用collect方法。 此方法在应用映射函数之前使用isDefinedAt方法,因此避免了MatchError

List(1, 2, 3, 4, 5).collect(partialFunction)

摘要 (Summary)

I hope you have found this post useful. If so, don’t hesitate to like or share this post. Additionally, you can follow me on my social media if you fancy so 🙂

我希望您发现这篇文章有用。 如果是这样,请随时喜欢或分享此帖子。 此外,如果您愿意,可以在我的社交媒体上关注我me

翻译自: https://medium.com/swlh/partial-functions-in-scala-92ad29c0cf78

scala 部分应用函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值