functional-programming

It is better to have a caching mechanism inside or outside of a Factory class?

梦想与她 提交于 2019-12-22 04:13:36
问题 my question here is not strictly language related, it is more a general programming concept. If I have a Factory class that has a method to return Parser objects, and these parser classes ,I know, do not need to be instantiated more than once per iteration cycle (outside of the factory, of course). It is better, in terms of use and object separation to create a caching mechanisms for all the instantiated Parsers inside of the Factory, ie: during the method call, or outside of it, when the

Is it recommended to always have exhaustive pattern matches in Haskell, even for “impossible” cases?

微笑、不失礼 提交于 2019-12-22 02:47:08
问题 Is it recommended to always have exhaustive pattern matches in Haskell, even for "impossible" cases? For example, in the following code, I am pattern matching on the "accumulator" of a foldr. I am in complete control of the contents of the accumulator, because I create it (it is not passed to me as input, but rather built within my function). Therefore, I know certain patterns should never match it. If I strive to never get the "Pattern match(es) are non-exhaustive" error, then I would place

Function signature of Tap (K-combinator)

前提是你 提交于 2019-12-21 21:43:23
问题 I've read in a book that the function signature of tap function (also called K-Combinator) is below: tap :: (a -> *) -> a -> a "This function takes an input object a and a function that performs some action on a. It runs the given function with the supplied object and then returns the object." Can someone help me to explain what is the meaning of star (*) in the function signature? Are below implementation correct? If all the three implementation are correct, which one should be used when?

Getting Unique Paths from list of tuple

一个人想着一个人 提交于 2019-12-21 20:54:44
问题 Given a tuple of lists, I need to find all unique path from that: Example I/P: [(1,2),(2,3),(3,4),(9,11),(4,5),(5,6),(6,7),(3,9)] O/P: [[(1,2),(2,3),(3,4),(4,5),(5,6),(6,7)],[(1,2),(2,3),(3,9),(9,11)]] Two tuples can connect if the second element of the tuple matches with the first element of the other tuple i.e: One tuple is (_,a) and other tuple is like (a,_) . What is the most efficient implementation for this ? I need to find the best data structure suited for it. Any suggestions ? The

Ramda: Is there a way to 'fork' a parameter to two functions during pipe?

心不动则不痛 提交于 2019-12-21 20:49:33
问题 I'm a functional programming beginner. I'm working on a React Native app using Ramda. The app lets users maintain their houses. I have written function called asyncPipe which lets me pipe promises and normal functions. I use it for the loginFlow which currently has a http request ( getHouseList ) as its last function. const asyncPipe = (...fns) => x => fns.reduce(async (y, f) => f(await y), x); const loginFlow = asyncPipe( // ... someFunctions getHouseList ); // used later like this in

Understanding nested lambda function behaviour in python

China☆狼群 提交于 2019-12-21 20:47:35
问题 I'm trying to learn pure functional programming. But this code is puzzling me particularly the second line. I do not understand how the value 2 is passed to the variable x . Can somebody explain this nested lambda behaviour? >>> square_func = lambda x: x**2 >>> function_product = lambda F, m: lambda x: F(x)*m >>> square_func(2) 4 >>> function_product(square_func, 3)(2) 12 回答1: The inner lambda creates a function when the outer lambda is called. The outer lambda returns this function. This

Lazy CSV Filtering / Parsing - Increasing Performance

有些话、适合烂在心里 提交于 2019-12-21 20:39:35
问题 Lazy Filtering CSV Files I had the need to filter through millions of log records, stored as numerous CSV files. The size of the records greatly exceeded my available memory so I wanted to go with a lazy approach. Java 8 Streams API With jdk8 we have the Streams API which paired with Apache commons-csv allows us to easily accomplish this. public class LazyFilterer { private static Iterable<CSVRecord> getIterable(String fileName) throws IOException { return CSVFormat .DEFAULT

Is it possible to do functional programming in a language without functions?

ⅰ亾dé卋堺 提交于 2019-12-21 20:17:44
问题 In this comment, it was stated that Ruby doesn't have functions, only methods. If Ruby doesn't have functions, is it not possible to do functional programming in it? Or am I confused about the term "function"? I mean "functional programming" in the sense of functions as first class objects, not as in prohibiting mutable state. 回答1: Yes. Method vs function is quite a fine distinction. It's easy to view each particular method implementation as a function; just have it take as an extra parameter

Point-free style with objects/records in F#

本秂侑毒 提交于 2019-12-21 20:12:23
问题 I'm getting stymied by the way "dot notation" works with objects and records when trying to program in a point-free functional style (which I think is a great, concise way to use a functional language that curries by default). Is there an operator or function I'm missing that lets me do something like: (.) object method instead of object.method ? (From what I was reading about the new ? operator, I think it works like this. Except it requires definition and gets into the whole dynamic binding

Is there a name for ADT with explicit subtyping?

最后都变了- 提交于 2019-12-21 20:07:54
问题 I'm looking for a proper name for a data type that combines ADT with explicit subtyping. In one of my applications, I use a structure similar to ADT to represent parse trees, on which I perform recursive pattern matching. I find it rather convenient if I could combine ADT with subtyping, as demonstrated in the example below: Note: the example is written in Haskell's syntax, but this is not Haskell code. data Empty = Empty data Expr = Int Int | Add Expr AddOp Expr data OptionalExpr = | Empty /