Questions tagged [functor]

The term 'functor' has several common meanings: 1. Function object. In object-oriented languages, it is a feature that allows objects to be used as if they were ordinary functions. 2. A mathematical structure which deals with mappings between categories. This concept is a useful abstraction in some programming languages, notably Haskell, where it is implemented as a type class. 3. In OCaml, a module that takes another module as an argument.

functor
Filter by
Sorted by
Tagged with
3 votes
2 answers
208 views

Generics in record "with" syntax

If I've got a record with a generic field, is there any way to mimic the convenient with syntax when changing the generic? i.e. if I have type User<'photo> = // 'photo can be Bitmap or Url { ...
Dax Fohl's user avatar
  • 10.8k
-4 votes
2 answers
304 views

C++ Functors explanation [duplicate]

Could you please explain following statements with example Statement1 Ordinary functions have different types only when their signatures differ. However, function objects can have different types ...
rajen v's user avatar
  • 11
0 votes
2 answers
313 views

Example of defining a functor in haskell between two categories (which are closely related to additive and multiplicative monoids)

I am trying to reconcile the math notion of functor and the haskell notion of it. This article http://brianshourd.com/posts/2012-10-26-tilt-functors-in-haskell.html explains a bit, but maybe someone ...
Ruslan Talpa's user avatar
0 votes
1 answer
310 views

Define a default functor argument using lambda expression

I'm trying to define a default value for an argument which's a functor. But I got this error: note: candidate template ignored: couldn't infer template argument 'Comp' Here's the definition ...
Alex 's user avatar
  • 75
7 votes
2 answers
968 views

Why is there no "Functor" trait in Scala? [closed]

In Scala, the generic classes such as Future, Option and List all have methods map and flatMap. As I understand, all of them are like Functors in Haskell. I was just wondering why there isn't a ...
Hanfei Sun's user avatar
  • 46.2k
9 votes
2 answers
184 views

Why are instances matched only by their heads?

I'll start by introducing a concrete problem (StackOverflow guys like that). Say you define a simple type data T a = T a This type is a Functor, Applicative and a Monad. Ignoring automatic deriving, ...
Luka Horvat's user avatar
  • 4,373
0 votes
1 answer
410 views

How to define comparison for priority_queue, function or functor?

I need to store customized objects with priority_queue in C++. Should I use binary function or functor? Any advantage or disadvantage for each approach?? Thanks!
Sarah's user avatar
  • 453
6 votes
2 answers
1k views

C++ Understanding Functors Polymorphism

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 ...
Vincent's user avatar
  • 1,361
0 votes
1 answer
236 views

Signature mismatch with types in modules/functors

Excuse my potential misuse of terminology, I'm still not very comfortable with OCaml. We have a functor with the following (abridged) signature: module type FUNCTORA = sig type input ...
Leon's user avatar
  • 194
83 votes
11 answers
34k views

Why can't I define a function inside another function?

This is not a lambda function question, I know that I can assign a lambda to a variable. What's the point of allowing us to declare, but not define a function inside code? For example: #include <...
Jonathan Mee's user avatar
  • 38.4k
1 vote
1 answer
49 views

How to transfer additional object in functor for std::priority_queue?

