closures

Javascript module pattern introduced in TGP deentityify method - why is this pattern necessary?

不羁岁月 提交于 2019-12-12 21:37:45
问题 Crockford introduces a pattern in the deentityify method to create a module. He claims: The module pattern takes advantage of function scope and close to create relationships that are binding and private. In this example, only the deentityify method has access to the entity data structure. Distilling to remove his custom functions, I think the code boils down to... String.prototype.deentityify = function() { var entity = { quot: '"', lt: '<', gt: '>' }; return function() { return this.replace

How to test functions inside javascript closure

时光总嘲笑我的痴心妄想 提交于 2019-12-12 20:52:27
问题 This seems impossible (and it might be), but I'm trying to get into more TDD and I keep hitting a wall with closures. Say I have the following: function createSomething(init) { function privateMethod(param) { return init[param]; //assuming this is more complicated, how can you test it? } function getData() { return init.data; } function getValue(name) { if (name === "privateNode" || typeof name !== "string") { return "permissionDenied"; } return privateMethod(name); } return { getData :

How to implement closures into a LISP interpreter

守給你的承諾、 提交于 2019-12-12 19:55:09
问题 I'm currently working on a LISP interpreter written in Java. Now I stuck at the closures. I want to enable closures like this: (define a 1000) (define closure (lambda (a) (lambda (b) (+ a b)))) (define x (closure 10)) (x 20) --> 30 So, (x 20) should return 30 . But, guess what, it returns 1020 in my interpreter. I think the mistake is in my lambda class. It looks like this: public class LLambda extends LOperation { private LList parameters; private LList definitions; public LLambda(LList

Identify how the function has been called in closure javascript

前提是你 提交于 2019-12-12 19:50:34
问题 Recently i faced one problem in hackerrank which has to calculate multiplication operation and has to return the answer. For example function multiply(a,b) { return a*b; } Now here is the problem the function might call in different ways such as multiply(4,5); multiply(4)(5); multiply(4)(5)(6); I know we have to closure apporach for the second one which is multiply(4)(5). I had written code for that function multiply(a,b) { return function(b) { return a*b; } } Now what if its been multiply

Closure identity in swift: unregister observing closure

余生长醉 提交于 2019-12-12 19:19:52
问题 When rethinking my everyday programming patterns to be more swifty , there is one, that I really struggle with: observing changes . After a lot of thinking and research I have yet to find a satisfying solution . That is one, that is easy to use, leverages the full potential of swift's strong type system, is compatible with value types and allows for static dispatch. Maybe the latter one is not possible and that's the reason why my search is unsuccessful so far. If so, I would like to know why

C# under the hood code generation for lambdas within loops

戏子无情 提交于 2019-12-12 19:10:46
问题 This question is based on the answer given by one of my favourite posters Mehrdad Afshari in this question about closure. I am having a hard time understand why C# generates the code the way it does...... Here is the code in question static void Main(string[] args) { List<string> list = new List<string> { "hello world", "TED", "goodbye world" }; IEnumerable<string> filteredList1 = list; IEnumerable<string> filteredList2 = list; var keywords = new[] { "hello", "world" }; foreach (var keyword

SKEase action, how to use Float changing Action Setter Block?

浪子不回头ぞ 提交于 2019-12-12 19:07:08
问题 In the following use case, I'm trying to animate the lineWidth of an SKShapeNode . SKEase is part of the wonderful SpriteKitEasing github repo from Craig Grummitt. One of its facilities is a Float changing ease action that appears to change the value of a float over time. However I can't figure out how to use it. Xcode gives the following suggestions when typing it in: let lineWeight = SKEase.createFloatTween(<start: CGFloat, end: CGFloat, time: TimeInterval, easingFunction: AHEasingFunction,

Unable to send a &str between threads because it does not live long enough

随声附和 提交于 2019-12-12 18:35:03
问题 Given the following simplified program: #[macro_use] extern crate log; extern crate ansi_term; extern crate fern; extern crate time; extern crate threadpool; extern crate id3; mod logging; use std::process::{exit, }; use ansi_term::Colour::{Yellow, Green}; use threadpool::ThreadPool; use std::sync::mpsc::channel; use std::path::{Path}; use id3::Tag; fn main() { logging::setup_logging(); let n_jobs = 2; let files = vec!( "/tmp/The Dynamics - Version Excursions/01-13- Move on Up.mp3", "/tmp/The

Incrementing object id automatically JS constructor (static method and variable)

给你一囗甜甜゛ 提交于 2019-12-12 18:18:57
问题 I am newbie in JavaScript, I have an idea, to create class (function), I know how it works in JS (prototypes and so on), but I need to do something like incrementing Id in databases. My idea is to create static method, I think closure will suit great here and whenever new object is created it should return incremented id. I don't know what is the right way to implement this, or maybe this is bad practice. Please provide simple example. 回答1: Closure is a good idea: var MyClass = (function() {

Unable to implement module pattern

六眼飞鱼酱① 提交于 2019-12-12 17:27:10
问题 I am trying to reproduce some code from the book "Javascript: The Good Parts" by Douglas Crockford. The idea is to use closures for object encapsulation and avoid Javascript's inherent global variables. var serial_maker = function ( ) { // Produce an object that produces unique strings. A // unique string is made up of two parts: a prefix // and a sequence number. The object comes with // methods for setting the prefix and sequence // number, and a gensym method that produces unique //