functor

C++: pass function with arbitrary number of parameters as a parameter

不问归期 提交于 2019-11-28 12:09:06
long time browser, first time asker here. I've written a number of scripts for doing various 1D numerical integration methods and compiled them into a library. I would like that library to be as flexible as possible regarding what it is capable of integrating. Here I include an example: a very simple trapezoidal rule example where I pass a pointer to the function to be integrated. // Numerically integrate (*f) from a to b // using the trapezoidal rule. double trap(double (*f)(double), double a, double b) { int N = 10000; double step = (b-a)/N; double s = 0; for (int i=0; i<=N; i++) { double xi

Why does the 2-tuple Functor instance only apply the function to the second element?

孤人 提交于 2019-11-28 08:54:41
import Control.Applicative main = print $ fmap (*2) (1,2) produces (1,4) . I would expect it it to produce (2,4) but instead the function is applied only to the second element of the tuple. Update I've basically figured this out almost straight away. I'll post my own answer in a minute.. Let me answer this with a question: Which output do you expect for: main = print $ fmap (*2) ("funny",2) You can have something as you want (using data Pair a = Pair a a or so), but as (,) may have different types in their first and second argument, you are out of luck. Pairs are, essentially, defined like

Generic functor for functions with any argument list

空扰寡人 提交于 2019-11-28 07:53:05
I need to implement a functor that takes any (!) function pointer when instantiated, analyses the argument types, stores the pointer and when operator() is called, does something with the pointer. The easiest case being, calling the function with its arguments. I tried converting the function pointer to something like std::function and I get the error: error: invalid use of incomplete type ‘struct std::function<void (*)(...)>’ /usr/include/c++/4.6/functional:1572:11: error: declaration of ‘struct std::function<void(*)(...)>’ I am using gcc 4.6.1, compiling with -std=c++0x This is a minimal

To what extent are Applicative/Monad instances uniquely determined?

浪尽此生 提交于 2019-11-28 05:21:54
As described this question/answers , Functor instances are uniquely determined, if they exists. For lists, there are two well know Applicative instances: [] and ZipList . So Applicative isn't unique (see also Can GHC derive Functor and Applicative instances for a monad transformer? and Why is there no -XDeriveApplicative extension? ). However, ZipList needs infinite lists, as its pure repeats a given element indefinitely. Are there other, perhaps better examples of data structures that have at least two Applicative instances? Are there any such examples that only involve finite data structures

What monads can be expressed as Free over some functor?

半世苍凉 提交于 2019-11-28 04:42:56
The documentation for Free says: A number of common monads arise as free monads, Given data Empty a , Free Empty is isomorphic to the Identity monad. Free Maybe can be used to model a partiality monad where each layer represents running the computation for a while longer. What other monads are expressible using Free ? I could think only of one more: I believe Free (Const e) is isomorphic to Either e . Edit: What monads are not expressible using Free and why? Philip JF Almost all of them (up to issues involving looping and mfix ) but not Cont . Consider the State monad newtype State s a = State

Sets, Functors and Eq confusion

家住魔仙堡 提交于 2019-11-28 03:38:57
A discussion came up at work recently about Sets, which in Scala support the zip method and how this can lead to bugs, e.g. scala> val words = Set("one", "two", "three") scala> words zip (words map (_.length)) res1: Set[(java.lang.String, Int)] = Set((one,3), (two,5)) I think it's pretty clear that Set s shouldn't support a zip operation, since the elements are not ordered. However, it was suggested that the problem is that Set isn't really a functor, and shouldn't have a map method. Certainly, you can get yourself into trouble by mapping over a set. Switching to Haskell now, data AlwaysEqual

“can't existentially abstract over parameterized type…”

岁酱吖の 提交于 2019-11-28 02:46:41
问题 I was messing around with Scala 2.8 for fun and trying to define a pimp which adds an "as" method to type constructors, allowing to convert from one functor to another (please overlook the fact that I'm not necessarily dealing with functors here). So for instance, you could use it like this: val array:Array[T] val list:List[T] = array.as[List] So here's what I tried to do: object Test { abstract class NatTrans[F[_], G[_]] { def convert[T](f:F[T]):G[T] } implicit def array2List:NatTrans[Array,

Stateful functors & STL : Undefined behaviour

孤人 提交于 2019-11-27 23:06:08
I am following this Function objects tutorial Copy-pasta below: I am unable to understand the following: Predicates should always be implemented as stateless function objects to avoid unexpected results. There is no guarantee how often an algorithm might internally copy the predicate. Thus, having predicates that are implemented as stateful function objects might have unexecpted results. The example is as follows: #include <iostream> #include <vector> #include <algorithm> #include <iterator> class predicate { public: predicate(int condition) : condition_(condition), state_(0) {} bool operator(

Why Functor class has no return function?

半城伤御伤魂 提交于 2019-11-27 22:13:12
From categorical point of view, functor is pair of two maps (one between objects and another between arrows of categories), following some axioms. I have assumed, what every Functor instance is similar to mathematical definition i.e. can map both objects and functions, but Haskell's Functor class has only function fmap which maps functions. Why so? UPD In other words: Every Monad type M has an function return :: a -> M a . And Functor type F has no function return :: a -> F a , but only F x constructor. laughedelic First of all, there are two levels: types and values. As objects of Hask are

When to use functors over lambdas

主宰稳场 提交于 2019-11-27 21:55:06
Is there ever a situation where it makes more sense to create a functor than to use a lambda? I know my question is effectively the reverse of when to use a lambda over a functor , but I can't think of a situation in practice where a functor would be preferred over a lambda. Any thoughts on this? A lambda is a functor - just defined with a shorter syntax. The problem is that this syntax is limited. It doesn't always allow you to solve a problem in the most efficient and flexible way - or at all. Until C++14, the operator() couldn't even be a template. Furthermore, a lambda has exactly one