将集合作为函数使用 [英] Working with Sets as Functions

查看:26
本文介绍了将集合作为函数使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 FP 课程:

type Set = Int => Boolean  // Predicate

  /**
   * Indicates whether a set contains a given element.
   */
  def contains(s: Set, elem: Int): Boolean = s(elem)

为什么这样做有意义?

assert(contains(x => true, 100))

基本上它的作用是为函数 x => 提供值 100;真的.即,我们提供 100,它返回 true.

Basically what it does is provide the value 100 to the function x => true. I.e., we provide 100, it returns true.

但这与集合有什么关系?

But how is this related to sets?

无论我们输入什么,它都会返回 true.它的意义在哪里?

Whatever we put, it returns true. Where is the sense of it?

我知道我们可以提供我们自己的集合实现/函数作为一个参数来表示提供的值在集合内(或不在集合内)的事实 - 然后(仅)这个实现使 contains 功能由某种意义/意义/逻辑/功能填充.

I understand that we can provide our own set implementation/function as a parameter that would represent the fact that provided value is inside a set (or not) - then (only) this implementation makes the contains function be filled by some sense/meaning/logic/functionality.

但到目前为止它看起来像一个无意义的函数.它被命名为 contains 但名称并不代表逻辑.我们可以称它为 apply() 因为它的作用是将一个函数(第一个参数)应用于一个值(第二个参数).只有名称 contains 可以告诉读者作者可能想说什么.是不是太抽象了,也许吧?

But so far it looks like a nonsense function. It is named contains but the name does not represent the logic. We could call it apply() because what it does is to apply a function (the 1st argument) to a value (the 2nd argument). Having only the name contains may tell to a reader what an author might want to say. Isn't it too abstract, maybe?

推荐答案

在上面显示的代码片段中,任何集合 S 都由所谓的 特征函数,即,一个给定一些整数 i 的函数检查 i 是否包含在集合 S 中.因此你可以把这样一个特征函数 f 想象成一个集合,即

In the code snippet you show above, any set S is represented by what is called its characteristic function, i.e., a function that given some integer i checks whether i is contained in the set S or not. Thus you can think of such a characteristic function f like it was a set, namely

{i |f itrue}

如果你想到任何类型为 Int => 的函数;Boolean as set(由类型同义词Set = Int => Boolean 表示)然后你可以用

If you think of any function with type Int => Boolean as set (which is indicated by the type synonym Set = Int => Boolean) then you could describe contains by

给定一个集合 f 和一个整数 icontains(f, i) 检查 i 是否为f 与否的元素.

Given a set f and an integer i, contains(f, i) checks whether i is an element of f or not.

一些示例集可能会使这个想法更明显:

Some example sets might make the idea more obvious:

Set                                Characeristic Function
 empty set                          x => false
 universal set                      x => true
 set of odd numbers                 x => x % 2 == 1
 set of even numbers                x => x % 2 == 0
 set of numbeers smaller than 10    x => x < 10

例子:集合{1, 2, 3}可以表示为

Example: The set {1, 2, 3} can be represented by

val S: Set = (x => 0 <= x && x <= 3)

如果你想知道某个数字 n 是否在这个集合中,你可以这样做

If you want to know whether some number n is in this set you could do

contains(S, n)

但当然(正如您已经观察到的那样)您可以通过直接执行获得相同的结果

but of course (as you already observed yourself) you would get the same result by directly doing

S(n)

虽然较短,但前者可能更容易阅读(因为意图有些明显).

While this is shorter, the former is maybe easier to read (since the intention is somewhat obvious).

这篇关于将集合作为函数使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