pure-function

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

雨燕双飞 提交于 2021-02-07 09:47:36
问题 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 programming paradigm. const crypto = require('crypto'); const encrypt = (data, publicKey) => { if (publicKey === undefined ) throw 'Missing public key.'; const bufferToEncrypt = Buffer.from(data); const encrypted = crypto.publicEncrypt({ key:

How map work on Options in Scala?

烂漫一生 提交于 2021-01-28 11:44:59
问题 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 => (s: String) => p.matcher(s).matches) Map is the higher-order function that applies a given function to each element of a list. Now I am not getting that how map is working here as per above statement. 回答1: Map is the higher-order function that applies a given

Must a React reducer be a pure function?

ε祈祈猫儿з 提交于 2021-01-27 06:20:26
问题 I wrote a UI element as a function component which uses React's userReducer hook and it seems to run without errors. useReducer references a function I wrote (called, imaginatively, reducer ): const [state, dispatch] = React.useReducer(reducer, inputData, (inputData) => initialState(inputData)); There's state data which is input and output by the reducer function; and there are "managed" UI elements which depend on state , something like ... return ( <div> <div> {state.elements.map(getElement

Pure Functions: Does “No Side Effects” Imply “Always Same Output, Given Same Input”?

ぐ巨炮叔叔 提交于 2020-07-16 16:17:57
问题 The two conditions that define a function as pure are as follows: No side effects (i.e. only changes to local scope are allowed) Always return the same output, given the same input If the first condition is always true, are there any times the second condition is not true? I.e. is it really only necessary with the first condition? 回答1: Here are a few counterexamples that do not change the outer scope but are still considered impure: function a() { return Date.now(); } function b() { return

Pure Functions: Does “No Side Effects” Imply “Always Same Output, Given Same Input”?

笑着哭i 提交于 2020-07-16 16:17:31
问题 The two conditions that define a function as pure are as follows: No side effects (i.e. only changes to local scope are allowed) Always return the same output, given the same input If the first condition is always true, are there any times the second condition is not true? I.e. is it really only necessary with the first condition? 回答1: Here are a few counterexamples that do not change the outer scope but are still considered impure: function a() { return Date.now(); } function b() { return

Why are pure reducers so important in redux?

不羁的心 提交于 2020-02-23 10:12:14
问题 Pure reducers have no side effects and enable things like time-travelling. They make reasoning about application behavior easier. This is intuitive to me. But I cannot articulate WHY pure reducers lead to these positive non-functional attributes. Can someone help me articulate why making reducers side-effect free makes reasoning about application behavior easier? Is it because you are guaranteed to have the exact same state after running the reducers? If so, surely even side-effectful (ie.

pure function of functions that returns functions in D

浪子不回头ぞ 提交于 2019-12-24 01:23:18
问题 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 running into problems, number one, I want to declare f1 and f2 to be pure functions/delegates/classes with opCall defined... which is required because I'm calling them from a pure function. But number two, and what seems to be the most problematic, is

Why are “pure” functions called “pure”? [closed]

早过忘川 提交于 2019-12-17 18:45:46
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . A pure function is one that has no side effects -- it cannot do any kind of I/O and it cannot modify the state of anything -- and it is referentially transparent -- when called multiple times with the same inputs, it always gives the same outputs. Why is the word "pure" used to describe functions with those

How to tell if an F# function is pure?

可紊 提交于 2019-12-08 19:21:01
问题 Suppose I have these two F# functions: let sq x = x*x let tm = DateTime.Now Clearly sq is pure in that it will always return the same value for a given input while tm is impure because it will return a different value each time it is called. In general is there a way to determine if a particular function in F# is pure or impure without analyzing what it does, in other words reading it line by line? Alternatively is there a way to annotate a function to tell the compiler that the function is

Can I restrict a function to be pure in TypeScript?

跟風遠走 提交于 2019-12-08 16:10:12
问题 Is there a way to allow a function to be pure only (thus not accepting the function to be non pure) in TypeScript? If yes, which? 回答1: You might be able to write a few TSLint rules to catch most of the common cases (access to outside variables, for example), but checking something like that is nigh impossible, so there's no way to actually know 100%. You (and your team) still have to be disciplined. 来源: https://stackoverflow.com/questions/45834108/can-i-restrict-a-function-to-be-pure-in