anonymous-function

Implementing anonymous functions in Fortran

泄露秘密 提交于 2019-11-27 03:44:29
问题 This question is successor of my previous question Implementing minimization method. In current question, I simplified my problem and here is the sample MATLAB code. I want to implement it in Fortran. %Script script1.m clear vars; close all; clc; fun1 = @(x1,x2) 3*x1^2 + 4*x2^2 + 5*x1 + 6*x2 + 10; lower = -2; upper = 0; fun5 = fun15(fun1); %fun5 is 'intermediate' function %calling minimization function [location,value]=minimize1(fun5,lower,upper) In the script1.m, I created a function handle

What are the rules to govern underscore to define anonymous function?

霸气de小男生 提交于 2019-11-27 03:27:38
问题 I am using _ as placeholder for creating anonymous function, and the problem is I cannot predict how Scala is going to transform my code. More precisely, it mistakenly determines how "large" the anonymous function I want. List(1,2,3) foreach println(_:Int) //error ! List(1,2,3) foreach (println(_:Int)) //work List(1,2,3) foreach(println(_:Int)) //work Using -Xprint:typer I can see Scala transforms the first one into "a big anonymous function": x$1 => List(1,2,3) foreach(println(x$1:Int)) the

Scala return statements in anonymous functions

こ雲淡風輕ζ 提交于 2019-11-27 02:04:09
问题 Why does an explicit return statement (one that uses the return keyword) in an anonymous function return from the enclosing named function, and not just from the anonymous function itself? E.g. the following program results in a type error: def foo: String = { ((x: Integer) => return x) "foo" } I know it's recommended to avoid the return keyword, but I'm interested in why the explicit and implicit return statements have a different semantics in anonymous functions. In the following example,

this value in JavaScript anonymous function

懵懂的女人 提交于 2019-11-27 01:49:20
Can anybody explain to me why A is true and B is false? I would have expected B to be true as well. function MyObject() { }; MyObject.prototype.test = function () { console.log("A", this instanceof MyObject); (function () { console.log("B", this instanceof MyObject); }()); } new MyObject().test(); this is special. It refers to the object that the function is being called on behalf of (most commonly via dot syntax). So, in the case of A , the function is being called on behalf of a new MyObject object. B is in a different function that isn't explicitly being called on behalf of any object, so

How to document anonymous functions (closure) with jsdoc-toolkit

你说的曾经没有我的故事 提交于 2019-11-27 01:46:19
问题 I am trying to document my code using JSDoc-toolkit. My code starts by being wrapped with a self-executing anonymous function. How in the world do I document this? I've spent nearly all day on this. JS Docs will not recognize anything inside of the anonymous function closure due to it not knowing what to do with it. It breaks and none of my comments come through. My code looks something like this. /** * @fileoverview BLA BLA BLA */ /** * This is where I don't know what to put. */ (function ()

Callback function using variables calculated outside of it

China☆狼群 提交于 2019-11-27 01:27:24
问题 Basically I'd like to do something like this: $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); Is this actually possible? Calculating a variable outside of the anonymous function and using it inside? 回答1: You can use the use keyword to inherit variables from the parent scope. In your example, you could do the following: $callback = function($val) use ($avg) { return $val < $avg

Equivalent of C# anonymous methods in Java?

家住魔仙堡 提交于 2019-11-27 01:19:21
In C# you can define delegates anonymously (even though they are nothing more than syntactic sugar). For example, I can do this: 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(); }); Is it possible to pass code like this in Java? I'm using the processing framework, which has a quite old version of Java (it doesn't have generics). Pre Java 8: The closest Java has to delegates are single method interfaces. You could use an

How to pass two anonymous functions as arguments in CoffeScript?

99封情书 提交于 2019-11-27 01:00:26
I want to pass two anonymous functions as arguments for jQuery's hover, like so: $('element').hover( function() { // do stuff on mouseover }, function() { // do stuff on mouseout } ); It's easy with just one – hover -> – but what is the proper syntax in CoffeeScript for two? I tried ...hover -> , ...hover( ->... , etc. but nothing gets me the above structure. Put parentheses around the anonymous functions. I think the problem lies with using single line comments // . Single-line comments enclosed in /* .. */ seem to work fine. Here's an equivalent example with something other than a comment. $

Choose Python function to call based on a regex

大兔子大兔子 提交于 2019-11-27 00:06:39
问题 Is it possible to put a function in a data structure, without first giving it a name with def ? # This is the behaviour I want. Prints "hi". def myprint(msg): print msg f_list = [ myprint ] f_list[0]('hi') # The word "myprint" is never used again. Why litter the namespace with it? The body of a lambda function is severely limited, so I can't use them. Edit: For reference, this is more like the real-life code where I encountered the problem. def handle_message( msg ): print msg def handle

Use keyword in functions - PHP [duplicate]

萝らか妹 提交于 2019-11-27 00:00:16
问题 Possible Duplicate: In Php 5.3.0 what is the Function “Use” Identifier ? Should a sane programmer use it? I've been examining the Closures in PHP and this is what took my attention: public function getTotal($tax) { $total = 0.00; $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk($this->products, $callback); return round($total, 2); } And