Questions tagged [pure-function]

A function that always evaluates to the same result value given the same argument value(s) and that does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.

124 questions
2
votes
1 answer

Can pure functions mutate other properties inside it's container class using `this`?

I know that pure functions shouldn't mutate state that's not passed in as a parameter but I don't know if the this keyword is an exception to that rule. Here is a simple example of what I'm thinking: class Car { color: string = 'red'; …
2
votes
1 answer

C++ class constructors qualified as __attribute__((pure)) or __attribute__((const))

Can and should C++ class constructors be declared __attribute__((pure)) if they only can reach data via its parameters? And in which cases should they be qualified as __attribute__((const))?
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
2
votes
1 answer

Calling non-pure function in list comprehension

I have the following code (simplified): def send_issue(issue): message = bot.send_issue(issue) return message def send_issues(issues): return [send_issue(issue) for issue in issues] As you see, send_issues and send_issue are non-pure…
rominf
  • 2,719
  • 3
  • 21
  • 39
2
votes
2 answers

Calculate total duration for properties in object

I've got the following object in an array[0]: var arr[0]= [ { "startTime": "1300", "endTime": "1700", "eventName": "Sleep", "end_datetime": "20180510M0100", "start_datetime": "20180509M2300", }, { "startTime": "0800", …
user21
  • 1,261
  • 5
  • 20
  • 41
2
votes
1 answer

Multiple inline pure function calls using JavaScript...?

I'm scratching my head about solving a problem within JS and pure functions. The one below is an example of impure function but useful to understand what it does. function fn(some) { var ret = 'g', mid = 'o'; if (some) return ret +…
MrJs0n
  • 95
  • 2
  • 4
2
votes
1 answer

Can GCC emits a warning/error if a pure function calls a "not pure" function?

I have discovered the gcc attribute pure and likes to use it, since it seems to me a good way to add additional information in my interface, (alongside the const keyword), and if I have understood its purpose correctly, will allow my compiler to…
VannTen
  • 441
  • 2
  • 12
2
votes
1 answer

Functional code in assembly? (pure functions)

I'm currently working on chess in TASM 16bit. I recently learned about pure functions and how they are the coolest thing in the universe, so my question is, Should go out of my way to make my functions pure and self contained with no side…
2
votes
1 answer

Meaning of the Sharp Sign in this Mathematica Split Function

I have some problems understanding sharp signs in Mathematica. I understand # & as a placeholder for variables. But how are #1 and #2 understood in the following code? x = {0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1}; Split[x, #1 =!= 0 && #2 =!= 0 &] What…
2
votes
1 answer

GCC error: function might be candidate for attribute ‘pure’ if it is known to return normally

Given this code: #include void func(int x) { if (x) abort(); }; g++ -Werror=suggest-attribute=pure complains: error: function might be candidate for attribute ‘pure’ if it is known to return normally This seems strange to…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
2
votes
2 answers

Why is default behavior of Akka Actor to process messages one after the other?

I have read that an akka actor processes messages one after the other. Why is this? What Im unable to wrap my head around is “Why is synchronized execution of messages the default behavior?”. I do understand that for parallel execution of mailbox…
Arpan Patro
  • 153
  • 2
  • 8
2
votes
1 answer

Does Clojure's `memoize` function useful in caching slow sql queries and complex computations?

My project has a mysql database and a backend which is written by Clojure. Some table of the database is updated only one time every day, to query the newest information we will use. But: the database is very large and network is slow, so every…
cmal
  • 2,062
  • 1
  • 18
  • 35
2
votes
1 answer

React, pure function warning?

I am trying to learn react and functional programming by trying to implement a simple todo app. I am not using flux as I am just trying to see the concepts of passing information between parent and children. I am trying to trigger a function in the…
2
votes
0 answers

Prefer pure function than React Component?

I'm working on a HTMl5 video player for a French company. We use React and Redux to build the UI, and it works very well, it's very pleasant to code ! We currently use eslint-plugin-react to check React code style. Since last versions, the linter…
2
votes
1 answer

Do creating value based on current state or based on defined value inside reducer makes a pure function impure?

I read on redux-devtools walkthrough: Your reducers have to be pure and free of side effects to work correctly with DevTools. For example, even generating a random ID in reducer makes it impure and non-deterministic. Instead, do this in action…
Mas Bagol
  • 4,377
  • 10
  • 44
  • 72
2
votes
2 answers

GCC `__attribute__ ((pure))` suggestion on "input state" getter method - correct?

Compiling with -Wsuggest-attribute=pure makes GCC suggest potential functions that can be marked with __attribute__ ((pure)) for optimization purposes. Here's the definition of pure on GCC's documentation: Many functions have no effects except the…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
1 2 3
8 9