functional-programming

Is there a method reference for a no-op (NOP) that can be used for anything lambda?

我的未来我决定 提交于 2019-12-17 23:26:07
问题 This may sounds like a strange question, but is there a way to refer to a standard no-op (aka null operation, null-pattern method, no-operation, do-nothing method) method for a Lambda in Java 8. Currently, I have a method that takes a, say, void foo(Consumer<Object>) , and I want to give it a no-op, I have to declare: foo(new Consumer<Object>() { public void accept(Object o) { // do nothing } } where I would like to be able to do something like: foo(Object::null) instead. Does something like

Recursive set union: how does it work really?

核能气质少年 提交于 2019-12-17 23:05:28
问题 I am currently taking the Scala course on Coursera on my free time after work, in an attempt to finally give a try to functional programming. I am currently working on an assignment where we are supposed to "calculate" the union of two sets that contain some object. I am intentionally omitting details as it's not really important to what I am trying to ask here. What is relevant, however, is that the sets are defined as binary trees, with each node containing an element, and two subtrees.

Why is the raising of an exception a side effect?

戏子无情 提交于 2019-12-17 22:33:39
问题 According to the wikipedia entry for side effect, raising an exception constitutes a side effect. Consider this simple python function: def foo(arg): if not arg: raise ValueError('arg cannot be None') else: return 10 Invoking it with foo(None) will always be met with an exception. Same input, same output. It is referentially transparent. Why is this not a pure function? 回答1: Purity is only violated if you observe the exception, and make a decision based on it that changes the control flow.

Motivation for Scala underscore in terms of formal language theory and good style?

江枫思渺然 提交于 2019-12-17 22:25:40
问题 Why is it that many people say that using underscore is good practice in Scala and makes your code more readable ? They say the motivation comes from formal language theory. Nevertheless many programmers, particularly from other languages, especially those that have anonymous functions, prefer not to use underscores particularly for placeholders. So what is the point in the underscore? Why does Scala (and some other functional languages as pointed by om-nom-nom) have the underscore? And what

Haskell recursion and memory usage

為{幸葍}努か 提交于 2019-12-17 22:16:49
问题 I'm getting comfortable with the idea of replacing loops with recursion. I'm fiddling around with a pet project, and I wanted to test some text input functionality so I wrote up a little command line interface that repeatedly asks for input until it receives a specific quit command. It looks something like this: getCommandsFromUser = do putStrLn "Enter command: " keyboardInput <- getLine let command = map toLower keyboardInput if command == "quit" then putStrLn "Good-bye!" else do -- stuff --

functional programming: immutable data structure efficiency

南楼画角 提交于 2019-12-17 22:14:06
问题 I don't understand, how FP compilers make the code dealing with immutable data structures fast, not blow up stack, etc. For example, insert operation in tree, it has to copy the whole tree before adding the new node and return the copied tree, versus the imperative couterpart that only needs to add a pointer to the new node. If the insert operation is run millions times, it would take a load of memory, and copying will be slower and slower when the tree is bigger. How do FP compilers actually

Union types and Intersection types

半腔热情 提交于 2019-12-17 21:52:48
问题 What are the various use cases for union types and intersection types? There has been lately a lot of buzz about these type system features, yet somehow I have never felt need for either of these! 回答1: If you want a more practice-oriented answer: With union and recursive types you can encode regular tree types and therefore XML types. With intersection types you can type BOTH overloaded functions and refinement types (what in a previous post is called coherent overloading) So for instance you

What is “Lambda Lifting”?

有些话、适合烂在心里 提交于 2019-12-17 21:45:46
问题 I just ran into this while going through the Erlang compiler source. I'm not really getting it. (go figure ;)), considering that I just realized that there is such a thing 5 minutes ago). Forgive me for asking first without first trying to understand reasons for its existence. There is a wikipedia article about it, but it is pretty cryptic. 回答1: Lambda lifting is used to turn a closure into a pure function. By passing extra arguments to the function, you reduce the number of its free

Object-oriented programming in a purely functional programming context?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 21:43:04
问题 Are there any advantages to using object-oriented programming (OOP) in a functional programming (FP) context? I have been using F# for some time now, and I noticed that the more my functions are stateless, the less I need to have them as methods of objects. In particular, there are advantages to relying on type inference to have them usable in as wide a number of situations as possible. This does not preclude the need for namespaces of some form, which is orthogonal to being OOP. Nor is the

Are FP and OO orthogonal?

回眸只為那壹抹淺笑 提交于 2019-12-17 21:26:05
问题 I have heard this time and again, and I am trying to understand and validate the idea that FP and OO are orthogonal. First of all, what does it mean for 2 concepts to be orthogonal? FP encourages immutability and purity as much as possible, while OO seems built for state and mutation – a slightly organized version of imperative programming? I realize that objects can be immutable, but OO seems to imply state/change to me. They seem like opposites. How does that affect their orthogonality? A