functional-programming

Why foldRight and reduceRight are NOT tail recursive?

自闭症网瘾萝莉.ら 提交于 2019-12-18 20:07:40
问题 Why compiler does not translate Scala (1,2,3,4,5,6).foldRight(10)(_ * _) to Java equivalent final int[] intArray = new int[]{1,2,3,4,5,6}; int accumulator = 10; for(int i = intArray.legth - 1; i >=0; i--) { accumulator = intArray[i] * accumulator; } The question is: Why foldLeft and reduceLeft are tail recursive, but their right counteparts aren't? Here are links which says that right handed aren't tail recursive. I am asking why it is so. How do you know when to use fold-left and when to use

How can I map a String to a function in Java?

狂风中的少年 提交于 2019-12-18 19:38:30
问题 Currently, I have a bunch of Java classes that implement a Processor interface, meaning they all have a processRequest(String key) method. The idea is that each class has a few (say, <10) member Strings , and each of those maps to a method in that class via the processRequest method, like so: class FooProcessor implements Processor { String key1 = "abc"; String key2 = "def"; String key3 = "ghi"; // and so on... String processRequest(String key) { String toReturn = null; if (key1.equals(key))

Swift: Flatten an array of dictionaries to one dictionary

让人想犯罪 __ 提交于 2019-12-18 19:02:32
问题 In Swift, I am trying to flatten an array of dictionaries into one dictionary i.e let arrayOfDictionaries = [["key1": "value1"], ["key2": "value2"], ["key3": "value3", "key4": "value4"]] //the end result will be: flattenedArray = ["key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4"] I have tried using flatmap, but the type of the returned result is [(String, AnyObject)] and not [String, Object] ie let flattenedArray = arrayOfDictionaries.flatMap { $0 } // type is [(String,

In C# is it a good practice to use recursive functions in algorithms? [closed]

十年热恋 提交于 2019-12-18 18:51:36
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . In many functional languages using a recursion is considered to be a good practice. I think it is good because of the way compiler optimizes functional language's code. But is it a good practice to use recursion in C#, when creating an algorithm? Is it right to say in regards

Haskell syntax for 'or' in case expressions

孤者浪人 提交于 2019-12-18 18:49:41
问题 In F#, I can use | to group cases when pattern matching. For example, let rec factorial n = match n with | 0 | 1 -> 1 // like in this line | _ -> n * factorial (n - 1) What's the Haskell syntax for the same? 回答1: There is no way of sharing the same right hand side for different patterns. However, you can usually get around this by using guards instead of patterns, for example with elem. foo x | x `elem` [A, C, G] = ... | x `elem` [B, D, E] = ... | otherwise = ... 回答2: with guards: factorial n

What special rules does the scala compiler have for the unit type within the type system

北城余情 提交于 2019-12-18 18:40:11
问题 The Unit gets special handling by the compiler when generating byte code because it's analogous to void on the jvm. But conceptually as a type within the scala type system, it seems like it also gets special treatment in the language itself (examples below). So my question is about clarifying this and understanding what mechanisms are used and if there really is special treatment for the Unit type. Example 1: For "normal" scala types like Seq , if a method returns Seq , then you must return

How does orElse work on PartialFunctions

久未见 提交于 2019-12-18 15:58:25
问题 I am getting very bizarre behavior (at least it seems to me) with the orElse method defined on PartialFunction It would seem to me that: val a = PartialFunction[String, Unit] { case "hello" => println("Bye") } val b: PartialFunction[Any, Unit] = a.orElse(PartialFunction.empty[Any, Unit]) a("hello") // "Bye" a("bogus") // MatchError b("bogus") // Nothing b(true) // Nothing makes sense but this is not how it is behaving and I am having a lot of trouble understanding why as the types signatures

Does joined() or flatMap(_:) perform better in Swift 3?

僤鯓⒐⒋嵵緔 提交于 2019-12-18 15:48:22
问题 I'm curious about the performance characteristics of joined() and .flatMap(_:) in flattening a multidimensional array: let array = [[1,2,3],[4,5,6],[7,8,9]] let j = Array(array.joined()) let f = array.flatMap{$0} They both flatten the nested array into [1, 2, 3, 4, 5, 6, 7, 8, 9] . Should I prefer one over the other for performance? Also, is there a more readable way to write the calls? 回答1: TL; DR When it comes just to flattening 2D arrays (without any transformations or separators applied,

Does joined() or flatMap(_:) perform better in Swift 3?

牧云@^-^@ 提交于 2019-12-18 15:48:08
问题 I'm curious about the performance characteristics of joined() and .flatMap(_:) in flattening a multidimensional array: let array = [[1,2,3],[4,5,6],[7,8,9]] let j = Array(array.joined()) let f = array.flatMap{$0} They both flatten the nested array into [1, 2, 3, 4, 5, 6, 7, 8, 9] . Should I prefer one over the other for performance? Also, is there a more readable way to write the calls? 回答1: TL; DR When it comes just to flattening 2D arrays (without any transformations or separators applied,

Flipping a function's argument order in Python

為{幸葍}努か 提交于 2019-12-18 15:47:11
问题 Nowadays, I am starting to learn haskell, and while I do it, I try to implement some of the ideas I have learned from it in Python. But, I found this one challenging. You can write a function in Haskell, that takes another function as argument, and returns the same function with it's arguments' order flipped. Can one do similiar thing in Python? For example, def divide(a,b): return a / b new_divide = flip(divide) # new_divide is now a function that returns second argument divided by first