标签: first-class-functions

可以在Javascript中在运行时创建函数吗?

维基百科关于一等公民的文章指出,"一些作者"认为,如果语言支持在运行时创建,那么函数只是一种语言中的一等公民.James Coglan撰写的这篇文章明确地将职能视为一流的公民 - 无论他是否意识到有关一流标准的争议,我不知道.

这是我的问题:

  1. 使用"在运行时创建"的附加标准,JavaScript程序是一流的公民吗?

值得一提的是,基于更广义的标准(适用于大多数其他对象),JavaScript函数显然是一等公民,即它们可以作为变量传递; 因此,我觉得上面提到的标准增加了一个有趣的动态 - 或者至少是一个澄清的动态 - 给对话而不是 - 就像一个用户写的那样 - "任意".

  1. 如果是这样,在运行时创建一个函数在JavaScript中是什么样的(这就是我们称之为promises,回调,匿名等)?
  2. 如果没有,在另一种语言的运行时,函数的创建是什么样的?

javascript first-class-functions

3
推荐指数
2
解决办法
1055
查看次数

使用函数名称作为参数

在Go中,您可以将函数作为参数传递callFunction(fn func).例如:

package main

import "fmt"

func example() {
    fmt.Println("hello from example")
}

func callFunction(fn func) {
    fn()
}    

func main() {
    callFunction(example)
}
Run Code Online (Sandbox Code Playgroud)

但是当它是结构的成员时,是否可以调用函数?以下代码将失败,但为您提供我正在谈论的内容的示例:

package main

import "fmt"

type Example struct {
    x int
    y int
}

var example Example

func (e Example) StructFunction() {
    fmt.Println("hello from example")
}

func callFunction(fn func) {
    fn()
}    

func main() {
    callFunction(example.StructFunction)
}
Run Code Online (Sandbox Code Playgroud)

(我知道我在这个例子中想做的事情有点奇怪.我所遇到的确切问题并没有很好地缩小到一个简单的例子,但这是我问题的本质.但我也很感兴趣这从学术角度来看)

methods struct types go first-class-functions

2
推荐指数
1
解决办法
149
查看次数

如何在Lisp中的变量中存储函数并使用它

我想print在变量中存储一个函数,这样我就可以输入一些简短的函数p,例如:
In Scheme:

