Questions tagged [purely-functional]

Purely Functional is a term in computer science used to describe algorithms, data structures, or programming languages that do not allow modification of data at run-time.

Purely Functional is a term in computer science used to describe algorithms, data structures, or programming languages that do not allow modification of data at run-time.

A pure function is a function whose returned value depends on nothing other than the arguments, and that causes no side effects; it contains no mutable hidden states. One consequence is that given the same arguments it will return the same value no matter when it is called, or how many times it is called, in the course of the program. In other words, such a function call (a "pure expression") has a value that does not depend on history or context.

Related concepts:

243 questions
1
vote
1 answer

Repeated application of parameters from a stream to a fluent builder method

I'm writing a REST service client using the Jax-RS. For a request I'd like to add query parameters. The according method in Jax-RS is webTarget.queryParam(name, value) returning a new WebTarget instance (so webtarget is immutable). Further, I have a…
1
vote
3 answers

More pythonic way of filtering out stable values in a list

I wrote a function that allows me to run through a list, compare the values with the predecessors, and assert at which point the list becomes "stable" for a certain amount of entries. The values in the list represent a signal, that may or not reach…
1
vote
1 answer

Functional Programming Purity Requirements for Hash functions

I am following this guide to begin learning functional programming w/ Javascript: https://medium.com/@cscalfani/so-you-want-to-be-a-functional-programmer-part-1-1f15e387e536#.iynj38h83 It defines a Pure function as: Operate only on input…
1
vote
2 answers

Recursion to enumerate an infinite tree left to right

I have a tree represented by type LazyTree<'T> = | LazyBranch of 'T * ('T LazyTree list Lazy) I want to assign a number n to each node, counting left to right. More formally Edited: For a node a, h(a)=0 if a has no children For a node a with…
user2136963
  • 2,526
  • 3
  • 22
  • 41
1
vote
0 answers

Algorithm for determining benefits of function memoization

Problem Let's say I have some programming language with only referentially transparent functions. It's well-known that any of these functions can then be memoized. However, it's not always worth it in terms of time or space to memoize functions.…
1
vote
5 answers

Optionally invoke side effects in a pure function

How can I refactor this long method of legacy Java code, loaded with side effects, into a more pure version? public Result nonPureMethod(String param1, String param2){ this.current_status = "running"; String s1 = step1(param1, param2); …
Dave
  • 2,735
  • 8
  • 40
  • 44
1
vote
0 answers

Unpure function from only pure ones

I wanted to try a mental experiment of creating a stateful function by using only pure ones, without using any sorts of assignment/monads and such. For instance, a function resembling a RS-flipflop, which has Set and Reset inputs: ff(1,0) -> 1 ;…
artemonster
  • 744
  • 4
  • 27
1
vote
1 answer

How can I rewrite this Scala program to be more functional?

I am solving a programming exercise that goes like this: A file contains a list of equalities between positive integers sums, one for each line, each one terminated by a semicolon, without whitespaces. These equalities can either be right or be…
1
vote
1 answer

PHP pass objects by value

I'd like to implement a pure function in PHP How do I pass an object by value and not by reference? In other words, this is the expected output: function change($obj) { $obj->set_value(2); } $obj = new…
1
vote
1 answer

Check Contents of Python Package without Running it?

I would like a function that, given a name which caused a NameError, can identify Python packages which could be imported to resolve it. That part is fairly easy, and I've done it, but now I have an additional problem: I'd like to do it without…
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
1
vote
1 answer

Does [Pure] have any implications other than "no visible side-effects" to Code Contracts?

The PureAttribute documentation says: Indicates that a type or method is pure, that is, it does not make any visible state changes Is this the only requirement of a Pure function under in Microsoft Code Contracts? And; does this model assume that…
user2864740
  • 60,010
  • 15
  • 145
  • 220
1
vote
2 answers

Modulo function in Clean

Is there a predefined way to compute the modulo of two integers in Clean? StdOverloaded defines the (mod) typeclass, but StdInt does not contain an instance of it, and StdEnv does not either anywhere else. I have seen a language overview about Clean…
1
vote
1 answer

Cope with Bacon.js's side effects

I'm a newbie to the Bacon.js, usually write programs in Haskell. By my experience with Haskell, I want to describe some situations in Bacon.js as purely-functional-like approach. Here is an example situation. triggerStream is a source…
user3749167
  • 161
  • 6
1
vote
2 answers

Fold left and fold right

I am trying to learn how to use fold left and fold right. This is my first time learning functional programming. I am having trouble understanding when to use fold left and when to use fold right. It seems to me that a lot of the time the two…
jcm
  • 5,499
  • 11
  • 49
  • 78
1
vote
2 answers

Scala Double epsilon calculation in a functional style

A suggested approach to calculate machine epsilon using Java is as follows, private static float calculateMachineEpsilonFloat() { float machEps = 1.0f; do machEps /= 2.0f; while ((float) (1.0 + (machEps / 2.0)) != 1.0); …
elm
  • 20,117
  • 14
  • 67
  • 113