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
3
votes
4 answers

What exactly is a pure function when we are talking about a function within a function

I've learned that a pure function is a function that doesn't alter global state, period. If this is true, functions within functions can alter the state of the outer function and still be pure, correct? Example: function func1() { let name = "My…
user11384784
3
votes
2 answers

Functional Programming: How to handle complex data without bloated functions?

Lets say in your program you have defined a complex car object. That object holds a very long list of predefined key value pairs (wheels,engine,color, lights, amountDoors etc.), each being either a part number or a list of part number, or a specific…
3
votes
3 answers

Explicit Return in Powershell

I can write the following code in javascript: function sum(num1, num2) { return num1 + num2; } and then get a value var someNum = sum(2,5); I would like to do the same thing in Powershell, but I read the following guide: PowerShell also knows…
VSO
  • 11,546
  • 25
  • 99
  • 187
3
votes
1 answer

What is the best practice for Handling Log message with using functional way

I'm new in scala programming. I'm puzzled now for how to declare a biz method in a asynchronous and functional way, that the method implementation should include many log message. As a bad practice, I write the code like this : // trait trait…
3
votes
1 answer

Can a pure function use a private constant variable inside the same class?

Can a pure function use a private constant variable inside the same class? for example: class TimesThousand { const CONSTANT = 1000; function calculate(number) { return number * CONSTANT; } } can calculate() be considered as a pure…
bbnn
  • 3,505
  • 10
  • 50
  • 68
2
votes
2 answers

What does pure function returning pure mean?

What does it mean for a pure function to return pure? pure int doubleMe(in int i) pure { return i * 2; } The code compiles without giving redundant storage class pure, so I suppose this is not a bug? // DMD 2.056
Arlen
  • 6,641
  • 4
  • 29
  • 61
2
votes
1 answer

How to implement db capability approach with pure functions in F#?

I have a code sample like this: type Db = | Db of Map let get id db = let (Db dict) = db dict |> Map.tryFind id |> (fun x -> x, db) let add id value db = let (Db dict) = db dict |> Map.add id…
XaLVa
  • 27
  • 3
2
votes
1 answer

Member method calls virtual method with same name but different signature

I have the following Header/Source files: // foo.h #ifndef __FOO_H #define __FOO_H #include #include template class FooBase { public: std::map a; FooBase(std::map a_) : a(a_) {}; T…
flonk
  • 3,726
  • 3
  • 24
  • 37
2
votes
1 answer

Terser: Annotate function as always pure (and what does it mean)

To assist with optimization Terser supports the pure comment to indicate that a function call is pure. Specifically, the docs give only the following example. const x = /*#__PURE__*/i_am_dropped_if_x_is_not_used() But, usually, if I define a pure…
Peter Gerdes
  • 2,288
  • 1
  • 20
  • 28
2
votes
2 answers

Scala compiler: detecing a pure/impure function

In FP languages like Scala, Haskell etc. pure functions are used which makes it possible for compiler to optimize the code. For eg: val x = method1()// a pure function call val y = method2// another pure function call val c = method3(x,y) As…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
2
votes
1 answer

pure function of functions that returns functions in D

I'm trying to create a pure function that returns the multiplication of two other pure functions: pure Func multiplyFunctions(Func,Real)(scope const Func f1, scope const Func f2) { return (Real a) { return f1(a) * f2(a); }; } Unfortunately, I'm…
Andrew Spott
  • 3,457
  • 8
  • 33
  • 59
2
votes
1 answer

c++: is there a way to ensure the compiler that calling twice the same const method gives the same result?

I am puzzled with the following 2 simple snippets: #include struct A{ int foo(int i) const {return v[i];} std::vector v; }; int f(const A &a, int i) { int j; j=a.foo(i); j=a.foo(i); return j; } which gives…
janou195
  • 1,175
  • 2
  • 10
  • 25
2
votes
0 answers

Persistant data, pure functions and RAM

I'm currently reading Eloquent JavaScript, and I don't really understand the interest of using persistent data structures like indicated in this paragraph. If I get it right, we are using pure functions (methods?) in this example, as the this.move…
Alan Kersaudy
  • 739
  • 7
  • 15
2
votes
1 answer

Memoization in Haskell

Context: def fib(n): if n < 2: return 1 return fib(n-1) + fib(n-2) can be sped up by memoization: fib_memo = {} def fib(n): if n < 2: return 1 if not fib_memo.has_key(n): fib_memo[n] = fib(n-1) + fib(n-2) return…
2
votes
1 answer

What strategy to turn non-pure functions into a pure functions in JavaScript

I'm starting to learn functional programming in javascript. This might be a silly question but what I'm trying to solve a non-pure function written in a functional way. My question is what strategy should be used to accomplish this in a functional…
per.eight
  • 428
  • 7
  • 17
1 2
3
8 9