assign

setq and defvar in Lisp

橙三吉。 提交于 2019-11-27 00:19:22
I see that the Practical Common Lisp uses (defvar *db* nil) for setting up a global variable . Isn't it OK to use setq for the same purpose? What are the advantages/disadvantages of using defvar vs. setq ? There are several ways to introduce variables. DEFVAR and DEFPARAMETER introduce global dynamic variables. DEFVAR optionally sets it to some value, unless it is already defined. DEFPARAMETER sets it always to the provided value. SETQ does not introduce a variable. (defparameter *number-of-processes* 10) (defvar *world* (make-world)) ; the world is made only once. Notice that you likely never

Error: Assigning to an array from an initializer list

落花浮王杯 提交于 2019-11-26 23:27:09
问题 I have a class such as: class dialog { public: double dReturnType[][5][3]; }; #include <cstdlib> #include <iostream> include <string> using namespace std; #include "dialog.h"; int main(int argc, char *argv[]) { dialog People; People.dReturnType[0][1] = {1.2,2.3,6.6}; return 0; } It returns: [Warning] extended initializer lists only available with -std=c++11 or -std=gnu11 [enabled by default] [Error]: assigning to an array from an initializer list I've looked it up online a bit and really

Why does an lvalue cast work?

此生再无相见时 提交于 2019-11-26 22:09:26
问题 I saw this kind of cast for the first time today, and I'm curious as to why this works. I thought casting in this manner would assign to the temporary, and not the class member. Using VC2010. class A { public: A() : m_value(1.f) { ((float)m_value) = 10.f; } const float m_value; }; 回答1: It shouldn't work. An explicit type conversion to float with cast notation will be a prvalue (§5.4): The result of the expression (T) cast-expression is of type T . The result is an lvalue if T is an lvalue

Assign multiple objects to .GlobalEnv from within a function

回眸只為那壹抹淺笑 提交于 2019-11-26 16:34:30
A post on here a day back has me wondering how to assign values to multiple objects in the global environment from within a function. This is my attempt using lapply ( assign may be safer than <<- but I have never actually used it and am not familiar with it). #fake data set df <- data.frame( x.2=rnorm(25), y.2=rnorm(25), g=rep(factor(LETTERS[1:5]), 5) ) #split it into a list of data frames LIST <- split(df, df$g) #pre-allot 5 objects in R with class data.frame() V <- W <- X <- Y <- Z <- data.frame() #attempt to assign the data frames in the LIST to the objects just created lapply(seq_along

Assign multiple new variables on LHS in a single line

半世苍凉 提交于 2019-11-26 10:22:20
I want to assign multiple variables in a single line in R. Is it possible to do something like this? values # initialize some vector of values (a, b) = values[c(2,4)] # assign a and b to values at 2 and 4 indices of 'values' Typically I want to assign about 5-6 variables in a single line, instead of having multiple lines. Is there an alternative? There is a great answer on the Struggling Through Problems Blog This is taken from there, with very minor modifications. USING THE FOLLOWING THREE FUNCTIONS (Plus one for allowing for lists of different sizes) # Generic form '%=%' = function(l, r, ...

What is better in a foreach loop… using the & symbol or reassigning based on key?

北城余情 提交于 2019-11-26 09:52:30
问题 Consider the following PHP Code: //Method 1 $array = array(1,2,3,4,5); foreach($array as $i=>$number){ $number++; $array[$i] = $number; } print_r($array); //Method 2 $array = array(1,2,3,4,5); foreach($array as &$number){ $number++; } print_r($array); Both methods accomplish the same task, one by assigning a reference and another by re-assigning based on key. I want to use good programming techniques in my work and I wonder which method is the better programming practice? Or is this one of

setq and defvar in Lisp

被刻印的时光 ゝ 提交于 2019-11-26 09:22:30
问题 I see that the Practical Common Lisp uses (defvar *db* nil) for setting up a global variable . Isn\'t it OK to use setq for the same purpose? What are the advantages/disadvantages of using defvar vs. setq ? 回答1: There are several ways to introduce variables. DEFVAR and DEFPARAMETER introduce global dynamic variables. DEFVAR optionally sets it to some value, unless it is already defined. DEFPARAMETER sets it always to the provided value. SETQ does not introduce a variable. (defparameter

Assign multiple objects to .GlobalEnv from within a function

纵饮孤独 提交于 2019-11-26 04:50:29
问题 A post on here a day back has me wondering how to assign values to multiple objects in the global environment from within a function. This is my attempt using lapply ( assign may be safer than <<- but I have never actually used it and am not familiar with it). #fake data set df <- data.frame( x.2=rnorm(25), y.2=rnorm(25), g=rep(factor(LETTERS[1:5]), 5) ) #split it into a list of data frames LIST <- split(df, df$g) #pre-allot 5 objects in R with class data.frame() V <- W <- X <- Y <- Z <- data

How to assign from a function which returns more than one value?

落爺英雄遲暮 提交于 2019-11-26 02:39:21
Still trying to get into the R logic... what is the "best" way to unpack (on LHS) the results from a function returning multiple values? I can't do this apparently: R> functionReturningTwoValues <- function() { return(c(1, 2)) } R> functionReturningTwoValues() [1] 1 2 R> a, b <- functionReturningTwoValues() Error: unexpected ',' in "a," R> c(a, b) <- functionReturningTwoValues() Error in c(a, b) <- functionReturningTwoValues() : object 'a' not found must I really do the following? R> r <- functionReturningTwoValues() R> a <- r[1]; b <- r[2] or would the R programmer write something more like

Assign multiple new variables on LHS in a single line

青春壹個敷衍的年華 提交于 2019-11-26 02:07:00
问题 I want to assign multiple variables in a single line in R. Is it possible to do something like this? values # initialize some vector of values (a, b) = values[c(2,4)] # assign a and b to values at 2 and 4 indices of \'values\' Typically I want to assign about 5-6 variables in a single line, instead of having multiple lines. Is there an alternative? 回答1: There is a great answer on the Struggling Through Problems Blog This is taken from there, with very minor modifications. USING THE FOLLOWING