anonymous-function

Logical short-circuit inside a function handle

十年热恋 提交于 2019-11-30 09:14:59
问题 I have a function handle that operates on 2d arrays of arbitrary size: R2T = @(DL1,DL2) arrayfun(@(DL1,DL2)... 1/(fzero(@(x)fFitObj1(x)./fFitObj2(x)-... DL1./DL2,[minLim maxLim])) ... ,DL1,DL2) - C1; Here's a bottom-up breakdown of what it does: fzero(@(x)fFitObj1(x)./fFitObj2(x)-DL1./DL2,[minLim maxLim]) - This bit looks for a zero of the considered function on the interval [minLim maxLim] , where fFitObj1 and fFitObj2 are function handles available from before, C1 is some known constant and

Use variables inside an anonymous function, which is defined somewhere else

被刻印的时光 ゝ 提交于 2019-11-30 08:14:35
问题 When using anonymous functions in PHP, you can easily use variables from right outside of its scope by using the use() keyword. In my case the anonymous functions are already defined somewhere, but called later on (somewhere else) in a class. The following piece of code is to illustrate the idea: <?php $bla = function ( $var1 ) use ($arg) { echo $var1; }; class MyClass { private $func; public function __construct ( $func ) { $this->func = $func; } public function test ( $arg ) { $closure =

Wanted: Matlab example of an anonymous function returning more than 1 output

一世执手 提交于 2019-11-30 06:31:03
问题 I use anonymous functions for simple data value transforms. The anonymous functions are defined with the following syntax sqr = @(x) x.^2; I would like to have a simple anonymous function that returns more than one output that can be used as follows . . . [b,a] = myAnonymousFunc(x); The Matlab documentation suggests that this is possible, but it does not give an example of the syntax needed to define such a function. http://www.mathworks.co.uk/help/techdoc/matlab_prog/f4-70115.html#f4-71162

Performance penalty using anonymous function in Julia

ぃ、小莉子 提交于 2019-11-30 05:47:41
问题 I have noticed that there is a performance penalty associated with using anonymous functions in Julia. To illustrate I have two implementations of quicksort (taken from the micro performance benchmarks in the Julia distribution). The first sorts in ascending order function qsort!(a,lo,hi) i, j = lo, hi while i < hi pivot = a[(lo+hi)>>>1] while i <= j while a[i] < pivot; i += 1; end while pivot < a[j]; j -= 1; end if i <= j a[i], a[j] = a[j], a[i] i, j = i+1, j-1 end end if lo < j; qsort!(a,lo

Anonymous Scala function syntax

给你一囗甜甜゛ 提交于 2019-11-30 03:59:48
I'm learning more about Scala, and I'm having a little trouble understanding the example of anonymous functions in http://www.scala-lang.org/node/135 . I've copied the entire code block below: object CurryTest extends Application { def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)) xs.head :: filter(xs.tail, p) else filter(xs.tail, p) def modN(n: Int)(x: Int) = ((x % n) == 0) val nums = List(1, 2, 3, 4, 5, 6, 7, 8) println(filter(nums, modN(2))) println(filter(nums, modN(3))) } I'm confused with the application of the modN function def modN(n: Int

PHP closures and implicit global variable scope

喜夏-厌秋 提交于 2019-11-30 03:02:53
问题 Is there a way that one can implicitly declare top-level variables as global for use in closures? For example, if working with code such as this: $a = 0; //A TOP-LEVEL VARIABLE Alpha::create('myAlpha') ->bind(DataSingleton::getInstance() ->query('c') ) ->addBeta('myBeta', function($obj){ $obj->bind(DataSingleton::getInstance() ->query('d') ) ->addGamma('myGamma', function($obj){ $obj->bind(DataSingleton::getInstance() ->query('a') ) ->addDelta('myDelta', function($obj){ $obj->bind

How to use $this in closure in php

断了今生、忘了曾经 提交于 2019-11-30 02:46:09
问题 I have function like this: class Service { function delete_user($username) { ... $sessions = $this->config->sessions; $this->config->sessions = array_filter($sessions, function($session) use ($this){ return $this->get_username($session->token) != $username; }); } } but this don't work because you can't use $this inside use , is it possible to execute function which is member of class Service inside a callback? Or do I need to use for or foreach loop? 回答1: $this is always available in (non

Why use anonymous function? [duplicate]

你离开我真会死。 提交于 2019-11-30 00:19:40
Possible Duplicate: How do you use anonymous functions in PHP? Why should i use an anonymous function? I mean, what's the real deal using it? I just don't really get this. I mean, you use function to make the code more clean or to use it more than once. But Anonymous functions just don't do neither the first nor the second. I googled them and i couldn't find anyone asking the same problem. I would say that anonymous functions show their beauty when there is good library classes/functions that use them. They are not that sexy by themselves. In the world of .net there is technology called LINQ

How to call anonymous function in C#?

人盡茶涼 提交于 2019-11-29 23:08:19
I am interested if it's possible using C# to write a code analogous to this Javascript one: var v = (function() { return "some value"; })() The most I could achieve is: Func<string> vf = () => { return "some value"; }; var v = vf(); But I wanted something like this: // Gives error CS0149: Method name expected var v = (() => { return "some value"; })(); Are there some way to call the function leaving it anonymous? Yes, but C# is statically-typed, so you need to specify a delegate type. For example, using the constructor syntax: var v = new Func<string>(() => { return "some value"; })(); //

php is_function() to determine if a variable is a function

泪湿孤枕 提交于 2019-11-29 21:59:57
I was pretty excited to read about anonymous functions in php, which let you declare a variable that is function easier than you could do with create_function . Now I am wondering if I have a function that is passed a variable, how can I check it to determine if it is a function? There is no is_function() function yet, and when I do a var_dump of a variable that is a function:: $func = function(){ echo 'asdf'; }; var_dump($func); I get this: object(Closure)#8 (0) { } Any thoughts on how to check if this is a function? Jon Benedicto Use is_callable to determine whether a given variable is a