【问题标题】:Examples of languages that hide variable multiplicity隐藏变量多重性的语言示例
【发布时间】:2018-06-01 01:12:00
【问题描述】:

有哪些编程语言、编程语言扩展或其他在操作变量时隐藏变量多样性、调用方法等解决方案的示例?

具体来说,我想象一个系统,其中我有一个单一类型的对象集合,它将透明地转发对象集合上的任何方法调用,以便将方法单独应用于所有对象,包括以有意义的方式使用返回值。最好我希望看到可以很好地做到这一点的语言示例,但也可以看看这不起作用的解决方案。

我想像这样:

struct Foo
{
    int bar();
}

void myFunction()
{
    // 4 Foo objects are created in a vector
    vector<Foo> vals(4); 
    // The bar() method is applied to each of the Foo objects and each 
    // return an int that is automatically inserted into a new vector
    vector<int> = vals.bar();
}

【问题讨论】:

    标签: programming-languages generic-programming type-systems multiplicity


    【解决方案1】:

    看看 Java 8 流。基本上,您将“流式传输”容器的内容,并向流指示经过的每一件事都应该应用 Foo::bar 方法。

    vals.stream().forEach(Foo::bar);
    

    其中很多概念来自早期语言,包括 Lisp(列表处理)。

    【讨论】:

      猜你喜欢
      • 2013-08-18
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多