function-parameter

Scala - parameter of type T or => T

拜拜、爱过 提交于 2021-02-07 08:16:20
问题 Is there any difference between the following def foo(s: String) = { ... } and def foo(s: => String) { ... } both these definitions accept "sss" as parameter. 回答1: An argument String is a by-value parameter, => String is a by-name parameter. In the first case, the string is passed in, in the second a so-called thunk which evaluates to a String whenever it is used. def stringGen: String = util.Random.nextInt().toString def byValue(s: String) = println("We have a '" + s + "' and a '" + s + "'")

Scala - parameter of type T or => T

大憨熊 提交于 2021-02-07 08:15:36
问题 Is there any difference between the following def foo(s: String) = { ... } and def foo(s: => String) { ... } both these definitions accept "sss" as parameter. 回答1: An argument String is a by-value parameter, => String is a by-name parameter. In the first case, the string is passed in, in the second a so-called thunk which evaluates to a String whenever it is used. def stringGen: String = util.Random.nextInt().toString def byValue(s: String) = println("We have a '" + s + "' and a '" + s + "'")

What does : => A syntax mean in method parameter declaration? [duplicate]

跟風遠走 提交于 2021-02-04 08:34:09
问题 This question already has answers here : By-name parameter vs anonymous function (5 answers) Closed 11 months ago . So, reading the Scala tour about implicit classes in Scala, I came across this piece of code: object Helpers { implicit class IntWithTimes(x: Int) { def times[A](f: => A): Unit = { def loop(current: Int): Unit = if(current > 0) { f loop(current - 1) } loop(x) } } } What is troubling me here is the def times[A](f: => A): Unit = { line. What is going on here? The part of the

Swift: using member constant as default value for function parameter

旧巷老猫 提交于 2021-01-27 07:08:15
问题 I have a swift class, in which I am trying to pass a default value for a function parameter: class SuperDuperCoolClass : UIViewController { // declared a constant let primaryColor : UIColor = UIColor(red: 72.0/255.0, green: 86.0/255.0, blue: 114.0/255.0, alpha: 1.0) // compilation error at below line: SuperDuperCoolClass.Type does not have a member named 'primaryColor' func configureCheckmarkedBullet(bullet: UIButton, color: UIColor = primaryColor){ // some cool stuff with bullet and

Use function argument as name for new data frame in R

女生的网名这么多〃 提交于 2020-07-18 17:45:07
问题 this is very simple, but I have searched and failed to find a solution for this small problem. I want to use the argument of a function as the name for a new data frame, for example: assign.dataset<-function(dataname){ x<-c(1,2,3) y<-c(3,4,5) dataname<<-rbind(x,y) } Then assign.dataset(new.dataframe.name) just creates a new dataset with the name dataname . I have tried to use the paste and assign functions but with no success. Many thanks 回答1: You can do it like this... assign.dataset<

Late destruction of function parameters

瘦欲@ 提交于 2020-04-05 15:53:54
问题 According to 5.2.2/4 "Function call" in n4640 (8.2.2/4 in n4659) function parameters are created and destroyed in the context of the caller. And implementations are allowed to delay the destruction of function parameters to the end of the enclosing full expression (as an implementation-defined feature). Note that the choice is not unspecified , but rather implementation-defined . ( It is not entirely clear how this agrees with 3.3.3 "Block scope" (6.3.3 in n4659), which seems to imply that

Passing functions as arguments in C++

回眸只為那壹抹淺笑 提交于 2020-02-03 08:10:44
问题 I'm a bit rusty in C++ and I'm switching after a year of python. Naturally I would like to translate the laziness coming from python to C++. I just discovered rot13 and I'm all excited about it. I found 3 ways of doing it and I wanted to do a little performance test. I wanted to see also if there is a difference in modifying the string in place or creating a new one. So I ended up having 6 functions. In the first method, I use a std::map to map the characters, thus I've built a class that

Understanding difference between int literal vs int parameter in PL/pgSQL function

妖精的绣舞 提交于 2020-01-24 20:42:23
问题 I have a function to left pad bit stings in PostgreSQL 9.5: CREATE OR REPLACE FUNCTION lpad_bits(val bit varying) RETURNS bit varying as $BODY$ BEGIN return val::bit(32) >> (32-length(val)); END; $BODY$ LANGUAGE plpgsql IMMUTABLE; which works fine: # select lpad_bits(b'1001100111000'); lpad_bits ---------------------------------- 00000000000000000001001100111000 (1 row) My problem is when I try to add a parameter to change the amount of padding: CREATE OR REPLACE FUNCTION lpad_bits(val bit

Using pointers or references to struct

霸气de小男生 提交于 2020-01-07 02:25:13
问题 Me and my friend is using structs in our code (our code is separate from each other). Lets take the following example: struct Book { char title; int pages; } void setBook(struct Book *tempBook) { tempBook->title = "NiceTitle"; tempBook->pages = 50; } The above code is pretty straight forward. The thing is, is there any difference in having these two mains: int main() { struct book obj1; setBook(&obj); } Or int main() { struct Book *obj2; setBook(obj2); } EDIT: I was not clear in my statement.

Error: Initializing Argument 1 of [closed]

偶尔善良 提交于 2020-01-06 14:47:34
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I've looked around and seen quite a few of these, but none of them provided the solution for my problem. I'm getting this compilation error with the following code: THE ERROR: THE CODE: const int TOP_WORDS = 25; ... void topWords(Hash t, string word, string topA[]); int main() { ... Hash table1; string word =