functor

Perform argument substitution on nested boost::bind without composition

落爺英雄遲暮 提交于 2019-11-30 15:29:21
Suppose I have a function which takes a nullary functor as an argument: void enqueue( boost::function<void()> & functor ); I have another function which takes an int and does something internally: void foo( int a); I would like to nest, but not compose, these together so that I get a functor with the signature: boost::function<void(int)> functor Which when called with a value - say 4 - performs the following: enqueue( boost::bind(&foo, 4) ) My first attempt was the following: boost::function<void(int)> functor = boost::bind(&enqueue, boost::bind(&foo,_1)) This fails because bind performs

Defining < for STL sort algorithm - operator overload, functor or standalone function?

可紊 提交于 2019-11-30 14:57:18
问题 I have a stl::list containing Widget class objects. They need to be sorted according to two members in the Widget class. For the sorting to work, a less-than comparator comparing two Widget objects must be defined. There seems to be a myriad of ways to do it. From what I can gather, one can either: a. Define a comparison operator overload in the class: bool Widget::operator< (const Widget &rhs) const b. Define a standalone function taking two Widgets: bool operator<(const Widget& lhs, const

Defining < for STL sort algorithm - operator overload, functor or standalone function?

南笙酒味 提交于 2019-11-30 12:57:46
I have a stl::list containing Widget class objects. They need to be sorted according to two members in the Widget class. For the sorting to work, a less-than comparator comparing two Widget objects must be defined. There seems to be a myriad of ways to do it. From what I can gather, one can either: a. Define a comparison operator overload in the class: bool Widget::operator< (const Widget &rhs) const b. Define a standalone function taking two Widgets: bool operator<(const Widget& lhs, const Widget& rhs); And then make the Widget class a friend of it: class Widget { // Various class definitions

c++ functor and function templates

淺唱寂寞╮ 提交于 2019-11-30 11:24:39
consider this simple and pointless code. #include <iostream> struct A { template<int N> void test() { std::cout << N << std::endl; } }; int main() { A a; a.test<1>(); } It is a very simple example of a function template. What if however, I wanted to replace A::test with an overloaded operator() to make it a functor? #include <iostream> struct A { template<int N> void operator()() { std::cout << N << std::endl; } }; int main() { A a; a<1>(); // <-- error, how do I do this? } Certainly if the operator() took parameters which were dependent on the template, the compiler could possibly deduce the

Lax monoidal functors with a different monoidal structure

感情迁移 提交于 2019-11-30 11:20:01
问题 Applicative functors are well-known and well-loved among Haskellers, for their ability to apply functions in an effectful context. In category-theoretic terms, it can be shown that the methods of Applicative : pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b are equivalent to having a Functor f with the operations: unit :: f () (**) :: (f a, f b) -> f (a,b) the idea being that to write pure you just replace the () in unit with the given value, and to write (<*>) you squish the function and

Fun with repeated fmap

梦想的初衷 提交于 2019-11-30 11:13:08
问题 I was playing around with functors, and I noticed something interesting: Trivially, id can be instantiated at the type (a -> b) -> a -> b . With the list functor we have fmap :: (a -> b) -> [a] -> [b] , which is the same as map . In the case of the ((->) r) functor (from Control.Monad.Instances ), fmap is function composition, so we can instantiate fmap fmap fmap :: (a -> b) -> [[a]] -> [[b]] , which is equivalent to (map . map) . Interestingly, fmap eight times gives us (map . map . map) !

If SML.NET had functors why can't F#?

感情迁移 提交于 2019-11-30 10:35:29
问题 This question started out from My translating of "ML for the Working Programmer" (WorldCat) by L. C. PAULSON to F# which uses functors for the examples. Eventual desire to translate "Purely Functional Data Structures" (WorldCat) by Chris Okasaki which uses functors. Reading "CATEGORIES TYPES AND STRUCTURES - An Introduction to Category Theory for the working computer scientist" (WorldCat) by Andrea Asperti and Giuseppe Longo. Not understanding it all, mostly the category theory. SML.NET can

Difference in capability between fmap and bind?

别来无恙 提交于 2019-11-30 08:43:07
I'm new to functional programming (coming from javascript), and I'm having a hard time telling the difference between the two, which is also messing with my understand of functors vs. monads. Functor: class Functor f where fmap :: (a -> b) -> f a -> f b Monad (simplified): class Monad m where (>>=) :: m a -> (a -> m b) -> m b fmap takes a function and a functor, and returns a functor. >>= takes a function and a monad, and returns a monad. The difference between the two is in the function parameter: fmap - (a -> b) >>= - (a -> m b) >>= takes a function parameter that returns a monad. I know

C++ std::transform vector of pairs->first to new vector

若如初见. 提交于 2019-11-30 08:13:19
Sorry for a little bit beginner question. There are vector and vector of pairs typedef std::vector <int> TItems; typedef std::vector < std::pair <int, int> > TPairs; Is there any way to transform all first items in pair to another vector in one step int main () { TItems items; TPairs pairs; pairs.push_back (std::make_pair(1,3)); pairs.push_back (std::make_pair(5,7)); std::transform( items.begin(), items.end(), items.begin(), comp ( &pairs ) ); return 0; } How to design a functor? class comp { private: TPairs *pairs; public: comp ( TPairs *pairs_ ) : pairs ( pairs_) { } unsigned int operator ()

Standard ML functor examples

假如想象 提交于 2019-11-30 06:58:16
问题 Functors in Standard ML are related to the module system and can generate structures based on other structures. An example of a functor generating list combinators for various types of lists is given below, but this example has a problem: The various types of lists all have advantages -- for example, lazy lists can be infinitely long, and concantenation lists have a O(1) concat operator. But when all of these list types conform to the same signature, the functor can only use their general