anonymous-function

Is it possible to specify an anonymous function's return type, in Scala?

北战南征 提交于 2019-12-02 17:34:50
I know you can create an anonymous function, and have the compiler infer its return type: val x = () => { System.currentTimeMillis } Just for static typing's sake, is it possible to specify its return type as well? I think it would make things a lot clearer. In my opinion if you're trying to make things more clear it is better to document the expectation on the identifier x by adding a type annotation there rather than the result of the function. val x: () => Long = () => System.currentTimeMillis Then the compiler will ensure that the function on the right hand side meets that expectation. val

Possible to use COUNTIF with a function on the range's values?

半腔热情 提交于 2019-12-02 16:26:31
问题 Let's say that I have a list of dates starting from A1 and going across... 1/3/2014 2/5/2014 5/5/2015 8/10/2016 ... I'd like to count the number of times a certain month appears in this range. My current solution is that the row below it just contains =MONTH(x1) , where x is the column, and then I call a COUNTIF on that row. I don't think that's so bad of a solution, but it does require a whole bunch of extra cells just to calculate months in my spreadsheet, which isn't really necessary for

matlab constant anonymous function returns only one value instead of an array

拥有回忆 提交于 2019-12-02 13:26:12
I've been searching the net for a couple of mornings and found nothing, hope you can help. I have an anonymous function like this f = @(x,y) [sin(2*pi*x).*cos(2*pi*y), cos(2*pi*x).*sin(2*pi*y)]; that needs to be evaluated on an array of points, something like x = 0:0.1:1; y = 0:0.1:1; w = f(x',y'); Now, in the above example everything works fine, the result w is a 11x2 matrix with in each row the correct value f(x(i), y(i)). The problem comes when I change my function to have constant values: f = @(x,y) [0, 1]; Now, even with array inputs like before, I only get out a 1x2 array like w = [0,1];

Making anonymous functions from PHP 5.3 work with PHP 5.2

余生颓废 提交于 2019-12-02 12:07:59
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 is now missing. Any ideas on how to resolve this? function clean_text($input) { function upper_case(

how to add an extra argument to a block

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 11:49:59
Mailcore has a cool method that downloads an attachment and accepts a block as a parameter to return download progress: - (CTCoreAttachment *)fetchFullAttachmentWithProgress:(CTProgressBlock)block; where CTProgressBlock is defined as typedef void (^CTProgressBlock)(size_t curr, size_t max); so typically I would use it like so: //AttachmentDownloader.m int fileNum = x; // explained later CTCoreAttachment* fullAttachment = [attachment fetchFullAttachmentWithProgress:^(size_t curr, size_t max) { NSLog(@"::: this is curr: %zu", curr); NSLog(@"::: this is max: %zu\n\n", max); }]; the problem is

Render a variable during creation of anonymous PHP function

南笙酒味 提交于 2019-12-02 08:04:21
问题 I'm trying to get a simple sort function going using anonymous functions. One each for asc and desc sorting. Is it possible to render the $sortBy variable right away when the function is created, but still have $x and $y passed in when called later? I want to be able to dynamically pass in a key when creating these. $sortBy = 'some_key'; // descending $sort['desc'] = function($x, $y) { if($x['data'][$sortBy] == $y['data'][$sortBy]) return 0; return ($x['data'][$sortBy] > $y['data'][$sortBy])

Render a variable during creation of anonymous PHP function

有些话、适合烂在心里 提交于 2019-12-02 05:17:49
I'm trying to get a simple sort function going using anonymous functions. One each for asc and desc sorting. Is it possible to render the $sortBy variable right away when the function is created, but still have $x and $y passed in when called later? I want to be able to dynamically pass in a key when creating these. $sortBy = 'some_key'; // descending $sort['desc'] = function($x, $y) { if($x['data'][$sortBy] == $y['data'][$sortBy]) return 0; return ($x['data'][$sortBy] > $y['data'][$sortBy]) ? -1 : 1; }; uasort($arrayToSort, $sort[$order]); EDIT: I'm passing this array as a param to uasort().

How can I call a function multiple times?

依然范特西╮ 提交于 2019-12-02 05:06:16
问题 My issue is: I'm developing a mobile-friendly website with 2 stylesheets, one for the "PC-oriented" visualization and the other for the mobile visualization. Along with the 2 CSS, I need a function to modify some href attributes on the menu when switching from one visualization mode to the other. I'm not going deep into the details of the stuff as it would be very long and probably boring, anyway I need the function to be called both on $(document).ready AND on $(window).resize , that is

JavaScript - referencing 'this' in an inner function

霸气de小男生 提交于 2019-12-02 03:58:23
Consider the following code: MyClass.prototype.my_func = function () { this.x = 10; $.ajax({ // ... success: function (data) { alert(this.x); } }); } It doesn't work, since apparently this is not bound into the closure's execution context. I've been able to work it around by introducing another variable: var _this = this; And this works inside the anonymous function. But looks quite ugly to me. Is there some nice way to handle this? This may look like the ugly solution for you and there are some walkarounds (such as using bind() method to change the context), but this is the best solution I

Anonymous partial function in early initializer requires “premature access to class”

妖精的绣舞 提交于 2019-12-02 03:57:54
问题 Why does this fail to compile: trait Item trait StringItem extends Item { def makeString: String } trait SomeOtherItem extends Item trait DummyTrait case class Marquee(items: Seq[Item]) extends { val strings: Seq[String] = items.collect { case si: StringItem => si.makeString // <-- partial function inside braces } } with DummyTrait with the error message <$anon: Item => String> requires premature access to class Marquee ? It seems to me that the partial function makes no use of Marquee . Yet