FAQ Database Discussion Community


Composition of compositions in Haskell

haskell,functional-programming,functor
I am learning Haskell recently, and I was reading Functors in Learn You a Haskell from which I came to know Functions ((->)r) which take one parameter, are also in a way Functors. Composition (.) is equivalent to fmap So with what I have understood, fmap takes two parameters. First...

Functor compile time error

c++,c++11,functor,standard-library
My code - #include <iostream> #include <vector> #include <string> #include <sstream> using namespace std; #include "boost\numeric\ublas\matrix.hpp" typedef boost::numeric::ublas::matrix<float> matrix; class FillMat{ public: FillMat(float valIn) : val(valIn){} float operator()(float in) { val = in + 1; return val; } private: float val; }; typedef boost::numeric::ublas::matrix<float> matrix; int main(){ matrix m1(10, 20);...

Define a default functor argument using lambda expression

c++,lambda,functor,default-arguments
I'm trying to define a default value for an argument which's a functor. But I got this error: note: candidate template ignored: couldn't infer template argument 'Comp' Here's the definition of my function: template <typename RAIterator, typename Comp> void somefunction(RAIterator beg, RAIterator end, Comp comp= [](const typename iterator_traits<RAIterator>::value_type& a, const...

In C++, how to let a (nested) compare functor refer to data of the enclosing class?

c++,caching,comparator,functor,nested-class
I want to design a custom compare functor for std::set, which uses cached values of the enclosing class (in which the set is defined). I know that in C++, there is no direct access from the nested class to the enclosing class and that you need to store a pointer...

Syntax help. Template operator() in template function object

c++,templates,c++11,functor
What is the proper syntax I need to run what I'm trying to run in main() below? #include <iostream> #include <vector> template <int... Is> void foo() { std::vector<int> v{Is...}; for (int x : v) std::cout << x << ' '; } template <int... Is> struct Foo { template <typename T,...

Understanding operations on composed functor types

haskell,functional-programming,functor
According to several sources, the Haskell implementation for composing functors is more or less the following: import Data.Functor.Compose newtype Compose f g a = Compose { getCompose :: f (g a) } instance (Functor f, Functor g) => Functor (Compose f g) where fmap f (Compose x) = Compose (fmap...

Passing dynamic functor to stl

c++,inheritance,stl,functor
I am relatively new to c++ and this is my very first post, so be gentle ;-) I understand how to pass function pointers or function objects, like many stl functions require. It is unclear to me how to use this in combination with inheritance. Specifically I would like to...

Why can't I define a function inside another function?

c++,functor,function-declaration
This is not a lambda function question, I know that I can assign a lambda to a variable. What's the point of allowing us to declare, but not define a function inside code? For example: #include <iostream> int main() { // This is illegal // int one(int bar) { return...

Example of defining a functor in haskell between two categories (which are closely related to additive and multiplicative monoids)

haskell,categories,functor
I am trying to reconcile the math notion of functor and the haskell notion of it. This article http://brianshourd.com/posts/2012-10-26-tilt-functors-in-haskell.html explains a bit, but maybe someone can provide an example for the following case: Suppose i define the following categories Add: where objects are 0,1,2,3,...., morphisms are (0+), (1+), (2+), ......

Checking every element of vector with functor

c++,vector,functor
I'm trying to detect whether every single element of the vector is fullfilling given condition, let's say it must even number. #include <iostream> #include <vector> #include <algorithm> bool isOdd(int i) { return i%2==0; } int main() { int arr[5]={1,2,3,4,5}; std::vector<int> myVec(arr, arr + sizeof(arr)/sizeof(arr[0])); std::vector<int>::iterator it = std::find_if(myVec.begin(), myVec.end(), isOdd());...

passing member-function as argument to function-template

c++,templates,functor,member-function-pointers,template-function
Consider three ways to implement a routine in c++: through functors, member functions, and non-member functions. For example, #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class FOO { public: void operator() (string word) // first: functor { cout << word << endl; } void m_function(string word) //...

Why is there no “Functor” trait in Scala? [closed]

scala,haskell,functional-programming,functor
In Scala, the generic classes such as Future, Option and List all have methods map and flatMap. As I understand, all of them are like Functors in Haskell. I was just wondering why there isn't a trait (interface) called Functor in Scala.. Does anyone have ideas about this?...

Must I implement Applicative and Functor to implement a Monad

haskell,monads,functor,applicative,fam-proposal
I'm trying to implement a Monad instance. As a simpler example, assume the following: data Maybee a = Notheeng | Juust a instance Monad Maybee where return x = Juust x Notheeng >>= f = Notheeng Juust x >>= f = f x fail _ = Notheeng This should be...

Getting error while using Functors without creating a Functor class?

c++,c++11,lambda,stl,functor
I want to use functors without creating a functor class but My foundVector is showing empty even if i store a matched string in foundVector. Also tell me is there any better way to use fucntors I am using Visual Studio 2013. Here is my demo code: Input: #include<iostream> #include<vector>...

Trivial functors

python,python-3.x,functor
I very often write code like: sorted(some_dict.items(), key=lambda x: x[1]) sorted(list_of_dicts, key=lambda x: x['age']) map(lambda x: x.name, rows) where I would like to write: sorted(some_dict.items(), key=idx_f(1)) sorted(list_of_dicts, key=idx_f('name')) map(attr_f('name'), rows) using: def attr_f(field): return lambda x: getattr(x, field) def idx_f(field): return lambda x: x[field] Are there functor-creators like idx_f and...

C++ Understanding Functors Polymorphism

c++,polymorphism,functor
I try to implement polymorphic functor objects (pure abstract base class and children) for understanding purposes only. My goal is to create many objects of the base class that use different implementations of the pure virtual functions. When I create a pointer of the base class and set it equal...

Functors with multiple inputs in Standard ML

sml,functor
High level question: How do I use functors with multiple arguments in SML? I've looked at this, this, this and this(PDF). All of them seem to conflict in terms of structure or functor definition syntax, and none of them show anything other than a unary functor. Specifics: I'm trying to...

Creation of a functor inside a member function without taking the class as a argument

c++,boost,bind,functor
Apologies for the cryptic decryption. I wish to create a functor of the following type: const boost::function<bool ()>& functor Please consider the class: #include <boost/function.hpp> class X { public: bool foo(); void bar() ; }; void X::bar() { const boost::function<bool (X *)>& f = &X::foo; } bool X::foo() { std::cout...

Why are instances matched only by their heads?

haskell,monads,typeclass,functor,applicative
I'll start by introducing a concrete problem (StackOverflow guys like that). Say you define a simple type data T a = T a This type is a Functor, Applicative and a Monad. Ignoring automatic deriving, to get those instances you have to write each one of them, even though Monad...

Easier way to apply multiple arguments in Haskell

haskell,functional-programming,composition,functor,applicative
Given value f with type :: Applicative f => f (a -> b -> c), What's the best way to map arguments to the inner function. So far I've found the following: (\x -> x a b) <$> f (flip ($ a) b) <$> f ($ b) <$> ($ a)...

Composing Free monads in Scala

scala,functional-programming,functor,free-monad
I think I understand what Free monad is. I hope I understand also that functors compose but monads do not, i.e. if M1 and M2 are monads then M1[M2] is not necessarily a monad. My questions are: Do Free monads compose ? Suppose we have functors F1 and F2 and...