functor

How are Functors useful?

我的未来我决定 提交于 2019-12-03 01:39:07
We know that any generic type F[_] with map method, which complies to some laws , is a functor . For instance, List[_] , Option[_] , and F[A] = Env => A are functors. I am just wondering if this functor abstraction is meaningful. How can I use the fact that they are functors ? Could you show an example of non-trivial computation, which would use the map and be actually useful ? One of the biggest benefits of concepts like functions is that there are generic constructions that allow you to build more complex types out of simpler functors, and guarantee that these complex types have certain

Indexing into containers: the mathematical underpinnings

不羁的心 提交于 2019-12-03 01:29:15
When you want to pull an element out of a data structure, you have to give its index. But the meaning of index depends on the data structure itself. class Indexed f where type Ix f (!) :: f a -> Ix f -> Maybe a -- indices can be out of bounds For example... Elements in a list have numeric positions. data Nat = Z | S Nat instance Indexed [] where type Ix [] = Nat [] ! _ = Nothing (x:_) ! Z = Just x (_:xs) ! (S n) = xs ! n Elements in a binary tree are identified by a sequence of directions. data Tree a = Leaf | Node (Tree a) a (Tree a) data TreeIx = Stop | GoL TreeIx | GoR TreeIx --

SIMD or not SIMD - cross platform

笑着哭i 提交于 2019-12-02 23:32:37
I need some idea how to write a C++ cross platform implementation of a few parallelizable problems in a way so I can take advantage of SIMD (SSE, SPU, etc) if available. As well as I want to be able at run time to switch between SIMD and not SIMD. How would you suggest me to approach this problem? (Of course I don't want to implement the problem multiple times for all possible options) I can see how this might not be very easy task with C++ but I believe that I'm missing something. So far my idea looks like this... A class cStream will be array of a single field. Using multiple cStreams I can

Make Data Type of Kind * -> * That's Not a Functor

独自空忆成欢 提交于 2019-12-02 20:57:59
Brent Yorgey's Typeclassopedia gives the following exercise: Give an example of a type of kind * -> * which cannot be made an instance of Functor (without using undefined ). Please tell me what " cannot be made an instance of Functor " means. Also, I'd appreciate an example, but perhaps as a spoiler so that you can, please, guide me to the answer. J. Abrahamson Let's talk about variances. Here's the basic notion. Consider the type A -> B . What I want you to imagine is that such a type is similar to "having a B " and also "owing an A ". In fact, if you pay back your A you immediately receive

Understanding how Either is an instance of Functor

五迷三道 提交于 2019-12-02 18:47:57
In my free time I'm learning Haskell, so this is a beginner question. In my readings I came across an example illustrating how Either a is made an instance of Functor : instance Functor (Either a) where fmap f (Right x) = Right (f x) fmap f (Left x) = Left x Now, I'm trying to understand why the implementation maps in the case of a Right value constructor, but doesn't in the case of a Left ? Here is my understanding: First let me rewrite the above instance as instance Functor (Either a) where fmap g (Right x) = Right (g x) fmap g (Left x) = Left x Now: I know that fmap :: (c -> d) -> f c -> f

Sort function does not work with function object created on stack?

久未见 提交于 2019-12-02 18:34:50
问题 #include<iostream> #include<vector> #include<algorithm> class Integer { public: int m; Integer(int a):m(a){}; }; class CompareParts { public: bool operator()(const Integer & p1,const Integer & p2) { return p1.m<p2.m; } }obj1; int main() { std::vector<Integer> vecInteger; vecInteger.push_back(Integer(12)); vecInteger.push_back(Integer(13)); vecInteger.push_back(Integer(5)); vecInteger.push_back(Integer(7)); vecInteger.push_back(Integer(9)); Integer obj2(); std::sort(vecInteger.begin()

How are functors in Haskell related to functors in category theory?

蹲街弑〆低调 提交于 2019-12-02 18:12:18
For as far as I understand, a functor is a mapping between two categories, for example from objects in to objects in where and are categories. In Haskell there is Hask in which the objects are Haskell types and the morphisms are Haskell functions. However, the Functor type class has a function fmap which maps between these types (which are thus objects and not categories themselves): fmap :: (a -> b) -> f a -> f b f a and f b are both objects in Hask . Does this mean every instance of Functor in Haskell is an endofunctor, and if not does Functor really represent a functor? What am I missing

C++ How do you pass a member function into a functor parameter?

前提是你 提交于 2019-12-02 13:53:48
问题 I am using TRI DDS - here is the prototype for the function I am trying to call: template<typename T , typename Functor > dds::sub::cond::ReadCondition::ReadCondition ( const dds::sub::DataReader< T > & reader, const dds::sub::status::DataState & status, const Functor & handler ) So I have a class that looks a bit like this (with load of irrelevant stuff omitted): MyClass test{ public: test(){... mp_reader = ...}; // not complete start_reader() { dds::sub::cond::ReadCondition rc(*mp_reader,

use n_th element in a container, but with another key

空扰寡人 提交于 2019-12-02 04:39:00
I have two vectors. One that actually holds the data (let's say floats) and one that holds the indices. I want to pass at nth_element the indices vector, but I want the comparison to be done by the vector that actually holds the data. I was thinking about a functor, but this provides only the () operator I guess. I achieved that by making the data vector a global one, but of course that's not desired. std::vector<float> v; // data vector (global) bool myfunction (int i,int j) { return (v[i]<v[j]); } int find_median(std::vector<int> &v_i) { size_t n = v_i.size() / 2; nth_element(v_i.begin(), v

C++ How do you pass a member function into a functor parameter?

可紊 提交于 2019-12-02 03:54:48
I am using TRI DDS - here is the prototype for the function I am trying to call: template<typename T , typename Functor > dds::sub::cond::ReadCondition::ReadCondition ( const dds::sub::DataReader< T > & reader, const dds::sub::status::DataState & status, const Functor & handler ) So I have a class that looks a bit like this (with load of irrelevant stuff omitted): MyClass test{ public: test(){... mp_reader = ...}; // not complete start_reader() { dds::sub::cond::ReadCondition rc(*mp_reader, dds::sub::status::DataState::any(), do_stuff()); // This does not work } void do_stuff() {...} private: