functor

Class's operator() or bind a function as a functor?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 21:26:24
问题 There are two ways to make a functor (a function that holds a state): bind a function and define a state: bind(f, _1, state) double g(double x, double state) { return x+state; } function f = bind(g,_1,state); use () operator and a class: struct f { double state; f(double state_):state(state_) {} double operator()(double x) {return x+state;} }; I find that bind -method is faster to write but I'm wondering if there are some hidden stones since most of the time in literature I see functor as

In applicative, how can `<*>` be represented in terms of `fmap_i, i=0,1,2,…`?

丶灬走出姿态 提交于 2019-12-10 17:28:39
问题 Class Applicative is declared as: class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b We can represent fmapi, i=0,1,2,... in terms of pure and (<*>) : fmap0 :: a -> f a fmap0 = pure fmap1 :: (a -> b) -> f a -> f b fmap1 g x = pure g <*> x fmap2 :: (a -> b -> c) -> f a -> f b -> f c fmap2 g x y = pure g <*> x <*> y fmap3 :: (a -> b -> c -> d) -> f a -> f b -> f c -> f d fmap3 g x y z = pure g <*> x <*> y <*> z In applicative, how can <*> be represented in

Why doesn't Haskell have a stronger alternative to Eq?

主宰稳场 提交于 2019-12-10 17:24:51
问题 The reason why Set is not a functor is given here. It seems to boil down to the fact that a == b && f a /= f b is possible. So, why doesn't Haskell have as standard an alternative to Eq, something like class Eq a => StrongEq a where (===) :: a -> a -> Bool (/==) :: a -> a -> Bool x /== y = not (x === y) x === y = not (x /== y) for which instances are supposed to obey the laws ∀a,b,f. not (a === b) || (f a === f b) ∀a. a === a ∀a,b. (a === b) == (b === a) and maybe some others? Then we could

C++ getting rid of Singletons: alternative to functors and static methods

孤街浪徒 提交于 2019-12-10 16:33:53
问题 My noble quest is to get rid of singletons and static classes. Background: I have the following structures: Cmd Frequently instantiated object, it holds a name of the command (string), and functor to the static method of any class as a pointer. It is typically created in main classes such as Input, Console, Render, etc. and refers to methods within the class that it is created in, giving a runtime verbal interface to those methods. Cmds also interpret parameters in a form of a string array,

correct use of functor for C++ STL thread

若如初见. 提交于 2019-12-10 15:39:09
问题 I'm having some difficulty understanding the correct usage of a functional object as a thread routine in C++ STL. From my understanding, one of the benefits of a functor is that the object instance can maintain state. There are times when I want one or more threads to run some routine and compute some result. I then query those results from the objects after I have joined the threads. I'm trying to do the same with C++ STL threads and running into some problems. It appears the problem stems

C++ functor advantage - holding the state [duplicate]

邮差的信 提交于 2019-12-10 14:55:27
问题 This question already has answers here : What are C++ functors and their uses? (15 answers) Closed 5 years ago . I did study the whole idea of functors, unfortunately I can't understand the real advantage of functors over typical functions. According to some academic scripts, functors can hold state unlike functions. Can anyone elaborate on this one with some simple, easy to understand example ? I really can't understand why typical, regular function are not able to do the same. I'm really

How to use pointed functor properly

核能气质少年 提交于 2019-12-10 14:47:10
问题 I'm trying to get familiar with functional programming in JavaScript. I've just read that pointer functor is: An object with an of function that puts any single value into it. ES2015 adds Array.of making arrays a pointed functor. And my question is what does exactly mean "single value"? I want to make a Functor/Container (like in https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch8.html) that holds grid of given dimension (width, height) as 1-dimensional array and allows me to do

C++ Understanding Functors Polymorphism

我与影子孤独终老i 提交于 2019-12-10 14:37:10
问题 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 to a new child class, I can not call the object as a function. The error is: main.cpp:29:7: error: ‘a’ cannot be used as a function Here is the code: #include <iostream> class foo{ public: virtual

Haskell defining Functor instance for an alternative Either data type

你离开我真会死。 提交于 2019-12-10 14:23:59
问题 Going through Typeclassopedia to gain some routing working with type classes. Want to make an alternative to Either an instance of Functor , but even examining the definition of Either as an instance of Functor keeps getting me in trouble. Have this, but will not compile. data Alt a b = Success a | Failure b deriving (Show, Eq, Ord) instance Functor (Alt a) where fmap _ (Failure a) = Failure a fmap f (Success x) = Success (f x) • Couldn't match expected type ‘a1’ with actual type ‘a’ ‘a1’ is

Why isn't (->) implemented with Control.Monad.Instances by default

只愿长相守 提交于 2019-12-10 14:06:41
问题 I was reading LYAH. It says I need to explicitly load Control.Monad.Instances to get the following syntax to work: ( ( fmap (+5) ) (+5) ) 4 Why is that? Why if functors are this underlying and unifying technology do I have to explicitly load Control.Monad.Instances to get that functionality. How is (->) implemented without it (or is just hidden and only -> exported)? Why isn't the use of fmap over function types implemented by default? 回答1: There are 3 different concepts involved here. The