I have my struct: struct S{ int a; }; And i have class: class Other{ //some fields }; I need write functor: struct Comparator { bool operator()(S& l, S& r) { //some ...
r1d1's user avatar
  • 469
7 votes
3 answers
898 views

Must I implement Applicative and Functor to implement a Monad

I'm trying to implement a Monad instance. As a simpler example, assume the following: data Maybee a = Notheeng | Juust a instance Monad Maybee where return x = Juust x Notheeng >>= f = ...
trevore's user avatar
  • 425
1 vote
1 answer
87 views

What is the functor in this example?

I have the following line of code: fmap (*2) (*11) 3 I'm trying to understand what my functor is in this example, because when I check the type I get this: Prelude> :t fmap (*2) (*11) 3 fmap (*2)...
jcm's user avatar
  • 5,579
3 votes
2 answers
227 views

What does the 'f' represent in the fmap function of a functor?

I'm looking at the following function: fmap :: (a -> b) -> f a -> f b and I want to understand what the 'f' is, as in (f a or f b). The article I am reading describes it as a 'box' but what'...
jcm's user avatar
  • 5,579
0 votes
1 answer
663 views

Scala class method to function with class instance as parameter

I'm pretty sure what I'd like to do is probably not possible and not a good idea anyway. Nonetheless, here it is. I would like to find a generic way of transforming any method on any class into a ...
user3618414's user avatar
3 votes
1 answer
961 views

How to make the function type constructor instance of functor? Duplicated instance

I'm trying to do the exercises in the typeclassopedia, but I don't have sufficient knowledge to make a data type that behaves like a function. The first exercise says: "Implement Functor instances ...
freinn's user avatar
  • 1,059
1 vote
1 answer
963 views

Understanding Applicative Functor in Scala

Suppose I need to write some functions to invoke a few REST APIs: api1, api2, api3. def api1(url: Url) = ??? def api2(url: Url) = ??? def api3(url: Url) = ??? Suppose for simplicity that I use my ...
Michael's user avatar
  • 41.7k
0 votes
1 answer
62 views

questions about function object in C++

I have a question regarding the following piece of code. template <typename T> struct DisplayElementKeepCount {     int m_nCount;     DisplayElementKeepCount () { m_nCount = 0; }     void ...
Sarah's user avatar
  • 453
4 votes
1 answer
243 views

Why is there no type class for monoids on functors in Haskell?

I admit that this question is a bit unspecific, but I was wondering why I never stumbled upon a type class for monoids on functors in Haskell. Did I just miss it, is there good reason for this absence ...
Martin Huschenbett's user avatar
10 votes
2 answers
438 views

Haskell: Flaw in the description of applicative functor laws in the hackage Control.Applicative article?: it says Applicative determines Functor

I think I found a flaw in the hackage article for Control.Applicative. As a description of the applicative functor laws, it says: class Functor f => Applicative f where A functor with ...
gksato's user avatar
  • 368
1 vote
2 answers
324 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 ...
Bernd L.'s user avatar
2 votes
1 answer
309 views

Haskell: how to map a function over an arbitrary tuple of monads

Is it possible to construct a function that will map of a tuple of monads of arbitrary arity? E.g., f :: (m a -> m [a]) -> (m x, m y) -> (m [x], m [y]) (but for arbitrary arity). This ...
user3416536's user avatar
  • 1,449
1 vote
2 answers
200 views

Interaction between functor, applicative and Monad

I am totally new to Haskell and I am trying to understand better how functor, applicative and monad work together. Below in my example: import Control.Monad import Control.Applicative data FooBar a =...
Randomize's user avatar
  • 8,913
0 votes
0 answers
102 views

I have this last exercise in Prolog to solve in lessons i do alone and dont know what to do

I have this last problem from the free lessons i take online ,to solve and i dont know from where to start i cant even understand what is this predicate does... Write predicate replace_var_args (...
Xanthoula Atsalaki's user avatar
2 votes
2 answers
139 views

Precedence Confusion about <$> and <*> in Haskell

Two examples both from http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors, 1). (+) <$> (+3) <*> (*100) $ 5 (+) <$> (+3) <*> (*100) $ ...
Matt Elson's user avatar
  • 4,295
3 votes
2 answers
5k views

How to use sort() in C++ with custom sort member function?

I have a question about passing the comparison function to sort(). What I want to do is define a sort() function that takes into account for its calculation a member variable of the class that I ...
nburk's user avatar
  • 22.6k
4 votes
3 answers
179 views

'Default Behavior' for Haskell recursive data types

I'm trying to write a propositional logic solver in Haskell. I'm representing logical expressions with a recursive data type called 'Sentence' that has several subtypes for different operations - '...
rwturner's user avatar
-1 votes
1 answer
75 views

use algorithm function max_element with function parametrer

I have a probleme by using max_element with 3 parametres : My list list<T*> myList_; the function template<typename T> T TheObject<T>::bigger () const{ return *(*(max_element(...
user1792430's user avatar
1 vote
2 answers
133 views

c++ stateful functor that populates vectors

I have a question regarding functors. I built a simple class: class PolygonPrinter { private: std::vector<float> x; std::vector<float> y; ...
A.I.'s user avatar
  • 87
11 votes
2 answers
523 views

Understanding operations on composed functor types

According to several sources, the Haskell implementation for composing functors is more or less the following: import Data.Functor.Compose newtype Compose f g a = Compose { getCompose :: f (g a) } ...
Marco Faustinelli's user avatar
0 votes
1 answer
145 views

passing a functor into a dll for reproting progress

I have a dll which is I am writing it as c++ on visual studio 2013. As part of this dll, there is a function which takes a long time to finish and I need to inform the caller progress ( as a percent ...
mans's user avatar
  • 17.6k
0 votes
1 answer
59 views

Getting error while using Functors without creating a Functor class?

I want to use functors without creating a functor class but My foundVector is showing empty even if i store a matched string in foundVector. Also tell me is there any better way to use fucntors I am ...
ojas's user avatar
  • 2,190
-1 votes
1 answer
296 views

Get rid of IF statements by using functors

please teach me how to get rid of those if statements by using functors (or any other better methods) inside the following loop: //Loop over each atom std::string temp_name ; float dst; for (pdb::...
Khoa's user avatar
  • 49
0 votes
4 answers
107 views

Functor compile time error

My code - #include <iostream> #include <vector> #include <string> #include <sstream> using namespace std; #include "boost\numeric\ublas\matrix.hpp" typedef boost::numeric::...
The Vivandiere's user avatar
4 votes
1 answer
761 views

How does fmap fmap apply to functions (as arguments)?

I am trying to understand how fmap fmap applies to a function like say (*3). The type of fmap fmap: (fmap fmap):: (Functor f1, Functor f) => f (a -> b) -> f (f1 a -> f1 b) Type of (*3): ...
egdmitry's user avatar
  • 2,111
0 votes
2 answers
92 views

calling functor using function Object tr1::function<>

I try to implement Scott Mayer book code example, the example is about calling functor through function object the header file gameCharachter.h #ifndef GAMECHARACTER_H #define GAMECHARACTER_H #...
freesyria's user avatar
0 votes
2 answers
279 views

In C++, how to let a (nested) compare functor refer to data of the enclosing class?

I want to design a custom compare functor for std::set, which uses cached values of the enclosing class (in which the set is defined). I know that in C++, there is no direct access from the nested ...
mr_T's user avatar
  • 2,611
4 votes
1 answer
580 views

Composition of compositions in Haskell

I am learning Haskell recently, and I was reading Functors in Learn You a Haskell from which I came to know Functions ((->)r) which take one parameter, are also in a way Functors. Composition (.) ...
shar's user avatar
  • 2,006
2 votes
2 answers
328 views

Checking every element of vector with functor

I'm trying to detect whether every single element of the vector is fullfilling given condition, let's say it must even number. #include <iostream> #include <vector> #include <...
Paulie Gaultieri's user avatar
5 votes
1 answer
339 views

Passing dynamic functor to stl

I am relatively new to c++ and this is my very first post, so be gentle ;-) I understand how to pass function pointers or function objects, like many stl functions require. It is unclear to me how to ...
APevar's user avatar
  • 53
11 votes
4 answers
1k views

Generating functors with iterator behavior

I have a question, which very likely has been asked like this before, because I think what I want is something that a considerable amount of people would want. However I could not come up with any way ...
midor's user avatar
  • 5,537
0 votes
0 answers
48 views

Persist functor in a realtional database

I have a Java Entity that has several well defined functors. I want to persist them so as not factorize the object once it is fetched from the database. Is there a way to do so with Ebean? I think ...
ag.albachicar's user avatar
2 votes
2 answers
1k views

Swift optional binding in generic function

Lovely buggy swift amazed me once again. In my new project I used optional applicative functor with <*> operator, described here. infix operator <*> { associativity left precedence 150 } ...
Aleksey G.'s user avatar
8 votes
1 answer
688 views

Composing Free monads in Scala

I think I understand what Free monad is. I hope I understand also that functors compose but monads do not, i.e. if M1 and M2 are monads then M1[M2] is not necessarily a monad. My questions are: Do ...
Michael's user avatar
  • 41.7k
0 votes
1 answer
3k views

Using drools for multiple object of same type and having relationships

Hey I have a following problem to solve using drools- We have an Object- Class Product{ String name; double cost; double margin; double sellPrice; } We have a Product object-P1 and it can have ...
Aashish Katta's user avatar
4 votes
1 answer
515 views

Haskell - Functor instance for generic polymorphic Algebraic Data Types using recursion-schemes

Problem: Recently I asked the following question on here, asking how to create a generic map function, and a generic instance of Functor for any arbitrary polymorphic ADT (Algebraic Data Type), like ...
gonzaw's user avatar
  • 771
3 votes
0 answers
309 views

Modern haskell implementation of generically derived bifunctors [closed]

I'm looking for a way to derive fmapFirst and fmapSecond for bifunctors automatically. I would prefer a way to do it using the new Generic type class or using Data.Data, and without Template Haskell. ...
tohava's user avatar
  • 5,392
4 votes
1 answer
4k views

inherit from std::function, syntax and usage?

I don't understand the syntax when a functor inherits from std::function: class do_sth : public function< any_type (another_type) > { bool operator() (string s) {...} }; What should I use ...
SimonH's user avatar
  • 1,405
8 votes
2 answers
1k views

Difference between function composition operator (.) and fmap (<$>)

Currently reading through this article (which is pretty brilliant btw) and have a pretty simple question: If I combine two functions like (+3) and (+2) with <$>, it seems to give me a new ...
Brendan's user avatar
  • 511
11 votes
3 answers
446 views

Functor instance for generic polymorphic ADTs in Haskell?

When it comes to applying category theory for generic programming Haskell does a very good job, for instance with libraries like recursion-schemes. However one thing I'm not sure of is how to create a ...
gonzaw's user avatar
  • 771

1
17 18
19
20 21
33