closures

Javascript closure syntax

偶尔善良 提交于 2019-12-12 08:47:49
问题 What is the difference, pros/cons (if any) between these constructs? new function(obj) { console.log(obj); }(extObj); vs (function(obj) { console.log(obj); })(extObj); 回答1: If you're looking at dealing expressly with an immediately-invoked function, then there isn't any real difference between the two, except that the first one will return this inside the function, to the outside world, automatically (that is what new does), and by default if a different return value is not specified (regular

Borrow-check error with variable not living long enough in nested lambda

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 08:37:02
问题 I am getting an error inside a nested lambda. let rows = vec![ vec![3, 6, 2, 8, 9, 0], vec![0, 0, 1, 4, 5, 1], ]; let pair_sums = rows.iter() .flat_map(|row| { (0 ..= row.len()).map(|i| row[i] + row[i + 1]) }) .collect::<Vec<_>>(); println!("{:?}", pair_sums); error[E0597]: `row` does not live long enough --> src/main.rs:9:40 | 9 | (0..row.len() - 1).map(|i| row[i] + row[i + 1]) | --- ^^^ does not live long enough | | | capture occurs here 10 | }) | - borrowed value only lives until here 11 |

Using a callback in implode()

隐身守侯 提交于 2019-12-12 08:23:50
问题 I have a multidimensional array, e.g.: $values = array( 'one' => array( 'title' => 'Title One', 'uri' => 'http://example.com/one', ), 'two' => array( 'title' => 'Title Two', 'uri' => 'http://example.com/two', ), ); ...and I'd like to parse through that with a closure in my implode function, à la: $final_string = implode(' | ', function($values) { $return = array(); foreach($values as $value) $return[] = '<a href="' . $value['uri'] . '">' . $value['title'] . '</a>'; return $return; }); However

How to call a method n times in Scala?

主宰稳场 提交于 2019-12-12 08:18:51
问题 I have a case where I want to call a method n times, where n is an Int. Is there a good way to do this in a "functional" way in Scala? case class Event(name: String, quantity: Int, value: Option[BigDecimal]) // a list of events val lst = List( Event("supply", 3, Some(new java.math.BigDecimal("39.00"))), Event("sale", 1, None), Event("supply", 1, Some(new java.math.BigDecimal("41.00"))) ) // a mutable queue val queue = new scala.collection.mutable.Queue[BigDecimal] lst.map { event => event

Usage of local variables in closures when accessing Spark RDDs

主宰稳场 提交于 2019-12-12 08:06:23
问题 I have a question regarding the usage of local variables in closures when accessing Spark RDDs. The problem I would like to solve looks as follows: I have a list of textfiles that should be read into an RDD. However, first I need to add additional information to an RDD that is created from a single textfile. This additional information is extracted from the filename. Then, the RDDs are put into one big RDD using union(). from pyspark import SparkConf, SparkContext spark_conf = SparkConf()

Where does Python store the name binding of function closure?

天涯浪子 提交于 2019-12-12 07:58:08
问题 So recently I understand the concept of function closure. def outer(): somevar = [] assert "somevar" in locals() and not "somevar" in globals() def inner(): assert "somevar" in locals() and not "somevar" in globals() somevar.append(5) return somevar return inner function = outer() somevar_returned = function() assert id(somevar_returned) == id(function.func_closure[0].cell_contents) As much as I understand, the objective of function closure is to keep an active reference to the object, in

Inconsistency of arity between Hash.each and lambdas

六眼飞鱼酱① 提交于 2019-12-12 07:20:45
问题 I lifted the following example from Josh Susser def strip_accents params thunk = lambda do |key,value| case value when String then value.remove_accents! when Hash then value.each(&thunk) end end params.each(&thunk) end when I put it in the the rails console (irb), and call it with a hash, I get the following: ruby-1.9.2-p136 :044 > `ruby --version` => "ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]\n" ruby-1.9.2-p136 :045 > strip_accents({:packs=>{:qty=>1}}) ArgumentError: wrong

iOS Swift Pass Closure as Property?

大憨熊 提交于 2019-12-12 07:09:33
问题 Let's say I have a custom UIView, lets call it MyCustomView. Inside this view is a UITextField property. Suppose my goal is to be able to create an instance of MyCustomView and add it to some view controller somewhere, and I want that view controller to be able to handle actions taken on that text field. For example, if I hit "return" on the keyboard within the text field, I may want to do some action - let me give an example of what I'm envisioning with some objective-c psuedo code:

Trying to simplify some Javascript with closures

早过忘川 提交于 2019-12-12 07:06:11
问题 I'm trying to simplify some JS code that uses closures but I am getting nowhere (probably because I'm not grokking closures) I have some code that looks like this: var server = http.createServer(function (request, response) { var httpmethods = { "GET": function() { alert('GET') }, "PUT": function() { alert('PUT') } }; }); And I'm trying to simplify it in this way: var server = http.createServer(function (request, response) { var httpmethods = { "GET": function() { alertGET() }, "PUT":

What is loop variable closure?

余生颓废 提交于 2019-12-12 06:44:09
问题 I have read in this question that in C# 5.0 was introduced loop variable closure, and I have accessed the link in the question to find out what it means. Shame on me, I wasn't able to understand much of what is a loop variable closure, since the blog article explains much about what involves to generate a warning from section blocks where before wasn't any compilation warning generated, and the impact of code changes at a new version of programming language. Since what I understand, the loop