(define print display)
(print "Hello world\n")
;; alternate way
(define print 'display)
((eval print) "Hello world\n")
Run Code Online (Sandbox Code Playgroud)

同样的方法似乎不起作用Common Lisp:

(defvar p 'print)
;;(print (type-of p))
(p "Hello world") ;; Attempt 1
((eval p) "Hello world") ;; >> Attempt 2
((eval (environment) p) "Hello world") ;; Attempt 3
Run Code Online (Sandbox Code Playgroud)

Attempt 1上面得到这个错误:

*** - EVAL: undefined function P
Run Code Online (Sandbox Code Playgroud)

而这与Attempt 23Clisp:

*** - EVAL: (EVAL (ENVIRONMENT) P) …
Run Code Online (Sandbox Code Playgroud)

scheme eval common-lisp first-class-functions lisp-2

2
推荐指数
3
解决办法
2634
查看次数

Java 8以一流公民的身份跳舞吗?

因此,我中的函数程序员喜欢像python这样的语言,它们将函数视为一等公民。看起来Java 8承受了压力,而诸如lambda表达式和方法引用之类的“已实现的”东西则被淘汰了。

我的问题是,Java是否正在使用一流的函数,或者这真的只是语法糖,以减少实现匿名类/接口(如可运行的接口)所需的代码量?(我的肠子说后者)。

我理想的情况

Map<String,DoubleToDoubleFunc> mymap = new HashMap<String,DoubleToDoubleFunc>();
...
mymap.put("Func 1", (double a, double b) -> a + b);
mymap.put("Func 2", Math::pow);
...
w = mymap.get("Func 1")(y,z);
x = mymap.get("Func 2")(y,z);
Run Code Online (Sandbox Code Playgroud)

java first-class-functions java-8

1
推荐指数
1
解决办法
1829
查看次数

Ruby匿名类作为第一类函数

Ruby没有一流的功能; 虽然它有procs和lambdas,但这些众所周知地需要很大的开销.(Python有第一类函数,显然没有开销.)我发现第一类函数可以使用匿名类进行模拟,如下所示:

f = Class.new { def self.f; puts 'hi'; end }

def g(fun); fun; end

g(f.f)
# => "hi"
Run Code Online (Sandbox Code Playgroud)

有谁知道更好的方法?

ruby first-class-functions

1
推荐指数
1
解决办法
339
查看次数

如何将函数复制到新符号?

我对Common Lisp中的函数副本有疑问.

在Scheme中我会选择:

(define (foo par1 par2) (+ par1 par2))
(define bar foo)
(print (bar 1 2)) ;; --> prints 3
(define (foo par1 par2) (* par1 par2))
(print (bar 1 2)) ;; --> prints again 3
(print (foo 1 2)) ;; --> prints 2
Run Code Online (Sandbox Code Playgroud)

我怎么能用Common Lisp做到这一点?

lisp scheme common-lisp first-class-functions

1
推荐指数
1
解决办法
62
查看次数

用外部.ex文件编写的Elixir匿名函数无法与交互式shell一起运行

我可以直接在交互式外壳中编写一个匿名函数,如下所示。

iex> total_bottles_milk = fn total -> total * 2 end  
iex> total_bottles_milk.(2)
Run Code Online (Sandbox Code Playgroud)

但是,如果我编写一个外部文件并在交互式外壳程序中运行,它将显示“编译错误”。

我的文件名和目录路径是lib / expense.ex

下面是我的代码

defmodule Expense do

    total_bread_slices = fn total -> (total * 10) / 100 end
    total_bottles_milk = fn total -> total * 2 end
    total_cakes = fn total -> total * 15 end

    def total_expense(bread_slices, bottles_of_milk, cakes) do
        total_bread_slices.(bread_slices) + total_bottles_milk.(bottles_of_milk) + total_cakes.(cakes)
    end

end
Run Code Online (Sandbox Code Playgroud)

当我进入文件夹路径并运行iex -S mix以运行Expense模块时,终端显示Compilation错误。
我只是想知道我是否可以直接在交互式外壳程序中运行匿名函数,而不是从外部源进行编译。我想写我作为一等公民的职能。如果有办法,我该怎么办?

anonymous-function elixir first-class-functions

1
推荐指数
1
解决办法
144
查看次数

如何理解swift中的"一流功能"?

我正在阅读一些关于swift函数式编程的书籍.当我看到一些书说swift中的函数是"一流的函数"时我迷惑了"一流"的含义.任何人都可以回答我或给我一些示例代码吗?

first-class-functions swift

0
推荐指数
1
解决办法
553
查看次数

闭包和 funarg 问题之间的区别?

我有一个错误的理解,过滤器功能是向下的 funarg 问题的一个例子吗?我在源面板下使用 chrome 调试器,并在范围部分下注意到了这一点。

我的描述截图

过滤器函数参数cb是闭包还是strainer闭包函数?我发现很难在网上整理有关闭包和 funarg 问题的信息。我显然不明白 funarg 问题或闭包,需要一些帮助吗?

function strainer(collection, cb) {
  return collection.reduce(function inner(acc, curr) {
    if (cb(curr)) {
      return acc.concat(curr);
    }
    return acc;
  }, []);
}

function even(number) {
  if (number % 2 === 0) {
    return true;
  }
  return false;
}

var collection = [1, 2, 3, 4, 5];

strainer(collection, even);
Run Code Online (Sandbox Code Playgroud)

背景:我的印象是私有变量返回到外部环境创建了闭包,但该示例看起来有所不同。

下面的 flintstones 函数示例在引号函数的范围内有闭包。(我认为这是向上的 funarg 问题)

function strainer(collection, cb) {
  return collection.reduce(function inner(acc, curr) {
    if (cb(curr)) {
      return acc.concat(curr); …
Run Code Online (Sandbox Code Playgroud)

javascript closures first-class-functions

0
推荐指数
1
解决办法
370
查看次数