functional-programming

How do I refer to std::sin(const valarray<double> &)?

▼魔方 西西 提交于 2019-12-29 07:43:16
问题 I'm having trouble with some valarray function pointer code: double (*fp)(double) = sin; valarray<double> (*fp)(const valarray<double> &) = sin; The first compiles, the second gives: error: no matches converting function 'sin' to type 'class std::valarray<double> (*)(const class std::valarray<double>&)' 回答1: This compiles, using the __typeof__ GCC extension. Looks like GCC's valarray uses expression templates to delay calculation of the sinus. But that will make the return type of the sin

Haskell Fibonacci Explanation

寵の児 提交于 2019-12-29 07:33:26
问题 I am quite new to Haskell and I'm trying to wrap my head around how the lazy expression of Fibonacci sequences work. I know this has been asked before, but none of the answers have addressed an issue I'm having with visualising the result. The code is the canonical one using zipWith fibs = 0 : 1 : zipWith (+) fibs (tail fibs) I understand the following: zipWith literally zips two lists together tail grabs all but the first element of a list Haskell references 'to-be' computed data as thunks .

Partial Function Evaluation in MATLAB

ぃ、小莉子 提交于 2019-12-29 06:40:25
问题 Is there an idiomatic way to bind variables in a MATLAB function? It seems like it would be fairly common to create a function, bind a few arguments, then pass the new function to an optimizer of some sort (in my case, a Newton solver). It doesn't look like the variable scoping rules permit a solution with nested or inline functions. Should I simply create a class? It doesn't seem like MATLAB has first-class function objects, is this correct? My search kung-fu is coming up short. Thanks! As

Is there an Iteratee-like concept which pulls data from multiple sources?

心已入冬 提交于 2019-12-29 06:13:27
问题 It is possible to pull on demand from a number (say two for simplicity) of sources using streams (lazy lists). Iteratees can be used to process data coming from a single source. Is there an Iteratee-like functional concept for processing multiple input sources? I could imagine an Iteratee whose state signals from which source does it want to pull. 回答1: To do this using pipes you nest the Pipe monad transformer within itself, once for each producer you wish to interact with. For example:

Why future has side effects?

匆匆过客 提交于 2019-12-29 06:13:07
问题 I am reading the book FPiS and on the page 107 the author says: We should note that Future doesn’t have a purely functional interface. This is part of the reason why we don’t want users of our library to deal with Future directly. But importantly, even though methods on Future rely on side effects, our entire Par API remains pure. It’s only after the user calls run and the implementation receives an ExecutorService that we expose the Future machinery. Our users therefore program to a pure

How to get String from Mono<String> in reactive java

可紊 提交于 2019-12-29 05:25:48
问题 I have a method which accepts Mono as a param. All I want is to get the actual String from it. Googled but didn't find answer except calling block() over Mono object but it will make a blocking call so want to avoid using block(). Please suggest other way if possible. The reason why I need this String is because inside this method I need to call another method say print() with the actual String value. I understand this is easy but I am new to reactive programming. Code: public String getValue

What does uncurry ($) do?

隐身守侯 提交于 2019-12-29 05:18:33
问题 I'm doing some excersises where I have to add a function's type and explain what it does. I'm stuck with this: phy = uncurry ($) The type, according to GHCi is phy :: (a -> b, a) -> b . My haskell knowledge is basic so I really have no idea what it does. 回答1: Let's spell out the type part systematically. We'll start with the types of uncurry and ($) : uncurry :: (a -> b -> c) -> (a, b) -> c ($) :: (a -> b) -> a -> b Since the target expression has ($) as the argument of uncurry , let's line

Passing additional parameters in higher-order functions

岁酱吖の 提交于 2019-12-29 03:40:08
问题 Consider this example: const samples = ["foo", "bar"]; const excludeFoos = function(item) { return item !== "foo"; } const foos = samples.filter(excludeFoos); How can I pass an additional parameter in excludeFoos ? For example: const samples = ["foo", "bar"]; const exclude = function(item, str) { return item !== str; } // obviously won't work but you get the point const foos = samples.filter(exclude("foo")); console.log(foos); // ["bar"] 回答1: Naming things "If you have the name of a spirit,

Impredicative types vs. plain old subtyping

北城余情 提交于 2019-12-29 02:47:08
问题 A friend of mine posed a seemingly innocuous Scala language question last week that I didn't have a good answer to: whether there's an easy way to declare a collection of things belonging to some common typeclass. Of course there's no first-class notion of "typeclass" in Scala, so we have to think of this in terms of traits and context bounds (i.e. implicits). Concretely, given some trait T[_] representing a typeclass, and types A , B and C , with corresponding implicits in scope T[A] , T[B]

Scala - Two Lists to Tuple List

社会主义新天地 提交于 2019-12-28 16:33:50
问题 Last year I had quite a bit of experience with standard ML, but I haven't done any real functional programming in about 10 months. Now that I'm on the Scala bandwagon, I'm having trouble finding an operation which I used extensively in standard ML when writing a compiler (although to be fair, this method may not have been a library method). Basically, I have two lists: List("a","b","c") List(1,2,3) And I want an operation that will give me a list of tuples like this: List(("a",1), ("b",2), (