functor

Nested applicative functors of different types in Haskell

纵然是瞬间 提交于 2019-12-24 03:41:11
问题 I'd like to make the nested applicative functors of different types. For example, nested simple functors of different types (in ghci) work fine: Prelude> ((+2) <$>) <$> (Just [1..4]) Just [3,4,5,6] But for applicative functors of different types: Prelude> ((*) <$>) <$> (Just [1,2,3]) <*> (Just [4,5,6,7]) <interactive>:56:1: error: * Couldn't match type `[Integer -> Integer]' with `[Integer] -> b' isn't working! I want to obtain something like this: Just [4,5,6,7,8,10,12,14,12,15,18,21] I know

Passing a closure as a parameter to a constructor c++

↘锁芯ラ 提交于 2019-12-24 02:55:09
问题 In the code below, I'd like to get rid of the weird constructors taking various types of function pointers, and the parameter lists that have to be saved off (along with all the member variables that are required to hold all this), and instead use a closure to do all of that, leaving Event with a single member variable that is something like Closure closure; . Is there a way to do this using closures in C++0x? #include <iostream> #include <list> #include <functional> #include <algorithm>

Functor for Choosing Between Two Functions

℡╲_俬逩灬. 提交于 2019-12-24 02:11:04
问题 Googling for C++ functor syntax brings a lot of different results and I don't think I see what need in any of them. Some use templates, some use a single class, others multiple, and still others use structs instead. I'm never sure what specific elements I need for what I want to do. So now, what I want to do: I have two functions. They both take the exact same parameters, but are named differently and will return a different result, though the return type is also the same, e.g., unsigned foo1

pure function of functions that returns functions in D

浪子不回头ぞ 提交于 2019-12-24 01:23:18
问题 I'm trying to create a pure function that returns the multiplication of two other pure functions: pure Func multiplyFunctions(Func,Real)(scope const Func f1, scope const Func f2) { return (Real a) { return f1(a) * f2(a); }; } Unfortunately, I'm running into problems, number one, I want to declare f1 and f2 to be pure functions/delegates/classes with opCall defined... which is required because I'm calling them from a pure function. But number two, and what seems to be the most problematic, is

Lambda expression as member functors in a class

烂漫一生 提交于 2019-12-24 00:35:08
问题 I was thrilled when lambda expressions (LE) were part of the gcc starting a 4.5.1 and hoped they would grant a way of getting rid of those nasty functions pointer in C++, which were basically, to my understanding, compiled as C functions. All those static declarations etc... Now I wanted to use LEs in a class, where one can choose a method of computation by a functor. But due to the definition in the proposal for C++1x, this seems not to be possible at all. Here the code and the problem(s).

Why am I getting this 'redeclared as different kind of symbol' error?

可紊 提交于 2019-12-23 23:15:05
问题 I have a functor like this, class PrintParentheses { public: PrintParentheses(unsigned pairsCount) {} void operator ()() {} }; Inside main() I am using it like, #include <iostream> int main() { unsigned pairsCount = 0; // Error: ‘PrintParentheses pairsCount()’ redeclared as different kind of symbol PrintParentheses(pairsCount)(); PrintParentheses(5)(); // But this works } Error positions are marked inside the code itself. I have tested both GCC-4.6 and clang-3.1 . Both are giving the same

Implementing the (typed) K combinator in C++

爷,独闯天下 提交于 2019-12-23 18:27:11
问题 I am trying to implement the K combinator from the SK combinator calculus in C++. The K combinator is a higher-order function that basically takes some value x , and returns something which in turn takes a value y and returns x from it. In other words, K(x)(y) == x or step-by-step: intermediate = K(x) intermediate(y) == x The ability to treat K(x) as a thing-in-itself, independent of y , is essential. Furthermore, it should not be necessary to specify the type of y when simply creating K(x)

Uniform random distribution “base class” for both int and double?

人走茶凉 提交于 2019-12-23 17:09:46
问题 I'm trying to make a function that will fill a list with random numbers, and based on the type of the list items it should generate either integer or floating point numbers. So far I've come up with the following code, and it works: template <typename T> void generateRandom(list<T>& numberList){ default_random_engine randomGenerator(random_device{}()); if( typeid(T) == typeid(int) ){ uniform_int_distribution<T> distribution(1000, 2000); auto myGenerator = bind(distribution, randomGenerator);

x <*> y <$> z in Haskell

僤鯓⒐⒋嵵緔 提交于 2019-12-23 11:54:39
问题 I'm trying to understand some Haskell source code, and I encountered this structure some times: x <*> y <$> z e.g. (+) <*> (+1) <$> a Can somebody explain this structure to me? I get that it translates to fmap a (+ a + 1) , but I can't make the connection 回答1: Let's start with: x <*> y <$> z Adding parentheses, it becomes: (x <*> y) <$> z Given that (<$>) :: Functor f => (a -> b) -> f a -> f b , we have: x <*> y :: a -> b z :: Functor f => f a Given that (<*>) :: Applicative g => g (c -> d) -

Is there a built-in Java type that guarantees an execute(T t) method?

拜拜、爱过 提交于 2019-12-23 09:37:53
问题 It seems the need for a type like the following would be so ubiquitous that something like it should be already built into Java: public interface Executer<T> { void execute(T object); } It can then be used in other classes like this trivial example that calls a bunch of executers on an object. class Handler<T> implements Executer<T> { List<Executer<T>> executerList; Handler(List<Executer<T>> executer) { this.executerList = executer; } void execute(T t) { for (Executer<T> executer : this