closures

What happens with values in closure? JavaScript

笑着哭i 提交于 2020-01-07 05:33:09
问题 I've seen a really cool explanation of closure. So in my view closure is a way to store data somewhere. Let's see examples of closure and example without closure: Without closure: function F1(x,y) { var z=5; function F2(){ console.log('x='+x+'y='+y+'z='+z); } F2(); } F1(1,2) With closure: function F1(x,y) { var z=5; function F2(){ console.log('x='+x+'y='+y+'z='+z); } return F2(); } var p=F1(1,2) p(); I've understood that in closure when F1() finishes its work, then F1() clears connection to

How to spy a function with Sinon.js that is in the same js file as the function under test

血红的双手。 提交于 2020-01-07 04:13:04
问题 I have a problem with Sinon.js while trying to spy a function that is in the same javascript file as the function that I want to test. Furthermore I assert that the spied function is called once. Unfortunately the test fails. Interesting thing is, that if the spied function is in another javascript file than the function under test it works ! Here is my code: mock_test.js: var sinon = require('sinon') var one = require('./one.js') var two = require('./two.js') describe('Spy ', function () {

Sequence of execution for closures

纵饮孤独 提交于 2020-01-07 03:48:23
问题 Here's a test function I created to learn how lapply and closures interact. My objective is to iterate over runif function sequentially and print each iteration. Mod_delay_by <- function(delay,f){ function(x,...){ Sys.sleep(delay) cat(".") #f(x,...) cat("Processing",x,"\n",sep = "") f(x,...) } } Now let's use lapply to call above function: lapply(1:3,Mod_delay_by(0.1,runif)) When I call above function, I get the following sample output: .Processing1 .Processing2 .Processing3 [[1]] [1] 0

Access Data Outside of Closure, Firebase ObserveSingleEvent // SWIFT

女生的网名这么多〃 提交于 2020-01-07 02:51:35
问题 When I first asked this question I hadn't really done my research. But after 20+ hours on this, I have structure exactly like in Firebase docs. But, I can't access any of the data outside of the closure. Here is the struct where the data should be written in to: struct UserStruct { let name : String! let age : String! } And when it gets called, everything is written perfect in the database, inside the closure it doesn't print nil, it does print the actual value obviously. I have already tried

How do you test functions and closures for equality?

吃可爱长大的小学妹 提交于 2020-01-07 02:04:51
问题 The book says that "functions and closures are reference types". So, how do you find out if the references are equal? == and === don't work. func a() { } let å = a let b = å === å // Could not find an overload for === that accepts the supplied arguments Here is how the Catterwauls are dealing with this: MultiClosures & Equatable Closures tests 回答1: Chris Lattner wrote on the developer forums: This is a feature we intentionally do not want to support. There are a variety of things that will

How do you test functions and closures for equality?

不羁的心 提交于 2020-01-07 02:04:14
问题 The book says that "functions and closures are reference types". So, how do you find out if the references are equal? == and === don't work. func a() { } let å = a let b = å === å // Could not find an overload for === that accepts the supplied arguments Here is how the Catterwauls are dealing with this: MultiClosures & Equatable Closures tests 回答1: Chris Lattner wrote on the developer forums: This is a feature we intentionally do not want to support. There are a variety of things that will

UIView, CMDeviceMotionHandler : unowned may only be applied to class and class-bound protocol types

只谈情不闲聊 提交于 2020-01-06 23:52:06
问题 I'm creating a UIView that listens to CMDeviceMotion Events: class MyView: UIView{ private var motionManager = CMMotionManager() let motionQueue = NSOperationQueue() override func awakeFromNib() { self.setupView() } func setupView(){ self.motionManager.deviceMotionUpdateInterval = 0.5 self.motionManager.startDeviceMotionUpdatesUsingReferenceFrame(.XArbitraryZVertical, toQueue: self.motionQueue, withHandler: self.motionHandler) } // MARK: - CMDeviceMotionHandler let motionHandler :

How to access the parent collection in a each closure?

主宰稳场 提交于 2020-01-06 19:53:47
问题 I'm looking from something that to replace the *parent* . %w[apple apples].each do |w| next if *parent*.includes? w + "s" puts w end # output: apples 回答1: each is a convention, there is no concept of a "parent collection" for blocks in general or ones passed to each in particular. Just name it, eg (parent = %w[apple apples]).each do |w| next if parent.includes? w + "s" puts w end You could add a different method to pass a parent, eg module Each2 def each2 each { |elem| yield(self, elem) } end

Adding explicit return to closure causes compiler error: A compiler bug?

我的未来我决定 提交于 2020-01-06 18:53:21
问题 Consider this generic method (only the types are important, not what it does): func flatMap<SourceType, TargetType>(source: [SourceType], transform: SourceType [TargetType]) -> [TargetType] { return [] } Following call to the method compiles nicely: let seq = flatMap(["some", "string"], { s in [1, 2] }) However, just adding explicit return to closure cases compile error: let seq = flatMap(["some", "string"], { s in return [1, 2] }) //ERROR: Cannot convert the expression's type ... to type

Ruby rcurry. How I can implement proc “right” currying?

别等时光非礼了梦想. 提交于 2020-01-06 15:04:12
问题 I'm working in a new gem to extend the Ruby Core classes, something similar to Active Support Core Extensions or Powerpack. These days I'm working on the Proc class, for example I added closure composition. But today I like to talk about currying. This is the Ruby standard library functionality for curry : describe "#curry" do it "returns a curried proc" do modulus = ->(mod, num) { num % mod } mod2 = modulus.curry[2] expect(mod2.call(5)).to eq(1) end end I like to add a new rcurry method to