This question already has answers here:
F# Functions vs. Values
(4 answers)
Closed 7 years ago.
I have F# code something like this:
let ran = new System.Random()
let makeVal = ran.NextDouble()
Why when I use makeVal do I get the same random number on every call within one run of the app session (i.e. it's not a seed issue).
Values in F# are immutable by default. So makeVal will not change after the first binding. To get different random values you should call ran.NextDouble() again.
For example use the function:
let makeVal() = ran.NextDouble()
Related
This question already has answers here:
What are the differences between C-like, constructor, and uniform initialization?
(2 answers)
What are the advantages of list initialization (using curly braces)?
(5 answers)
Closed 3 years ago.
I am new to C++ 11 & 14. In my new role I saw a code where string was initialized (below) and I do not know what is this new way called and where to read about it. Can some one tell what is it called and how does it work. Regards.
std::string mystring{""};
This is initialization with string literal, refer to :https://en.cppreference.com/w/cpp/string/basic_string/basic_string
see Notes.
Your example is same as std:string s{"",0};
This question already has answers here:
Why should I prefer `Option::ok_or_else` instead of `Option::ok_or`?
(3 answers)
Closed 3 years ago.
fn main() {
let _one = None.unwrap_or("one".to_string());
let _two = None.unwrap_or_else(|| "two".to_string());
}
Any particular reason why people should prefer unwrap_or_else over unwrap_or?
I have seen comments that unwrap_or is eager (this as an example). Does that mean the values in unwrap_or are always evaluated before program execution? And the FnOnce values in unwrap_or_else is called only when the program execute up to that line?
Both are evaluated during the program's execution and can be arbitrary values. The difference is that:
With unwrap_or the fallback value is evaluated just before unwrap_or is called, and therefore is evaluated whether it's needed or not (because Rust is an eager language).
With unwrap_or_else the fallback value is evaluated only when unwrap_or_else triggers it (by invoking the function you pass), and therefore is evaluated only if it's needed.
This question already has answers here:
Lambda expression to convert array/List of String to array/List of Integers
(10 answers)
Closed 6 years ago.
I am converting my project to java8. How would I write this code in a better way using java8?
List<Bar> bars = new ArrayList<>();
for (Foo foo : obj.getFooList()) {
bars.add(Helper.fooToBar(foo));
}
return detailsVos;
Stream the list, mapping using a method reference, then collect to a list and return it:
return obj.getFooList().stream().map(Helper::fooToBar).collect(Collectors.toList());
Note that "better" has been interpreted as "neater" and "using the Java 8 style".
Also note that this may perform slightly worse than your original code, due to the overhead of using a stream.
This question already has answers here:
What does an underscore and interface name after keyword var mean?
(2 answers)
Closed 5 years ago.
I was reading DigitalOcean's golang client. I noticed that they create an instance of their *Op struct in a _ variable. Example:
https://github.com/digitalocean/godo/blob/master/droplets.go#L32
var _ DropletsService = &DropletsServiceOp{}
Why is this line needed?
This line is a compile time check that *DropletsServiceOp satisfies the DropletsService interface.
The line has no effect on the execution of the program.
If you look at the blame on that file, at that line, you get a clue:
https://github.com/digitalocean/godo/blame/master/droplets.go#L32
It does a compile-time check that *DropletsServiceOp satisfies the DropletsService interface. Prior to the commit introducing that, they were doing this in their test suite.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A cycle was detected in a LINQ expression exception
I have a small problem. I have 2 IQueryable (A and B). I want to find the complement numbers. For this i use Except: A.Except(B)
This will give me all the number in A that ARE NOT in B.
This works. My problem is that i want to do this A = A.Except(B)
But this leaves me with an ERROR:
A cycle was detected in a LINQ expression exception
Anyone got a suggestion on how I could solve this.
I can't just create a new IQueryable C to hold the A.Except(B) result. Because I need A repediatly in later code.
Just create an intermediate list which you use to store your result.
var C = A.Except(B).ToArray();
C will hold your desired result, while A and B will remain unchanged.