side-effects

Should one wrap type providers containing values that have side effects inside a class?

廉价感情. 提交于 2020-01-05 02:32:25
问题 I am trying to implement in my code the excellent advice in the F# coding conventions page https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/conventions. The section Use classes to contain values that have side effects is particularly interesting. It says There are many times when initializing a value can have side effects, such as instantiating a context to a database or other remote resource. It is tempting to initialize such things in a module and use it in subsequent functions.

Can C++ compiler assume a const bool & value will not change?

放肆的年华 提交于 2020-01-03 16:39:36
问题 Can the C++ compiler assume a 'const bool &' value will not change? For example, imagine that I have a class: class test { public: test(const bool &state) : _test(state) { } void doSomething() { if (_test) { doMore(); } } void doMore(); private: const bool &_test; }; And I use it as follows: void example() { bool myState = true; test myTest(myState); while (someTest()) { myTest.doSomething(); myState = anotherTest(); } } Is it allowed by the standard for the compiler to assume _test's value

Can C++ compiler assume a const bool & value will not change?

大憨熊 提交于 2020-01-03 16:38:11
问题 Can the C++ compiler assume a 'const bool &' value will not change? For example, imagine that I have a class: class test { public: test(const bool &state) : _test(state) { } void doSomething() { if (_test) { doMore(); } } void doMore(); private: const bool &_test; }; And I use it as follows: void example() { bool myState = true; test myTest(myState); while (someTest()) { myTest.doSomething(); myState = anotherTest(); } } Is it allowed by the standard for the compiler to assume _test's value

Can C++ compiler assume a const bool & value will not change?

☆樱花仙子☆ 提交于 2020-01-03 16:38:08
问题 Can the C++ compiler assume a 'const bool &' value will not change? For example, imagine that I have a class: class test { public: test(const bool &state) : _test(state) { } void doSomething() { if (_test) { doMore(); } } void doMore(); private: const bool &_test; }; And I use it as follows: void example() { bool myState = true; test myTest(myState); while (someTest()) { myTest.doSomething(); myState = anotherTest(); } } Is it allowed by the standard for the compiler to assume _test's value

Generate unique numbers at compile time

点点圈 提交于 2020-01-02 02:20:29
问题 I want to generate unique numbers for each class in my header, primes in my case primes but let's say this should only be consecutive numbers i.e. 1,2,3,4,etc. Of course I can hardcode these: struct A { enum { ID = 1; }; }; struct B { enum { ID = 2; }; }; struct C { enum { ID = 3; }; }; struct D { enum { ID = 4; }; }; This is very error-prone since in reality the classes are not that small and if I add a new class in the middle I have to change all the following numbers if I don't want to

How to handle bidirectional relationships when constructing hibernate entities?

半城伤御伤魂 提交于 2020-01-01 12:16:08
问题 I want to model the relationship between two entities, a group and an account with JPA/Hibernate. An account can have several groups, but not vice versa, so we have a OneToMany relationship between account and group. My working colleague suggested to model the entities Account and Group like public class Account { private List<Group> groups = new ArrayList<Group>(); public Account() {} public void setGroups(List<Group> usergroups) { this.groups = groups; } @OneToMany(mappedBy = "account")

How to handle bidirectional relationships when constructing hibernate entities?

一个人想着一个人 提交于 2020-01-01 12:16:04
问题 I want to model the relationship between two entities, a group and an account with JPA/Hibernate. An account can have several groups, but not vice versa, so we have a OneToMany relationship between account and group. My working colleague suggested to model the entities Account and Group like public class Account { private List<Group> groups = new ArrayList<Group>(); public Account() {} public void setGroups(List<Group> usergroups) { this.groups = groups; } @OneToMany(mappedBy = "account")

Functional programming construct for composing identity and side effect

徘徊边缘 提交于 2020-01-01 10:49:27
问题 Does functional programming have a standard construct for this logic? const passAround = (f) => (x) => { f(x); return x; }; This enables me to compose functions that have side effects and no return values, like console.log . It's not like a Task because I don't want to represent the state of the side effect. 回答1: The SKI combinator calculus might interest you. Let's pretend that f is always a pure function: const S = g => f => x => g(x)(f(x)); // S combinator of SKI combinator calculus const

Cancel request using redux observable is not working

谁都会走 提交于 2019-12-24 18:55:17
问题 I'm trying to add in canceling in my request using redux-observable. Trying to implement a simple login example. Currently, I am unable to submit the login request again after I added the cancelation. const loginUserApiCall = (username, password) => { return new Promise((resolve, reject) => { if (username === "foo" && password === "foo") { resolve({ user: { name: "foo", lastName: "fooLName", email: "foo@gmail.com" } }); } reject({ err: "Creds are incorrect" }); }); }; const loginRequestEpic =

Is `pure x :: IO a` a pure value or one with a side effect?

送分小仙女□ 提交于 2019-12-23 20:21:06
问题 Given pure id <*> v = v holds, can pure do anything observable and not break the law? If I define a type that encapsulates IO and say, spawn a new thread, is GHC free to optimize it away? EDIT: I finally realized that the question actually is about consequences of having an unlawful instance of IO... 回答1: GHC does not know anything about type class laws (unlike e.g. Idris or Coq), those only exist as documentation and programming convention. Hence, an instance can be lawful or unlawful, and