scoping

Is it a good practice to call functions in a package via ::

我怕爱的太早我们不能终老 提交于 2019-11-26 22:38:15
问题 I'm writing some R functions that employ some useful functions in other packages like stringr and base64enc . Is it good not to call library(...) or require(... ) to load these packages first but to use :: to directly refer to the function I need, like stringr::str_match(...) ? Is it a good practice in general case? Or what problem might it induce? 回答1: It all depends on context. :: is primarily necessary if there are namespace collisions , functions from different packages with the same name

Node-style require for in-browser javascript?

若如初见. 提交于 2019-11-26 19:42:49
Are there any libraries for in-browser javascript that provide the same flexibility/modularity/ease of use as Node's require ? To provide more detail: the reason require is so good is that it: Allows code to be dynamically loaded from other locations (which is stylistically better, in my opinion, than linking all your code in the HTML) It provides a consistent interface for building modules It is easy for modules to depend on other modules (so I could write, for instance, an API that requires jQuery so I can use jQuery.ajax() Loaded javascript is scoped , meaning I could load with var dsp =

What are the double colons (::) in R?

拜拜、爱过 提交于 2019-11-26 16:08:11
问题 I am following a tutorial in Rbloggers and found the use of double colons, I looked online, but I couldn't find an explanation for their use. Here is an example of their use. df <- dplyr::data_frame( year = c(2015, NA, NA, NA), trt = c("A", NA, "B", NA) ) I understand it creates a data frame but I don't understand their purpose. 回答1: As you probably have looked up the help page by now usage of :: helps to access the exact function from that specific package. When you load dplyr you probably

What is the difference between my and local in Perl?

妖精的绣舞 提交于 2019-11-26 15:47:14
问题 I am seeing both of them used in this script I am trying to debug and the literature is just not clear. Can someone demystify this for me? 回答1: Dynamic Scoping. It is a neat concept. Many people don't use it, or understand it. Basically think of my as creating and anchoring a variable to one block of {}, A.K.A. scope. my $foo if (true); # $foo lives and dies within the if statement. So a my variable is what you are used to. whereas with dynamic scoping $var can be declared anywhere and used

How can a non-imported method in a not-attached package be found by calls to functions not having it in their namespace?

江枫思渺然 提交于 2019-11-26 11:05:15
问题 An R namespace acts as the immediate environment for all functions in its associated package. In other words, when function bar() from package foo calls another function, the R evaluator first searches for the other function in <environment: namespace:foo> , then in \"imports.foo\" , <environment: namespace:base> , <environment: R_GlobalEnv> , and so on down the search list returned by typing search() . One nice aspect of namespaces is that they can make packages act like better citizens:

Node-style require for in-browser javascript?

你。 提交于 2019-11-26 06:05:46
问题 Are there any libraries for in-browser javascript that provide the same flexibility/modularity/ease of use as Node\'s require ? To provide more detail: the reason require is so good is that it: Allows code to be dynamically loaded from other locations (which is stylistically better, in my opinion, than linking all your code in the HTML) It provides a consistent interface for building modules It is easy for modules to depend on other modules (so I could write, for instance, an API that

How do you use “<<-” (scoping assignment) in R?

邮差的信 提交于 2019-11-25 23:45:45
问题 I just finished reading about scoping in the R intro, and am very curious about the <<- assignment. The manual showed one (very interesting) example for <<- , which I feel I understood. What I am still missing is the context of when this can be useful. So what I would love to read from you are examples (or links to examples) on when the use of <<- can be interesting/useful. What might be the dangers of using it (it looks easy to loose track of), and any tips you might feel like sharing. 回答1:

What is lexical scope?

大城市里の小女人 提交于 2019-11-25 22:23:41
问题 Could someone please give me a brief introduction to lexical scoping? 回答1: I understand them through examples. :) First, Lexical Scope (also called Static Scope), in C-like syntax: void fun() { int x = 5; void fun2() { printf("%d", x); } } Every inner level can access its outer levels. There is another way, called Dynamic Scope used by first implementation of Lisp, again in C-like Syntax: void fun() { printf("%d", x); } void dummy1() { int x = 5; fun(); } void dummy2() { int x = 10; fun(); }

Javascript function scoping and hoisting

岁酱吖の 提交于 2019-11-25 21:40:16
问题 I just read a great article about JavaScript Scoping and Hoisting by Ben Cherry in which he gives the following example: var a = 1; function b() { a = 10; return; function a() {} } b(); alert(a); Using the code above, the browser will alert \"1\". I\'m still unsure why it returns \"1\". Some of the things he says come to mind like: All the function declarations are hoisted to the top. You can scope a variable using function. Still doesn\'t click for me. 回答1: Function hoisting means that