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
1
vote
2 answers

How map work on Options in Scala?

I have this two functions def pattern(s: String): Option[Pattern] = try { Some(Pattern.compile(s)) } catch { case e: PatternSyntaxException => None } and def mkMatcher(pat: String): Option[String => Boolean] = pattern(pat) map (p =>…
Saurabh kukade
  • 1,608
  • 12
  • 23
1
vote
1 answer

Component should be written as a pure function

I have a react-native android application. My component in index.android.js is stateless so eslint is throwing the error "Component should be written as a pure function". If I make the component as pure function, how do i register the application or…
scn
  • 45
  • 2
  • 9
1
vote
2 answers

Is there a simple way to map nested data with Lodash?

For my current project, I'm working with an API that returns data formatted like this: { groups: [ { items: [ { points: [ { name: "name1", ... }, { name: "name2", ... }, { name:…
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
1
vote
0 answers

Do modern JavaScript engines perform optimizations targeting pure functions?

While advocating pure functions I'd like to also mention performance as an advantage and while I know that traditional functional languages heavily optimize them (I assume everything in haskell is magically memoized) I don't know what the case is…
adrianton3
  • 2,258
  • 3
  • 20
  • 33
1
vote
1 answer

perform actions before garbage collection

i'd like to perform some actions on a particular entity after there are no longer any references to it, but before the garbage collector wipes its data out. i'm using this for an experiment with more "purely functional" gui abstractions. here is…
xen
  • 133
  • 6
1
vote
0 answers

Could PureAttribute only be guaranteed when manipulating primitive types?

JetBrains annotations: Indicates that a method does not make any observable state changes. The same as System.Diagnostics.Contracts.PureAttribute Microsoft Code Contracts: Indicates that a type or method is pure, that is, it does not make any…
Den
  • 1,827
  • 3
  • 25
  • 46
0
votes
2 answers

Can I make a pure Haskell function wait for 2 seconds?

In a Haskell program which I don't understand that well (yet), I would like a function myInfo :: Int -> Picture myInfo mode = ... to take always 2 seconds longer than normal (to slow down the output). I looked up Control.Concurrent.threadDelay,…
halloleo
  • 9,216
  • 13
  • 64
  • 122
0
votes
0 answers

In functional programming, how to convert a function which uses global variables into a pure function?

In the code below, power isn't a pure function (uses two global variables), how can i convert it into a pure function with just one input array as parameter? const multiplyByFour = function (array) { return array.map((item) => item * 4); }; const…
daego
  • 177
  • 1
  • 1
  • 10
0
votes
0 answers

why react UI Frameworks not use memo or PureComponent by defaults

https://github.com/segmentio/evergreen https://github.com/kiwicom/orbit https://github.com/react-bootstrap/react-bootstrap https://github.com/ariakit/ariakit https://github.com/Semantic-Org/Semantic-UI-React None of these well-known libraries use…
0
votes
0 answers

Are there any languages in which you can enforce pure functions?

Languages like Haskell have the concept of pure functions and will not compile if a function that is supposed to be pure is in fact not pure. e.g. if I have processFoo :: (() -> Foo) -> Boolean and pass in getFooFromServer :: () -> IO Foo as the…
Jonny
  • 2,509
  • 23
  • 42
0
votes
2 answers

Is my usage of useEffect to generate array correct here?

I want to generate a 16-length array of random prizes using prizes array that is passed as a prop in Board component, and display them. prizes array - [ { prizeId: 1, name: 'coupon', image: 'img/coupon.svg', }, { …
0
votes
0 answers

what are some potential dependencies or needs in pure functions?

so i’m learning coding from the very beginning and i’m having trouble understanding potential dependencies or needs in pure functions. I have a workbook that says- remember that pure functions cannot have any side effects. one way to make functions…
0
votes
1 answer

Can a React usestate update be nested in another useState functional update?

There are n items the user can vote on. There are two React states: votes is an array of the user's votes on every item. const [votes, setVotes] = useState({}); voteChanges indicates how many times the user changed their votes on any of the…
1man
  • 5,216
  • 7
  • 42
  • 56
0
votes
0 answers

Can Javascript Class methods still be considered "pure", if they call other "pure" class methods?

Consider class Service { methodA(input: string[]): number { const resB = this.methodB(input); return resB * 2; } methodB(input: string[]): number { return input.length; } } If MethodB is a pure function, can…
Han Che
  • 8,239
  • 19
  • 70
  • 116
0
votes
0 answers

Will it be possible in the foreseeable future to enforce purity/referential transparency in C++?

I think I know what refertially transparent and pure mean. However here's a question about the two properties and how they differ. As regards how referential transparency and/or purity are enforced in a language, I don't know much. (Not even this…
1 2 3
8 9