functional-programming

String.IsNullOrEmpty Monad

岁酱吖の 提交于 2020-07-19 10:58:10
问题 I have lately been dipping my feet into the fascinating world of functional programming, largely due to gaining experience in FP platforms like React and reading up on blogs the likes of https://blog.ploeh.dk/. As a primarily imperative programmer, this has been an interesting transition, but I am still trying to get my wet feet under me. I am getting a little tired of using string.IsNullOrEmpty as such. So much of the time I find myself littering my code with expressions such as _ = string

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

summaryStatistics Method of IntSummaryStatistics

╄→гoц情女王★ 提交于 2020-07-16 06:41:43
问题 Why summaryStatistics() method on an empty IntStream returns max and min value of integer as the maximum and minimum int value present in the stream? IntStream intStream = IntStream.of(); IntSummaryStatistics stat = intStream.summaryStatistics(); System.out.println(stat); Output : IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648} What is the point of returning these values? Should not it considered the wrong result? 回答1: See IntSummaryStatistics.getMax()

What are the primary differences between Haskell and F#? [closed]

血红的双手。 提交于 2020-07-14 12:52:09
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Closed 6 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. I've searched on the Internet for comparisons between F# and Haskell but haven't found anything really definitive. What are the primary differences and why would I want to choose one over the other? 回答1: Haskell

What are the primary differences between Haskell and F#? [closed]

时光总嘲笑我的痴心妄想 提交于 2020-07-14 12:51:25
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Closed 6 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. I've searched on the Internet for comparisons between F# and Haskell but haven't found anything really definitive. What are the primary differences and why would I want to choose one over the other? 回答1: Haskell

What are the primary differences between Haskell and F#? [closed]

这一生的挚爱 提交于 2020-07-14 12:51:20
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Closed 6 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. I've searched on the Internet for comparisons between F# and Haskell but haven't found anything really definitive. What are the primary differences and why would I want to choose one over the other? 回答1: Haskell

Rewrite error catching higher order function to catch async errors?

徘徊边缘 提交于 2020-07-09 19:45:00
问题 Here I have a function that works well for catching sync errors, and doing something with them before re-throwing them. function logExceptions<T extends (...args: any[]) => any>(func: T): (...funcArgs: Parameters<T>) => ReturnType<T> { return (...args: Parameters<T>): ReturnType<T> => { try { return func(...args); } catch (err) { console.log(func.name + " caused an error") throw err; } }; } function syncExample() { throw new Error() } logExceptions(syncExample)(); console.log "syncExample

Rewrite error catching higher order function to catch async errors?

ぃ、小莉子 提交于 2020-07-09 19:44:47
问题 Here I have a function that works well for catching sync errors, and doing something with them before re-throwing them. function logExceptions<T extends (...args: any[]) => any>(func: T): (...funcArgs: Parameters<T>) => ReturnType<T> { return (...args: Parameters<T>): ReturnType<T> => { try { return func(...args); } catch (err) { console.log(func.name + " caused an error") throw err; } }; } function syncExample() { throw new Error() } logExceptions(syncExample)(); console.log "syncExample

Return first element of the tuple

浪子不回头ぞ 提交于 2020-07-09 16:55:48
问题 Suppose I create a function that adds two integers : def addInt(a : Int, b: Int) : (Int, Int) = { | val x = a + b | (x,2) | } I'm returning (result, 2) on purpose for the sake of this question. Now I want to create a variable that returns only x. val result = addInt(3,4) for example result would return (7,2) but I only want it to return 7. How can I do this? (Without changing the code of the function obviously). 回答1: val result = addInt(3,4)._1 And if you wanted the 2: val the2 = addInt(3,4).