pure-function

How is this pure function able to modify non-private state?

余生长醉 提交于 2019-12-07 05:08:15
问题 TDPL, p. 167: as long as the mutable state in a function is entirely transitory (i.e., allocated on the stack) and private (i.e., not passed along by reference to functions that may taint it), then the function can be considered pure. import std.stdio : writeln; struct M{ int[4] _data; pure ref int opIndex(size_t i){ return _data[i]; } } pure M foo(ref M m){ m[0] = 1234; return m; } void main(){ M m1 = M([7, 7, 7, 7]); writeln(m1); foo(m1); writeln(m1); } // output: // M([7, 7, 7, 7]) // M(

Is print in Haskell a pure function?

让人想犯罪 __ 提交于 2019-12-07 00:31:43
问题 Is print in Haskell a pure function; why or why not? I'm thinking it's not, because it does not always return the same value as pure functions should. 回答1: A value of type IO Int is not really an Int . It's more like a piece of paper which reads "hey Haskell runtime, please produce an Int value in such and such way". The piece of paper is inert and remains the same, even if the Int s eventually produced by the runtime are different. You send the piece of paper to the runtime by assigning it

Is print in Haskell a pure function?

ぐ巨炮叔叔 提交于 2019-12-05 04:16:16
Is print in Haskell a pure function; why or why not? I'm thinking it's not, because it does not always return the same value as pure functions should. danidiaz A value of type IO Int is not really an Int . It's more like a piece of paper which reads "hey Haskell runtime, please produce an Int value in such and such way". The piece of paper is inert and remains the same, even if the Int s eventually produced by the runtime are different. You send the piece of paper to the runtime by assigning it to main . If the IO action never comes in the way of main and instead languishes inside some

How to test a tree of pure function calls in isolation?

你离开我真会死。 提交于 2019-12-04 09:32:44
问题 In our team of JavaScript devs we have embraced redux/react style of writing pure functional code. However, we do seem to have trouble unit testing our code. Consider the following example: function foo(data) { return process({ value: extractBar(data.prop1), otherValue: extractBaz(data.prop2.someOtherProp) }); } This function call depends on calls to process , extractBar and extractBaz , each of which can call other functions. Together, they might require a non-trivial mock for data parameter

Is a function that calls Math.random() pure?

£可爱£侵袭症+ 提交于 2019-12-03 02:54:02
问题 Is the following a pure function? function test(min,max) { return Math.random() * (max - min) + min; } My understanding is that a pure function follows these conditions: It returns a value computed from the parameters It doesn't do any work other than calculating the return value If this definition is correct, is my function a pure function? Or is my understanding of what defines a pure function incorrect? 回答1: No, it's not. Given the same input, this function will return different values.

Is a function that calls Math.random() pure?

随声附和 提交于 2019-12-02 16:26:59
Is the following a pure function? function test(min,max) { return Math.random() * (max - min) + min; } My understanding is that a pure function follows these conditions: It returns a value computed from the parameters It doesn't do any work other than calculating the return value If this definition is correct, is my function a pure function? Or is my understanding of what defines a pure function incorrect? Christian Benseler No, it's not. Given the same input, this function will return different values. And then you can't build a 'table' that maps the input and the outputs. From the Wikipedia