functional-programming

How to early return in Scala [duplicate]

梦想与她 提交于 2020-01-01 10:05:15
问题 This question already has answers here : Explicit return from play action (2 answers) Closed 3 years ago . I am learning Scala at the moment. One thing that I like to do is early returns. I'm convinced that this is far easier for everyone to read as we just remove the invalid states before. Now, as Scala is a functional language and I've read that cutting computation is bad functional style, I'm wondering if there is some trick or functional programming equivalent to early return. This is

Using recursion with map in python

喜夏-厌秋 提交于 2020-01-01 09:40:14
问题 I am trying to learn functional programming concepts. An exercise, Flatten a nested list using map/reduce. My code. lists = [ 1 , 2 , [ 3 , 4, 5], 6, [7, 8, 9] ] def flatten(lists): return map(lambda x: flatten(x) if isinstance(x,list) else x, lists) print flatten(lists) I get output same as input. What did i do wrong ? How recursion works with map() ? 回答1: Here's a solution that uses both map and reduce : def flatten(seq): return reduce(operator.add, map( lambda x: flatten(x) if isinstance(x

Is there any good example of use cases for angular.identity()?

痞子三分冷 提交于 2020-01-01 08:25:11
问题 According to the documentation A function that returns its first argument. This function is useful when writing code in the functional style. I am wondering where I can find a good example of such use case - writing code in functional style in an angular app. Thanks 回答1: Example from the AngularJS source code: function transformer(transformationFn, value) { return (transformationFn || angular.identity)(value); }; Explanation: In case transformationFn is provided as first parameter, it will be

Is there any good example of use cases for angular.identity()?

廉价感情. 提交于 2020-01-01 08:24:12
问题 According to the documentation A function that returns its first argument. This function is useful when writing code in the functional style. I am wondering where I can find a good example of such use case - writing code in functional style in an angular app. Thanks 回答1: Example from the AngularJS source code: function transformer(transformationFn, value) { return (transformationFn || angular.identity)(value); }; Explanation: In case transformationFn is provided as first parameter, it will be

Sum array of arrays (matrix) vertically efficiently/elegantly

醉酒当歌 提交于 2020-01-01 05:35:08
问题 In Javascript, if I have an array of arrays representing a matrix, say x = [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ]; summing it "horizontally" is easy and can be done like x.map(function(y){ return y.reduce(function(a,b){ return a+b; }); }); Now I would like to compute the "vertical" sum, which can be done x.reduce(function(a, b){ return a.map(function(v,i){ return v+b[i]; }); }); But I am not happy with this version and I wish there were something nicer, more elegant and more straightforward.

C# Linq merge two dictionaries [duplicate]

╄→尐↘猪︶ㄣ 提交于 2020-01-01 05:25:08
问题 This question already has answers here : Combine two Dictionaries with LINQ (6 answers) Closed 6 years ago . How to make the following method more functional-linq-style? public static Dictionary<T, T> MergeDict<T, T>(Dictionary<T, T> a, Dictionary<T, T> b) { var e = new Dictionary<T, T>(); a.Concat(b).ToList().ForEach(pair => { e[pair.Key] = pair.Value; }); return e; } 回答1: To continue your duplicate discarding ways, just group up and take a winning item in the group (such as the Last one).

Scheme/Lisp nested loops and recursion

北城以北 提交于 2020-01-01 05:19:32
问题 I'm trying to solve a problem in Scheme which is demanding me to use a nested loop or a nested recursion. e.g. I have two lists which I have to check a condition on their Cartesian product. What is the best way to approach these types of problems? Any pointers on how to simplify these types of functions? I'll elaborate a bit, since my intent might not be clear enough. A regular recursive function might look like this: (define (factorial n) (factorial-impl n 1)) (define (factorial-impl n t)

Is RxJS faster than imperative? [closed]

我怕爱的太早我们不能终老 提交于 2020-01-01 04:44:12
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I'm newer in functional programming and functional reactive programming. I read a lot of times the great power of functional reactive programming . Okey; is readable, avoid side effects, etc. But... I don't know how to improve my code in functional/reactive way to execute faster

How to write monoid protocol in Clojure?

孤人 提交于 2020-01-01 04:43:15
问题 The following does not work, for obvious reasons. (defprotocol Monoid (mappend [a b]) (mzero [])) mzero has zero arguments, and zero argument methods are not allowed (or do not make sense) in protocols. In Haskell or Scala, where the dispatch is type-based rather than value-based, this is not a problem. What would be the correct way to conceptualize and write Monoid protocol in Clojure? 回答1: looking at the source, the way that this is implemented in the new reducers library is not as a

Is there any intuition to understand join two functions in Monad?

前提是你 提交于 2020-01-01 04:34:07
问题 join is defined along with bind to flatten the combined data structure into single structure. From type system view, (+) 7 :: Num a => a -> a could be considered as a Functor , (+) :: Num a => a -> a -> a could be considered as a Functor of Functor , how to get some intuition about it instead of just relying on type system? Why join (+) 7 === 14 ? Even though it is possible to get the final result through manually stepping by the function binding process, it would be great if some intuition