functional-programming

Removing syntactic sugar: List comprehension in Haskell

懵懂的女人 提交于 2019-12-17 12:16:34
问题 Can I unsugar list comprehension in this expression: [(i,j) | i <- [1..4], j <- [i+1..4]] This is the output: [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)] How can I, with map, filter and so on, write that piece of code? edit Here an other: [(i,j,k) | i <- [1..6], j <- [i+1..6],k <- [j+1..6]] This is the output: [(1,2,3),(1,2,4),(1,2,5),(1,2,6),(1,3,4),(1,3,5),(1,3,6),(1,4,5),(1,4,6),(1,5,6),(2,3,4),(2,3,5),(2,3,6),(2,4,5),(2,4,6),(2,5,6),(3,4,5),(3,4,6),(3,5,6),(4,5,6)] 回答1: List comprehensions (in

Removing syntactic sugar: List comprehension in Haskell

﹥>﹥吖頭↗ 提交于 2019-12-17 12:16:23
问题 Can I unsugar list comprehension in this expression: [(i,j) | i <- [1..4], j <- [i+1..4]] This is the output: [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)] How can I, with map, filter and so on, write that piece of code? edit Here an other: [(i,j,k) | i <- [1..6], j <- [i+1..6],k <- [j+1..6]] This is the output: [(1,2,3),(1,2,4),(1,2,5),(1,2,6),(1,3,4),(1,3,5),(1,3,6),(1,4,5),(1,4,6),(1,5,6),(2,3,4),(2,3,5),(2,3,6),(2,4,5),(2,4,6),(2,5,6),(3,4,5),(3,4,6),(3,5,6),(4,5,6)] 回答1: List comprehensions (in

How to use Math.max, etc. as higher-order functions

家住魔仙堡 提交于 2019-12-17 12:16:08
问题 In short, this works: [1, 2, 3].reduce(function (a, b) { return Math.max(a, b); }); => 3 But this doesn't: [1, 2, 3].reduce(Math.max); => NaN Pure puzzlement. This is in Firefox 3.5.9, which I presume is using the mozilla standard implementation of reduce, FWIW. 回答1: Math.max can be used as a higher-order function. The problem is .reduce will call the function with 4 arguments: Math.max(accumulator, value, index, the_array) here is the_array is an array, so Math.max returns NaN. I don't think

While or Tail Recursion in F#, what to use when?

混江龙づ霸主 提交于 2019-12-17 10:38:14
问题 Ok, only just in F# and this is how I understand it now : Some problems are recursive in nature (building or reading out a treestructure to name just one) and then you use recursion. In these cases you preferably use tail-recursion to give the stack a break Some languagues are pure functional, so you have to use recursion in stead of while-loops, even if the problem is not recursive in nature So my question : since F# also support the imperative paradigm, would you use tail recursion in F#

While or Tail Recursion in F#, what to use when?

让人想犯罪 __ 提交于 2019-12-17 10:38:00
问题 Ok, only just in F# and this is how I understand it now : Some problems are recursive in nature (building or reading out a treestructure to name just one) and then you use recursion. In these cases you preferably use tail-recursion to give the stack a break Some languagues are pure functional, so you have to use recursion in stead of while-loops, even if the problem is not recursive in nature So my question : since F# also support the imperative paradigm, would you use tail recursion in F#

Handling incremental Data Modeling Changes in Functional Programming

安稳与你 提交于 2019-12-17 10:32:43
问题 Most of the problems I have to solve in my job as a developer have to do with data modeling. For example in a OOP Web Application world I often have to change the data properties that are in a object to meet new requirements. If I'm lucky I don't even need to programmatically add new "behavior" code (functions,methods). Instead I can declarative add validation and even UI options by annotating the property (Java). In Functional Programming it seems that adding new data properties requires

Handling incremental Data Modeling Changes in Functional Programming

大兔子大兔子 提交于 2019-12-17 10:32:14
问题 Most of the problems I have to solve in my job as a developer have to do with data modeling. For example in a OOP Web Application world I often have to change the data properties that are in a object to meet new requirements. If I'm lucky I don't even need to programmatically add new "behavior" code (functions,methods). Instead I can declarative add validation and even UI options by annotating the property (Java). In Functional Programming it seems that adding new data properties requires

Applicative functor evaluation is not clear to me

心不动则不痛 提交于 2019-12-17 09:58:12
问题 I am currently reading Learn You a Haskell for Great Good! and am stumbling on the explanation for the evaluation of a certain code block. I've read the explanations several times and am starting to doubt if even the author understands what this piece of code is doing. ghci> (+) <$> (+3) <*> (*100) $ 5 508 An applicative functor applies a function in some context to a value in some context to get some result in some context. I have spent a few hours studying this code block and have come up

Any Functional Programming method of traversing a nested dictionary?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 09:53:03
问题 I am trying to find a better way to implement this: d = {"a": {"b": {"c": 4}}} l = ["a", "b", "c"] for x in l: d = d[x] print (d) # 4 I am learning functional programming so I am just trying random example that come to my head :) 回答1: Use reduce(): reduce(dict.__getitem__, l, d) or better still, using operator.getitem(): from operator import getitem reduce(getitem, l, d) Demo: >>> d = {"a": {"b": {"c": 4}}} >>> l = ["a", "b", "c"] >>> from operator import getitem >>> reduce(getitem, l, d) 4

Convert Swift Array to Dictionary with indexes [duplicate]

♀尐吖头ヾ 提交于 2019-12-17 09:48:18
问题 This question already has answers here : Error: Immutable value passed on reduce function (3 answers) Closed 4 years ago . I'm using Xcode 6.4 I have an array of UIViews and I want to convert to a Dictionary with keys "v0", "v1"... . Like so: var dict = [String:UIView]() for (index, view) in enumerate(views) { dict["v\(index)"] = view } dict //=> ["v0": <view0>, "v1": <view1> ...] This works, but I'm trying to do this in a more functional style. I guess it bothers me that I have to create the