anonymous-function

Converting code with Anonymous functions to PHP 5.2

耗尽温柔 提交于 2019-12-03 18:05:55
问题 I have some PHP 5.3 code which builds an array to be passed to a view. This is the code I have. # Select all this users links. $data = $this->link_model->select_user_id($this->user->id); if (count($data) > 0) { # Process the data into the table format. $table = array ( 'properties' => array ( 'delete_link_column' => 0, ), 'callbacks' => array ( # Callback for the name link. function($value) { return sprintf('<a href="/links/view/name/%s">%s</a>', $value, $value); }, # Callback for the

Javascript variable scope in addEventListener anonymous function

扶醉桌前 提交于 2019-12-03 16:44:41
When clicking on each div it should alert '1' if div 1 was clicked on or '5' if div 2 was clicked on. I have tried to make this code as easy to as possible because this is needed in a much larger application. <html> <head> <style type="text/css"> #div1 { background-color: #00ff00; margin: 10px; padding: 10px; } #div2 { background-color: #0000ff; margin: 10px; padding: 10px; } </style> <script type="text/javascript"> function init() { var total = 1; var div1 = document.getElementById('div1'), div2 = document.getElementById('div2'); var helper = function(event, id) { if (event.stopPropagation)

how to return value to parent function from nested anonymous function

戏子无情 提交于 2019-12-03 16:04:32
I have a javascript function which should return a geocoding for a string: function codeAddress(address) { var result = (new google.maps.Geocoder()).geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { return String(results[0].geometry.location.Ya)+','+String(results[0].geometry.location.Za) } else { return status; } }); console.log(result); return result } However it returns "undefined". I understand the bug here,i.e, since javascript is asynchronous, its returning from the function codeAddress even before function(results, status) gets

PHP Anonymous function with array_walk

橙三吉。 提交于 2019-12-03 13:30:49
I'm trying to use array_walk with an anonymous function, but I always get the error // Parse error: syntax error, unexpected T_FUNCTION in ... on line X if(!empty($myArray)) { array_walk($myArray, function(&$value, $key){ // Line X $value = '"'.$value.'"'; // Add quotes }); } The surrounding file syntax is correct. Any thoughts? Yes, true anonymous functions (closures) are only available from PHP 5.3, however you can still create an anonymous function in earlier versions of PHP using the create_function() call, which can be used with array_walk(). Something like: array_walk($myArray, create

Fake anonymous functions in C

房东的猫 提交于 2019-12-03 11:55:40
问题 In this SO thread, Brian Postow suggested a solution involving fake anonymous functions: make a comp(L) function that returns the version of comp for arrays of length L... that way L becomes a parameter, not a global How do I implement such a function? 回答1: See the answer I just posted to that question. You can use the callback(3) library to generate new functions at runtime. It's not standards compliant, since it involves lots of ugly platform-specific hacks, but it does work on a large

Are lambda functions faster than delegates/anonymous functions?

孤街醉人 提交于 2019-12-03 11:29:32
I assumed lambda functions , delegates and anonymous functions with the same body would have the same "speed", however, running the following simple program: static void Main(string[] args) { List<int> items = new List<int>(); Random random = new Random(); for (int i = 0; i < 10000000; i++) { items.Add(random.Next()); } Stopwatch watch; IEnumerable<int> result; Func<int, bool> @delegate = delegate(int i) { return i < 500; }; watch = Stopwatch.StartNew(); result = items.Where(@delegate); watch.Stop(); Console.WriteLine("Delegate: {0}", watch.Elapsed.TotalMilliseconds); Func<int, bool> lambda =

Which languages support *recursive* function literals / anonymous functions?

假装没事ソ 提交于 2019-12-03 08:54:00
问题 It seems quite a few mainstream languages support function literals these days. They are also called anonymous functions, but I don't care if they have a name. The important thing is that a function literal is an expression which yields a function which hasn't already been defined elsewhere, so for example in C, &printf doesn't count. EDIT to add: if you have a genuine function literal expression <exp> , you should be able to pass it to a function f(<exp>) or immediately apply it to an

How to call a method inside a javascript object

强颜欢笑 提交于 2019-12-03 07:26:39
I'm just learning about how to best organize my javascript code, and I had a question regarding this small piece of code I wrote: var reportsControllerIndex = { plotMapPoints: function(data) { //plots points }, drawMap: function() { $.getJSON('/reports.json', function(data) { reportsControllerIndex.plotMapPoints(data); }); }, run: function() { reportsControllerIndex.drawMap(); } }; The question is regarding calling another function of reportsControllerIndex from within the reportsControllerIndex object. I had first tried the following piece of code for the run function: run: function() { this

Dynamically assigning function implementation in Python

筅森魡賤 提交于 2019-12-03 07:24:13
问题 I want to assign a function implementation dynamically. Let's start with the following: class Doer(object): def __init__(self): self.name = "Bob" def doSomething(self): print "%s got it done" % self.name def doItBetter(self): print "Done better" In other languages we would make doItBetter an anonymous function and assign it to the object. But no support for anonymous functions in Python. Instead, we'll try making a callable class instance, and assign that to the class: class Doer(object): def

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

余生长醉 提交于 2019-12-03 07:16:27
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 don't seem to need this explicit type? The trick to type inference is to consider it as an process of