anonymous-function

When does Scala need parameter types for anonymous and expanded functions?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 11:42:59
问题 When does the Scala compiler really need the type information of parameters of anonymous functions? For instance, given this function: def callOn[T,R](target: T, f: (T => R)) = f(target) then I cannot use it like this: callOn(4, _.toString) => error: missing parameter type for expanded function ((x$1) => x$1.toString) and I have to specify callOn(4, (_: Int).toString) which is rather ugly. Why doesn't my example work, whereas method like map, filter, foldLeft, etc. on the collection classes

Anonymous functions in WordPress hooks

删除回忆录丶 提交于 2019-12-04 10:16:54
问题 WordPress hooks can be used in two ways: using callback function name and appropriate function add_action( 'action_name', 'callback_function_name' ); function callback_function_name() { // do something } using anonymous function (closure) add_action( 'action_name', function() { // do something } ); Is there any difference for WordPress what way to use? What is prefered way and why? 回答1: The disadvantage of the anonymous function is that you're not able to remove the action with remove_action.

Calling anonymous functions defined as object variables in php [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-04 09:36:51
This question already has an answer here: Calling closure assigned to object property directly 11 answers I have php code like: class Foo { public $anonFunction; public function __construct() { $this->anonFunction = function() { echo "called"; } } } $foo = new Foo(); //First method $bar = $foo->anonFunction(); $bar(); //Second method call_user_func($foo->anonFunction); //Third method that doesn't work $foo->anonFunction(); Is there a way in php that I can use the third method to call anonymous functions defined as class properties? thanks Gordon Not directly. $foo->anonFunction(); does not

How do you declare a Func with an anonymous return type?

[亡魂溺海] 提交于 2019-12-04 08:53:11
问题 I need to be able to do this: var getHed = () => { // do stuff return new { Property1 = value, Property2 = value2, etc...}; }; var anonymousClass = getHed(); But I get an error which indicates I need to explicitly declare getHed. How do I declare Func such that T is the anonymous type I am returning? In case you are curious why I need to do this, it is because I am using 3rd party software that allows customization code, but only within a single method. This can become very difficult to

What is the context of an anonymous function?

点点圈 提交于 2019-12-04 08:27:59
I have code like this: function demo() { this.val=5; function() { this.val=7; }(); } Now when I give execute this code in the firefox or chrome console it gives a syntax error. I don't understand why this is an error because I have read that javascript functions are objects so when I call the anonymous function, inside it this points to function demo and should change the val to 7 , so if I do var x=new demo(); x.val; //should give 7 but when I do this function demo() { this.val=5; var f=function() { this.val=7; }(); } window.val; // gives 7 I don't understand if functions are objects then why

Why are anonymous function expressions and named function expressions initialized so differently?

◇◆丶佛笑我妖孽 提交于 2019-12-04 08:26:54
I'm looking at section 13 or the ECMAScript specification (v. 5). An anonymous function expression is initialized as follows: Return the result of creating a new Function object as specified in 13.2 with parameters specified by FormalParameterListopt and body specified by FunctionBody. Pass in the LexicalEnvironment of the running execution context as the Scope. Pass in true as the Strict flag if the FunctionExpression is contained in strict code or if its FunctionBody is strict code. this logic is very similar to how a function declaration is initialized. However, notice how different

Recursion and anonymous functions in elixir

回眸只為那壹抹淺笑 提交于 2019-12-04 08:15:20
问题 I'm trying to define an anonymous function to do a dot product, I can code this as a private function without any problem but I am struggling with the anonymous function syntax. I know I could implement this differently but I am trying to understand how to define anonymous functions with pattern matching and recursion. This is my current implementation dot = fn [i|input],[w|weights], acc -> dot.(input,weights,i*w+acc) [],[bias],acc -> acc + bias end And I get this error on compile: function

Expression tree depth limitations

给你一囗甜甜゛ 提交于 2019-12-04 07:41:13
I'm facing a problem trying to call Compile() on the LambdaExpression of type Expression<Func<MyType, bool>> which has a depth around 400. And lesser values do not cause any problems. And I can't find anything about such kind of limitation. Can anyone clarify this? Can I increase this limit? upd: Sorry, forgot to mention, I'm getting StackOverflowException: An unhandled exception of type 'System.StackOverflowException' occurred in System.Core.dll {Cannot evaluate expression because the current thread is in a stack overflow state.} You are legitimately running into a limit on the stack size

PHP - Difference between 'use()' or 'global' to access a global variable in a closure?

99封情书 提交于 2019-12-04 07:37:06
Is there any kind of performance or other difference between following two cases of accessing a global variable in a closure: Case 1: $closure = function() use ($global_variable) { // Use $global_variable to do something. } Case 2: $closure = function() { global $global_variable; // Use $global_variable to do something. } There is an important difference between your two examples: $global_variable = 1; $closure = function() use ($global_variable) { return $global_variable; }; $closure2 = function() { global $global_variable; return $global_variable; }; $global_variable = 99; echo $closure(); /

Making anonymous functions from PHP 5.3 work with PHP 5.2

扶醉桌前 提交于 2019-12-04 06:42:27
问题 I have an anonymous functions that I now need to update to be compatible with PHP 5.2. The function (below) takes text and uppercases the first letter of every sentence. function clean_text($input) { $output = $input; $output = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) { return strtoupper($matches[1] . ' ' . $matches[2]); }, ucfirst(strtolower($input))); return $output; } I tried pulling the function out, but I'm receiving an error stating that argument 2 in the callback