Questions tagged [functor]
839 questions
0
votes
0
answer
6
Views
Data types a la carte — over-engineered?
I'm working through Swiertstra's 2008 paper. I'm up to Section 3 eval addExample, just before 'Automating injections'. And boy! does it need automating.
Is it just me, or is the whole thing over-engineered? Compare this in the paper
addExample :: Expr (Val :+: Add)
addExample = In (Inr (Add (In (Inl...
0
votes
1
answer
47
Views
How can I Tell if a Type is a Functor? [duplicate]
This question already has an answer here:
Find out whether a C++ object is callable
8 answers
Can std::is_invocable be emulated within C++11?
1 answer
In c++17 I have is_invocable to match function pointers, lambdas, and functors.
But what if I'm trapped on c++14? Do I have a type trait, or can I...
1
votes
1
answer
86
Views
Fluent Interface in ReasonML
Is there a way to get a module to return its own type using a functor submodule?
Example
module type A = {};
module type B = { let varB: int; };
module AMod : A = {};
module BMod : B = { let varB = 42; };
module Fn = (A: A, B: B) => A; /* Lets say this does some complex function that returns Type A...
3
votes
4
answer
126
Views
Applicative typeclass based on two different functors
Is there something similar to the Applicative type class, but where there are two functors for each side of the application which are different?
i.e. () :: (Functor f, Functor g) => f (a -> b) -> g a -> f b
1
votes
1
answer
84
Views
Is there a way to call a class method from a functor inside the same class?
I am a little bit new in C++ so I am not sure how to handle such a problem. I have a class with several private methods. Inside one of the methods, there is a functor from which I want to call another class method.
Here is the example code:
# myprogram.h
namespace new_space
{
// Generic functor
temp...
1
votes
1
answer
64
Views
Can I have an array of lambda pointers in order to hold functions with different types of arguments/return values?
Let's assume I have a class with two member functions:
#include
#include
using namespace std;
class MyClass{
MyClass(){
MyCommandServer server;
server.addToCollection(&[](string a)->string{return helloWorld();});
server.addToCollection(&[](string a)->string{return to_string(add(stoi(a[0]),stoi(a[1...
1
votes
0
answer
49
Views
Haskell: Will Kurt exercise - getting more info from dictionary
Exercise 28.3 'get programming with haskell' from Will Kurt. Idea is to use the cheapest robot part using applicative functors. Now I wrote the code below, which does exactly that, but I don't want to know the cost, but actually the entire part (name, description etc). Seems pointless to me to provi...
1
votes
1
answer
50
Views
Pass member function as callback to constructor other class
Hi I want to pass member function as argument to constructor other class. I tried lot solutions but all given me fail.
So..
First class..
class WebSocketsServerRunner : public WebSocketsServer {
private:
ThreadController threads = ThreadController();
SensorsState sensorsState;
void notifyClient(uint...
4
votes
3
answer
125
Views
Any function with the same polymorphic type as fmap must be equal to fmap?
I'm reading the second edition of Programming in Haskell and I've came across this sentence:
... there is only one way to make any given parameterised type into a functor, and hence any function with the same polymorphic type as fmap must be equal to fmap.
This doesn't seem right to me, though. I ca...
1
votes
3
answer
1.4k
Views
Creating functor from lambda expression
I would like to know if it is possible to create an actual functor object from a lambda expression. I don't think so, but if not, why?
To illustrate, given the code below, which sorts points using various policies for x and y coordinates:
#include
#include
#include
#include
struct Point
{
Poin...
1
votes
1
answer
2.1k
Views
pthread member function of a class with arguments
I was successful at attaching a thread to class member using the code on the bottom of this page: http://www.tuxtips.org/?p=5.
I can't figure out how to expand the code to encapsulate a method such as void* atom(void *inst) where *inst is a structure that contains various thread parameters. Specific...
1
votes
1
answer
344
Views
std::tr1::function - store function objects of various types in common container
I'm converting my implementation of functor to std::tr1::function (the reason is that with my implementation we need to predefine type and functor class for each function type, I want to extend this to work with any function).
I have
int param = 1;
std::tr1::bind (&MyClass::func, param);
and I want...
1
votes
2
answer
241
Views
C++ Trying to create an 'Intermediate' functor
What I mean by an 'intermediate' functor is: a normal functor where one of the arguments can be specified at call-time. The problem is that I have an animation timeline (essentially scalar values at particular frames), and the output of this needs to be piped to and from getter/setter methods withi...
1
votes
1
answer
633
Views
error: ambiguous overload for ‘operator[]’ when using boost-bind to boost-function
I am attempting to map the filter functor with one of the member methods of my class based on the value of an input string.
#include
#include
#include
#include
#include
#include
typedef boost::function < bool(std::map, std::string) > MyFilterFunctor;
class MyClass
{
public:
bool FilterFunction...
1
votes
1
answer
45
Views
Templated partial application call issues
For the upcoming Coursework in January, I started developing a small DirectX engine. To find out if there is a performance increase I wanted to try to not use any virtuals at all (I know that virtuals aren't all bad, but I just wanted to see if it is possible without them).
When I started playing ar...
1
votes
2
answer
191
Views
std::map of member functions with different args
I have a DeviceSettingsManager class that looks like:
class DeviceSettingsManager
{
int32_t PropertyA();
void SetPropertyA(int32_t prop);
std::string PropertyB();
void SetPropertyB(std::string prop);
// about 50 properties go here ...
}
and I need to set the properties according to the property stri...
1
votes
2
answer
76
Views
C++ functor compile error
I have the following main program, which when compiled gives this error:
Undefined symbols for double squarer(double, SineFunctor&)
Why can't the linker find the function squarer?
include
#include 'SineFunctor.h'
#include 'squarer.h'
int main()
{
const double PI = 3.141592L;
double x = 1.0;
double...
1
votes
1
answer
378
Views
C++ Defining a virtual base class overloaded operator
I am trying to write a set of generic math utility classes (root finders, integrators, etc.) that take in upon construction a pointer to a base class that defines the function I want the specific algorithm to operate on. The base class should only define a public virtual interface (abstract or with...
1
votes
1
answer
72
Views
Link 2 modules generated by functors in 2 files
I have defined several modules as follows:
(* zone.ml *)
module ZoneFun (Prop : PROP) = (struct ... end: ZONE)
(* zones.ml *)
module ZonesFun (Zone : ZONE) = (struct ... end: ZONES)
where PROP is the interface of the module Type and some other modules.
(* calculate.ml *)
open Type
open Zone
open Zon...
1
votes
1
answer
97
Views
Why can't compiler deduce types from templates like template<typename R(typename… Args)>?
For me it would look natural that the compiler could deduce types of templates like this:
template
struct wrap {
std::function func_;
template
wrap(Args2&& ...args2) : func_(std::forward(args2)...) {
}
R operator()(Args&&... args) {
cout
1
votes
1
answer
434
Views
Difference between function and functor [closed]
I'm trying to write the following code:
class MyClass{
public:
virtual int operator()()=0;
}
int bar()
{
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
class : public MyClass{
int operator()(){
return 1;
}
} foo;
}
What's difference between foo and bar?
1
votes
1
answer
75
Views
Functor and function pointer logic
class StrangeFunctor
{
public:
StrangeFunctor(int (*comp)(string, string))
{
this->comp = comp;
}
int operator()(string str1, string str2)
{
return –comp(str1, str2);
}
private:
int (*comp)(string, string);
}
I was just curious as what the above code actually did. Assuming the functo...
1
votes
1
answer
309
Views
How can you bind a variadic member function to a functor?
I am attempting to bind the first parameter of a variadic function using std::bind and then pass the returned functor to the connect() function of a boost::signals2::signal. The process works fine as long as the variadic function is not a member function. This is what I would like to be able to do...
1
votes
1
answer
580
Views
Functors vs Function Closures in Python
We were supposed to use functors for our lab, and then use function closures instead.
For the functors we imported partial as command from functools, where the set up ended up looking something like:
digit = command(self.add_character, text = number)
label.bind('< Button-1 >', digit)
where we would...
1
votes
1
answer
118
Views
How to define fmap in a GADT Expression?
Given a simple 'language':
data Expr a where
ConstE :: a -> Expr a
FMapE :: (b -> a) -> Expr b -> Expr a
instance Functor Expr where
fmap = FMapE
interpret :: Expr a -> a
interpret (ConstE a) = a
interpret (FMapE f a) = f (interpret a)
From that I would like to extract a call g...
1
votes
2
answer
207
Views
How to use different functoids within an array or vector
I have written a small piece of code where I am able to call setter and getter functions packed within a functoid using mem_fun templates.
I now would like to use this approach on top of a class hierarchy where every class might have getter and setter which can be registered as pair within a vector...
0
votes
0
answer
16
Views
Why does GHC not parse functor instance for 'data Wrap f a = Wrap (f a)'?
In Haskell Programming From First Principles section 16.13, the Wrap datatype is presented to demonstrate a type whose Functor instance requires a typeclass constraint on one of its parameters:
data Wrap f a =
Wrap (f a)
deriving (Eq, Show)
After demonstrating a couple of incorrect instances of Func...
1
votes
1
answer
69
Views
Passing a function object to a constructor [duplicate]
This question already has an answer here:
Pass anonymous function object to std::function?
1 answer
What I am trying to achieve is to make a functor that can take different functors as arguments.
Edit: the reason for my problem, the 'most vexing parse', and the solution are well-described: see this...
1
votes
1
answer
554
Views
Invalid cast from type 'MyStruct' to type 'void*'
I have a struct called Spiketimesolver defined in the following header file:
#include
#include 'plot_support.h'
struct Spiketimesolver {
const Doub tau_0, I_0, V_start, I_start;
Spiketimesolver(const Doub tau_0, const Doub I_0, const Doub V_start,
const Doub I_start) :
tau_0(tau_0), I_0(I_0), V_sta...
1
votes
1
answer
389
Views
Examples of Functors composition
This is a follow-up to an answer to my previous question.
We know that functors compose. I can write a composition of functors List[_] and Option[_] using scalaz like this:
import scalaz._, Scalaz._
scala> val flist = Functor[List]
flist: scalaz.Functor[List] = [email protected]
1
votes
1
answer
37
Views
Terminology surrounding functors
Let's take a simple functor
class LessThan {
public:
explicit LessThan (int in ) : val(in) {}
bool operator() (int param) const {
return param < val;
}
private:
int val;
};
which I can use like this for example -
vector myVector = factory();
size_t count = count_if(begin(myVector), end(myVector), L...
1
votes
1
answer
183
Views
Conditional specialization of functors operator()
I would like to ask you for help with the programming headache I face last few days. Let me try to explain what I am about to implement...
My goal is to define a set of equations with its validity. Let me explain more in detail...
I think about being each equation object a functor - a class defining...
1
votes
1
answer
201
Views
How to write the Functor instance for this type?
I'm struggling with an exercise from Haskell Book (Chapter 16. Functor). Given the following, it expects me to define fmap:
{-# LANGUAGE FlexibleInstances #-}
newtype Flip f a b =
Flip (f b a)
deriving (Eq, Show)
newtype K a b =
K a
instance Functor (Flip K a) where
fmap = undefined
In a previous...
1
votes
4
answer
102
Views
Restrict functor parameter type and constness
I am trying to implement a resource protection class which would combine data along with a shared mutex (actually, QReadWriteLock, but it's similar). The class must provide the method to apply a user-defined function to the data when the lock is acquired. I would like this apply method to work diffe...
1
votes
1
answer
137
Views
How can you add a String to a list of Monads in Haskell
I am a new to functional programming. I am scraping a website using Scalpel and I need to extract information from links contained in that website. What I can extrapolate though is just part of the link and I need to add to the String 'http://www.google.com/' to each of these links. I can't do a nor...
1
votes
1
answer
106
Views
Can you use a functor/functional programming to group a list in Java 7 (and count its elements per group)?
Can you group List types = new ArrayList(Arrays.asList(TypeEnum.A, TypeEnum.B, TypeEnum.A)); into a Map countPerType;, using functors (e.g. Google's Guava, Apache's Commons Functor) pre Java 8?
I'm trying to get my head around functional programming, but am unsure if that sort of thing would actuall...
1
votes
1
answer
254
Views
How to resolve this ambiguity while mapping my Either type?
Suppose I've got two types IntResult and StringResult:
import cats._
import cats.data._
import cats.implicits._
scala> case class MyError(msg: String)
defined class MyError
scala> type Result[A] = Either[NonEmptyList[MyError], A]
defined type alias Result
scala> type StringResult = Result[String]
de...
1
votes
1
answer
123
Views
Scala: composition law counter example
I'm learning functional programming by using Scala language. I have read tutorial about functor that functor has 2 laws:
1. identity law: functor.map(x => x) ≡ functor
2. Composition law: functor.map(x => f(g(x))) ≡ functor.map(g).map(f)
The thing I don't know about composition law. I have a fee...
1
votes
1
answer
203
Views
why scala.util.Try has an instance in cats but not in scalaz?
typelevel's cats(version 0.9.0) has a Functor instance for Try
object catsTry {
import cats.Functor
import cats.instances.try_._
import scala.util.Try
val f = implicitly[Functor[Try]] //compile
}
while there is no Functor instance for Try in scalaz(version 7.3.0-M12).
object scalazTry {
import scal...
1
votes
1
answer
77
Views
fmap over variable argument function
I would like to define fmap on variable argument functions:
type family VarArg (args :: [*]) e where
VarArg '[] e = e
VarArg (a ': as) e = a -> VarArg as e
mapVarArg :: forall args e e'
. (e -> e') -> VarArg args e -> VarArg args e'
mapVarArg f = _
This is the closest solution I have found:
mapVarAr...