anonymous-function

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

风流意气都作罢 提交于 2019-11-28 10:11:03
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 worked 2th 3th are right transformation into what I want. ... foreach (x$1 => println(x$1:Int)) Why

C#: Anonymous method vs Named method

社会主义新天地 提交于 2019-11-28 10:09:55
I'm new to SO and programming and learning day by day with bits and pieces of tech (C#) jargons. After Googling for a while, below is what I've researched about methods A Method is a block of statements, which serves for code reusability & it also supports overloading with different SIGNATURE....for ex: drawShape(2pts), drawShape(3pts) etc... An Anonymous method is one with block of statements, but no name....(as its premature to ask, in wt situation we come across anonymous method...any articles, samples ...) Named method : Here's a link but at the end i didn't get what Named Method actually

In Scala, can you make an anonymous function have a default argument?

有些话、适合烂在心里 提交于 2019-11-28 09:23:24
This works: scala> def test(name: String = "joe"): Boolean = true test: (name: String)Boolean I expected this to work in the same way: scala> val test: String => Boolean = { (name: String = "joe") => true } <console>:1: error: ')' expected but '=' found. The boring, correct answer is no, you can't, but actually you kind of can, with the experimental single abstract method (SAM) synthesis in 2.11. First you need to define your own SAM type with the default value for the apply method's parameter: trait Func { def apply(name: String = "joe"): Boolean } Now you can use the function literal

Moq a function with anonymous type

ぃ、小莉子 提交于 2019-11-28 08:36:57
问题 I'm trying to mock this method Task<TResult> GetResultAsync<TResult>(Func<string, TResult> transformFunc) like this iMock.Setup(m => m.GetResultAsync(It.IsAny<Func<string, object>>())).ReturnsAsync(new { isPair = false }); The method to test doing the call passing an anonymous type to the generic parameter like this instance.GetResultAsync(u => new {isPair = u == "something" }) //dont look at the function return because as generic could have diferent implementations in many case Moq never

Scala return statements in anonymous functions

橙三吉。 提交于 2019-11-28 08:05:01
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, the return statement "survives" after m has finished executing, and the program results in a run-time

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

时光毁灭记忆、已成空白 提交于 2019-11-28 07:12:58
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 () { "use strict"; /** or here */ var stlib = function (param, param, param) { /** or here */ var share =

Callback function using variables calculated outside of it

别来无恙 提交于 2019-11-28 06:43:27
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? 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; }; For more information, see the manual page on anonymous functions . use global variables i.e $GLOBAL[

setTimeOut() or setInterval() . 4 methods to apply same thing. which is best?

老子叫甜甜 提交于 2019-11-28 04:27:20
问题 I am displaying a countdown watch with respect to a given endtime. although its working perfect but i want to know which is best methods to apply. below is my countdown function. var timerId; var postData = {endDate : endDate, tz : tz}; var countdown = function() { $.ajax({ type : 'post', async : false, timeout : 1000, url : './ajax_countdown.php', data : $.param(postData), dataType : 'json', success : function (resp){ $('#currentTime').html(resp.remainingTime); } }); } what i want is that

Closure vs Anonymous function (difference?) [duplicate]

佐手、 提交于 2019-11-28 04:03:27
Possible Duplicates: What is Closures/Lambda in PHP or Javascript in layman terms? What is the difference between a 'closure' and a 'lambda'? Hi, I have been unable to find a definition that clearly explains the differences between a closure and an anonymous function. Most references I have seen clearly specify that they are distinct "things" yet I can't seem to get my head around why. Could someone please simplify it for me? What are the specific differences between these two language features? Which one is more appropriate in what scenarios? An anonymous function is just a function that has

Choose Python function to call based on a regex

天涯浪子 提交于 2019-11-28 03:32:52
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_warning( msg ): global num_warnings, num_fatals num_warnings += 1 if ( is_fatal( msg ) ): num_fatals += 1