side-effects

Why are assignments not allowed in Python's `lambda` expressions?

只谈情不闲聊 提交于 2019-12-01 03:08:39
This is not a duplicate of Assignment inside lambda expression in Python , i.e., I'm not asking how to trick Python into assigning in a lambda expression. I have some λ-calculus background. Considering the following code, it looks like Python is quite willing to perform side-effects in lambda expressions: #!/usr/bin/python def applyTo42(f): return f(42) def double(x): return x * 2 class ContainsVal: def __init__(self, v): self.v = v def store(self, v): self.v = v def main(): print('== functional, no side effects') print('-- print the double of 42') print(applyTo42(double)) print('-- print 1000

Side effects in Scala

狂风中的少年 提交于 2019-12-01 02:37:06
I am learning Scala right in these days. I have a slight familiarity with Haskell, although I cannot claim to know it well. Parenthetical remark for those who are not familiar with Haskell One trait that I like in Haskell is that not only functions are first-class citizens, but side effects (let me call them actions) are. An action that, when executed, will endow you with a value of type a , belongs to a specific type IO a . You can pass these actions around pretty much like any other value, and combine them in interesting ways. In fact, combining the side effects is the only way in Haskell to

Anything in Guava similar to Functional Java's Effect?

回眸只為那壹抹淺笑 提交于 2019-11-30 15:45:35
问题 I know one of the goals of pure functional programming is to eliminate mutability, and therefore to preclude side-effects. But let's face it, Java is not a functional language even with all of the functional-programming libraries that exist. In fact it seems that some of the FP-libraries know and expect this. For instance in Functional Java, there is the Effect class. In the Jedi FP library, there is the Command interface. This allows you to -- among other things -- apply a command pattern

What are the side effects of using EmptyWorkingSet?

本秂侑毒 提交于 2019-11-30 14:13:05
I'm using the code below to free up memory on some running programs because my own program needs large memory resources to run faster. [DllImport("psapi.dll")] public static extern bool EmptyWorkingSet(IntPtr hProcess); public FreeMem(string programName){ EmptyWorkingSet(Process.GetCurrentProcess().Handle); foreach(Process process in Process.GetProcesses(programName)) { try { EmptyWorkingSet(process.Handle); } catch (Exception) { ... } } } It seems to be working fine, I was able to bring down memory usage of some programs like explorer from 100,000 Kb down to 2,000 Kb. That's pretty good but

apparent side effects of writeln (“:width” specifier causes question marks in output)

自作多情 提交于 2019-11-30 05:56:45
问题 I have the following code (RAD Studio XE2, Windows 7 x64): program letters; {$APPTYPE CONSOLE} {$DEFINE BOO} const ENGLISH_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'; begin {$IFDEF BOO} writeln; {$ENDIF} write(ENGLISH_ALPHABET[1]:3); readln; end. When {$DEFINE BOO} directive is turned off , I have the following (expected) output (spaces are replaced with dots for readability) : ..a When the directive is turned on , I have the following (unexpected) output: // empty line here ?..a instead of

C# Paradigms: Side effects on Lists

我是研究僧i 提交于 2019-11-30 05:17:17
I am trying to evolve my understanding of side effects and how they should be controlled and applied. In the following List of flights, I want to set a property of each flight satisfying a conditions: IEnumerable<FlightResults> fResults = getResultsFromProvider(); //Set all non-stop flights description fResults.Where(flight => flight.NonStop) .Select(flight => flight.Description = "Fly Direct!"); In this expression, I have a side effect on my list. From my limited knowledge I know for ex. "LINQ is used for queries only" and "There are only a few operations to lists and assigning or setting

What are the side effects of using EmptyWorkingSet?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 20:56:44
问题 I'm using the code below to free up memory on some running programs because my own program needs large memory resources to run faster. [DllImport("psapi.dll")] public static extern bool EmptyWorkingSet(IntPtr hProcess); public FreeMem(string programName){ EmptyWorkingSet(Process.GetCurrentProcess().Handle); foreach(Process process in Process.GetProcesses(programName)) { try { EmptyWorkingSet(process.Handle); } catch (Exception) { ... } } } It seems to be working fine, I was able to bring down

Undefined behavior in c/c++: i++ + ++i vs ++i + i++ [duplicate]

风格不统一 提交于 2019-11-29 17:53:48
This question already has an answer here: Why are these constructs using pre and post-increment undefined behavior? 14 answers Imagine that we have the code below: int i = 1; int j = i++ + ++i; I know that this is a Undefined Behavior, because before the semicolon, which is a sequence point, the value of i has been changed more than once. It means that the compiler may have two possibilities even if the precedence of operator plus is Left-to-Right: case 1) take the value of i++ --- value of i is 1 take the value of ++i --- value of i is 2 do the operator plus and assign the result which is 3

Bug or Feature: Kotlin allows to change 'val' to 'var' in inheritance

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 11:19:18
问题 I just started to explore the language Kotlin. I'm struggling with inheritance, var&val and side-effects. If I declare a trait A with a val x and override x in AImpl it is possible to override it as var (see code below). Surprisingly the print() method in A is affected by the reassignment of x even though x is a value in A . Is this a bug or a feature? Code: trait A { fun print() { println("A.x = $x") } val x : Int; } class AImpl(x : Int) : A { override var x = x; // seems like x can be

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

一笑奈何 提交于 2019-11-28 23:21:46
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...? 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 to chain multiple computations. class Functor f where fmap :: (a -> b) -> f a -> f b The only thing a