标签: anonymous-function

为什么以及如何在PHP中使用匿名函数?

PHP 5.3提供匿名函数.
我应该使用它们还是避免使用它们?如果是这样,怎么样?

编辑 ; 刚刚用php匿名函数发现了一些不错的技巧......

$container           = new DependencyInjectionContainer();
$container->mail     = function($container) {};
$conteiner->db       = function($container) {};
$container->memcache = function($container) {};
Run Code Online (Sandbox Code Playgroud)

php anonymous-function

44
推荐指数
3
解决办法
4万
查看次数

回调函数使用在其外部计算的变量

基本上我想做这样的事情:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$avg = array_sum($arr) / count($arr);
$callback = function($val){ return $val < $avg };

return array_filter($arr, $callback);
Run Code Online (Sandbox Code Playgroud)

这有可能吗?计算匿名函数之外的变量并在里面使用它?

php anonymous-function

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

如何在MATLAB匿名函数中执行多个语句?

我想做这样的事情:

>> foo = @() functionCall1() functionCall2()
Run Code Online (Sandbox Code Playgroud)

所以当我说:

>> foo()
Run Code Online (Sandbox Code Playgroud)

它会执行functionCall1()然后执行functionCall2().(我觉得我需要类似C,运算符的东西)

编辑:

functionCall1并且functionCall2不一定有返回值的函数.

matlab command-line anonymous-function

43
推荐指数
4
解决办法
2万
查看次数

Scala中的匿名递归函数

有没有办法在Scala中编写一个递归的匿名函数?我在考虑这样的事情:

((t: Tree) => {
    print(t.value);
    for (c <- t.children)
        thisMethod(c)
})(root)
Run Code Online (Sandbox Code Playgroud)

(相关问题:哪些语言支持*递归*函数文字/匿名函数?)

recursion scala anonymous-function

43
推荐指数
4
解决办法
6688
查看次数

Python:以lambda形式传递语句

一个Python新手问题,为什么这个语法无效:lambda: pass,虽然这个:def f(): pass是正确的吗?

感谢您的见解.

python lambda anonymous-function

39
推荐指数
3
解决办法
1万
查看次数

36
推荐指数
4
解决办法
7790
查看次数

elixir中的递归和匿名函数

我正在尝试定义一个匿名函数来做一个点积,我可以将它编码为私有函数而没有任何问题,但我正在努力使用匿名函数语法.我知道我可以以不同的方式实现它,但我试图了解如何使用模式匹配和递归来定义匿名函数.这是我目前的实施

dot = fn
  [i|input],[w|weights], acc -> dot.(input,weights,i*w+acc)
  [],[bias],acc -> acc + bias
end
Run Code Online (Sandbox Code Playgroud)

我在编译时遇到这个错误:

function dot/0 undefined
Run Code Online (Sandbox Code Playgroud)

任何提示?这不可能吗?

recursion anonymous-function elixir

34
推荐指数
3
解决办法
5866
查看次数

如何使用jsdoc-toolkit记录匿名函数(闭包)

我正在尝试使用JSDoc-toolkit记录我的代码.我的代码首先包含一个自执行的匿名函数.我怎么在世界上记录这个?我几乎整天都在这上面.JS Docs不会识别匿名函数闭包内部的任何内容,因为它不知道如何处理它.它打破了,我的评论都没有通过.

我的代码看起来像这样.

/** 
 * @fileoverview BLA BLA BLA
 */

/**
 * This is where I don't know what to put.
 */
 (function () {
     "use strict";

     /** or here */
     var stlib = function (param, param, param) {
         /** or here */
         var share = {
             /** or here */
             config: {
                 button: DOM Element,
                 property: blablabla
             },

             init: function () { ...some init code here}
         };

         share.init();
     };

     widgets.add("share", stlib);
 }());
Run Code Online (Sandbox Code Playgroud)

谢谢!

javascript closures comments anonymous-function jsdoc

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

在新线程上运行简单函数的最佳方法?

我有两个函数,我想在不同的线程上运行(因为它们是数据库的东西,它们不是立即需要的).

功能是:

            getTenantReciept_UnitTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciept_Unit);
            getTenantReciept_TenantNameTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciept_TenantName);
Run Code Online (Sandbox Code Playgroud)

在javascript中,我知道我可以创建一个匿名函数并在一个新线程上使用这样的东西很容易地调用它:

setTimeout(new function(){doSomethingImportantInBackground();}, 500);
Run Code Online (Sandbox Code Playgroud)

在C#中有这样的东西吗?

.net c# multithreading anonymous-function

32
推荐指数
4
解决办法
9万
查看次数

Java中C#匿名方法的等价物?

在C#中,您可以匿名定义委托(即使它们只不过是语法糖).例如,我可以这样做:

public string DoSomething(Func<string, string> someDelegate)
{
     // Do something involving someDelegate(string s)
} 

DoSomething(delegate(string s){ return s += "asd"; });
DoSomething(delegate(string s){ return s.Reverse(); });
Run Code Online (Sandbox Code Playgroud)

是否可以在Java中传递这样的代码?我正在使用处理框架,它有一个相当旧版本的Java(它没有泛型).

c# java processing delegates anonymous-function

30
推荐指数
2
解决办法
2万
查看次数