side-effects

Are side-effects possible in pure functional programming

泪湿孤枕 提交于 2019-11-28 19:33:10
I have been trying to wrap my head around functional programming for a while now? I have looked up lambda calculus, LISP, OCML, F# and even combinatorial logic but the main problem I have is how do you do things that require side effects like (interacting with a user, communicating with a remote service, or even handle simulating using random sampling) without violating the fundamental premise of pure functional programming which is, that for a given input the output is deterministic? I hope I am making sense, if not I welcome any attempts to properly educate me. Thanks in advance. Most real

Is the use of del bad?

允我心安 提交于 2019-11-28 18:33:24
I commonly use del in my code to delete objects: >>> array = [4, 6, 7, 'hello', 8] >>> del(array[array.index('hello')]) >>> array [4, 6, 7, 8] >>> But I have heard many people say that the use of del is unpythonic. Is using del bad practice? >>> array = [4, 6, 7, 'hello', 8] >>> array[array.index('hello'):array.index('hello')+1] = '' >>> array [4, 6, 7, 8] >>> If not, why are there many ways to accomplish the same thing in python? Is one better than the others? Option 1: using del >>> arr = [5, 7, 2, 3] >>> del(arr[1]) >>> arr [5, 2, 3] >>> Option 2: using list.remove() >>> arr = [5, 7, 2, 3]

Javascript closures and side effects in plain English? (separately)

眉间皱痕 提交于 2019-11-28 18:18:43
I've been reading some JavaScript books and I always hear about closures and side effects. For some reason I can't understand what they really are. Can anyone explain to me what they are in plain English plus examples? (as you were explaining it to someone with the programming level of a graphic designer). Larry OBrien Side effects are the easier concept. A "pure function" is a function that maps its input value(s) into an output value function plus(x, y) { return x + y; } . A "side effect" is any effect other than that return value. So, for instance: function plusWithSideEffects(x, y) { alert

Why is the raising of an exception a side effect?

不羁的心 提交于 2019-11-28 17:46:51
According to the wikipedia entry for side effect , raising an exception constitutes a side effect. Consider this simple python function: def foo(arg): if not arg: raise ValueError('arg cannot be None') else: return 10 Invoking it with foo(None) will always be met with an exception. Same input, same output. It is referentially transparent. Why is this not a pure function? Purity is only violated if you observe the exception, and make a decision based on it that changes the control flow. Actually throwing an exception value is referentially transparent -- it is semantically equivalent to non

Are side effects a good thing?

前提是你 提交于 2019-11-28 05:49:53
I feel the term rather pejorative. Hence, I am flabbergasted by the two sentences in Wikipedia: Imperative programming is known for employing side effects to make programs function. Functional programming in turn is known for its minimization of side effects. [1] Since I am somewhat Math-biased, the latter sounds excellent. What are the arguments for side-effects? Do they mean the loss of control or the acceptance of uncertainty? Are they a good thing? Every so often I see a question on SO which forces me to spend half an hour editing a really bad Wikipedia article. The article is now only

Purity vs Referential transparency

那年仲夏 提交于 2019-11-28 04:37:55
The terms do appear to be defined differently, but I've always thought of one implying the other; I can't think of any case when an expression is referentially transparent but not pure, or vice-versa. Wikipedia maintains separate articles for these concepts and says: From Referential transparency : If all functions involved in the expression are pure functions, then the expression is referentially transparent. Also, some impure functions can be included in the expression if their values are discarded and their side effects are insignificant. From Pure expressions : Pure functions are required

Javascript closures and side effects in plain English? (separately)

时光总嘲笑我的痴心妄想 提交于 2019-11-27 20:18:34
问题 I've been reading some JavaScript books and I always hear about closures and side effects. For some reason I can't understand what they really are. Can anyone explain to me what they are in plain English plus examples? (as you were explaining it to someone with the programming level of a graphic designer). 回答1: Side effects are the easier concept. A "pure function" is a function that maps its input value(s) into an output value function plus(x, y) { return x + y; } . A "side effect" is any

What exactly does “effectful” mean

人盡茶涼 提交于 2019-11-27 18:22:43
Time and again I read the term effectful , but I am still unable to give a clear definition of what it means. I assume the correct context is effectful computations , but I've also seen the term effectful values ) I used to think that effectful means having side effects . But in Haskell there are no side-effects (except to some extent IO). Still there are effectful computations all over the place. Then I read that monads are used to create effectful computations. I can somewhat understand this in the context of the State Monad. But I fail to see any side-effect in the Maybe monad. In general

Why can applicative functors have side effects, but functors can't?

匆匆过客 提交于 2019-11-27 14:54:13
问题 I'm feeling rather silly asking this question, but it's been on my mind for a while and I can't find any answers. So the question is: why can applicative functors have side effects, but functors can't? Maybe they can and I've just never noticed...? 回答1: This answer is a bit of an over-simplification, but if we define side effects as computations being affected by previous computations, it's easy to see that the Functor typeclass is insufficient for side effects simply because there is no way

Why can I call a non-constexpr function inside a constexpr function?

删除回忆录丶 提交于 2019-11-27 14:02:05
Consider the following code: #include <stdio.h> constexpr int f() { return printf("a side effect!\n"); } int main() { char a[f()]; printf("%zd\n", sizeof a); } I would have expected the compiler to complain about the call to printf inside f , because f is supposed to be constexpr , but printf is not. Why does the program compile and print 15 ? Shafik Yaghmour The program is ill-formed and requires no diagnostic according to the C++11 draft standard section 7.1.5 The constexpr specifier paragraph 5 which says: For a constexpr function, if no function argument values exist such that the function