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.

Filter by
Sorted by
Tagged with
-1 votes
1 answer
77 views

Making a function an instance of functor

I'm attempting to implement functor for a record object that has a function attribute, something like this: data Function a = Function { , funcApply :: FData -> [Exp a] -> Either (...
Chris J Harris's user avatar
0 votes
0 answers
57 views

Changing an inlined global function to a constant propagated functor argument

I made a change to some code to increase user flexibility. However, this worsened performance in the original use cases. The original code made a call to a global function, and the new code made the ...
Bobby's user avatar
  • 31
0 votes
1 answer
84 views

Haskell Error: Expected kind ‘* -> *’, but ‘Movie’ has kind ‘*’

I created a "Movie" algebraic data type (as requested by the task): data Movie = Movie { title :: String, director :: String, releaseYear :: Int} Then added a functor: instance Functor ...
EvilROMA's user avatar
0 votes
1 answer
54 views

How to use a class based member function as an callback function in curl_easy_setopt() in cpp

I am writing a program where I have defined an event handler function as a class member function in file 'one.cpp'. My custom http client is defined in other file 'second.cpp'. Initially I had written ...
Sumit Patil's user avatar
0 votes
4 answers
129 views

Functor over multiple levels

I have this lame attempt: fmap2 :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b) fmap2 f f2 = (fmap2 f . fmap f2) It is supposed to work like this: fmap2 negate [[1,2], [3]] -- ...
coderodde's user avatar
  • 1,359
3 votes
1 answer
126 views

Can we always use <$> in Haskell to define functions "point free"?

I have been learning just how powerful the <$> and <*> operators are in Haskell, and how it is possible to define some functions without parameters where they would normally be needed. I ...
FuzzyCat444's user avatar
1 vote
1 answer
40 views

Why typescript cannot refer the type in my example correctly?

Considering the code for Option ADT as below (pretty similar to fp-ts Option): export type Option<A> = Some<A> | None export interface Some<A> { _tag: 'Some' value: A } export ...
Shnd's user avatar
  • 1,868
3 votes
0 answers
119 views

C++ STL: The third parameter of sort(), why the functor is faster than inline function? [duplicate]

inline bool mycmp(int i, int j) { return (i < j); } class mycmp2 { public: bool operator()(int i, int j) { return (i < j); } }; above is my example. I want know why the ...
xf h's user avatar
  • 39
0 votes
1 answer
62 views

Implementing the Option Functor in Julia - while maintaining type stability

First off, I have created the following attempt at an Option-type in Julia: # building an Option-type abstract type MyNothing end struct None{T} <: MyNothing end Option = Union{Some{T},None{T}} ...
David Reynolds's user avatar
6 votes
1 answer
148 views

Is this "Coapplicative" class a superclass for Comonad?

Recall the Applicative class: class Functor f => Applicative f where pure :: a -> f a liftA2 :: (a -> b -> c) -> f a -> f b -> f c (<*>) :: f (a -> b) -> f ...
Dannyu NDos's user avatar
  • 2,478
4 votes
3 answers
149 views

With C++ CRTP, how do I infer the parameters of a function in the derived class?

I'm trying to infer the return type and args of a function from within the CRTP base class in order to "wrap" functors. Starting with the basic CRTP pattern: template<typename Deriv> ...
user5406764's user avatar
  • 1,679
0 votes
1 answer
79 views

distinguish between functions and classes (functor)

I have both a function and class called GreaterThan. template <typename T> bool GreaterThan(const T& number) { return number > 100; } template <typename T> class GreaterThan { ...
quant's user avatar
  • 47
1 vote
1 answer
123 views

What is a proper way to declare priority queue as a private member in class with custom compare function?

Assume I have a class in c++: struct CompareTuple { public: auto operator() (const Tuple& t1, const Tuple& t2) -> bool { // do comparison and return } }; class Foo { ...
Auzdora's user avatar
  • 23
2 votes
1 answer
130 views

Function composition with functor - Performance overhead

As we know, in C++ 11, can't deduce the return type of function returning lambda with auto or decltype(auto). So can't use code like below. template <typename F, typename G> constexpr decltype(...
lighthouse's user avatar
2 votes
3 answers
99 views

How to define and use std::less as a template argument in a function definition?

What is the proper C++ syntax to supply and use std::less as a template argument? #include <iostream> #include <functional> template<typename CMP> float order(float a, float b) { ...
wcochran's user avatar
  • 10.2k
3 votes
1 answer
77 views

C++ Overloaded function call is ambiguous

I was playing around with C++ std::function objects, and wanted to add some functionality to them, so I subclassed them. However, when I tried to create a function that overloaded based on the ...
danschultewhpc's user avatar
1 vote
0 answers
19 views

c++ How to place definition of static functors in class implementation file?

I'm using static functors (to be used everywhere) defined in the base class like: /* Base.hpp */ class Base { ... static struct Cpyz_t cpy_z; }; struct Cpyz_t { private: template<...
atlantis23's user avatar
1 vote
1 answer
90 views

A function as an argument in Julia (best practice / code design)

What is the best/most elegant code design in the Julia programming language for the following problem: The modules mod1, mod2, mod3, ... implement a complicated function fun(a,b,c), which calculates ...
thyme's user avatar
  • 398
1 vote
0 answers
163 views

Which kinds of lawfull `Functor`s are possible in Haskell?

I know following types of Functors: Polynomial functor == simple not-nested parameterized datatype Generalization of 1, by using "parameter removing" newtypes: Fix, Some (existentials), ...
uhbif19's user avatar
  • 3,169
2 votes
2 answers
177 views

Monad and Functor law for Monad and Functor type class in Haskell

I am a somewhat experienced functional programmer, but I have always been confused by the statement “A monad is just a monoid in the category of endofunctors” and the actual implementation of monads/...
SmoothTraderKen's user avatar
0 votes
0 answers
20 views

Advise on microsoft visual C++ unit test feature ExpectException

I have a class with a method called func and it takes a bool argument and returns void. class Test { public: void func (bool b); } To test it, I use VS UnitTest framework and would like to test ...
Iman Abdollahzadeh's user avatar
1 vote
0 answers
60 views

Why is the result of the following functor not opaque?

Consider the following SML program: signature BinaryOp = sig type domain val operation: (domain * domain) -> domain end; signature INTEGER = sig val value: int end; functor ModularInt(...
Marco's user avatar
  • 499
0 votes
0 answers
83 views

Segfault when calling functor or function pointer

I have a class which can create urls, and thread which asks instances for urls and runs them with curl (thread managed by static members of the class, instances are added to the list, etc, omitted for ...
herhor67's user avatar
1 vote
1 answer
74 views

Define Writer Functor

Given any category C with products and an object c in C, we can define a functor _ × c : C -> C which maps an object a to a × c and a morphism f : a -> b to f × idc : a × c -> b × c. I would ...
CCB's user avatar
  • 131
0 votes
1 answer
112 views

Operands to ?: have different types with matching auto functors [duplicate]

I have the following, which does not compile: void func(std::optional<std::function<void(int, int)>> arg1) { return; } void callee(bool flag) { auto arg1 = [](int a1) { ...
Tyler Kelly's user avatar
0 votes
1 answer
36 views

how to overload and print map<map<list<int>*, vector<int>, myFunctorClass>*, int, myFunctorClass> M1

Im trying to print map<map<list, vector, myFunctorClass>, int, myFunctorClass> M1 as following: map<map<list<int>*, vector<int>, myFunctorClass>*, int, ...
Jack Yanchen Li's user avatar
0 votes
1 answer
258 views

It is Good Practice to Assign Lambda Expression to a Variable in C++? [closed]

Is it considered as good practice to assign lambda expressions in C++ to a variable? I know from python, that it isn't good practice Which is more preferable to use: lambda functions or nested ...
bilaljo's user avatar
  • 368
1 vote
2 answers
61 views

ruby - can you iterate over a set of declared functions with lambda or functors?

in ruby if i have n functions: def a puts "a" end def b puts "b" end is there any way to do an iteration like [a,b].each do |x| puts "running #{x.name} run a ...
user1130176's user avatar
  • 1,802
1 vote
1 answer
79 views

OCaml functor Set.Make/Map.Make weird annotation in VSCode

Why in the world is there a /1. between Ord: Set and OrderedType -> ? This shows up for Set.Make and for Map.Make in VSCode environment, and possibly for other built-in Make functors as well. See ...
Yanny's user avatar
  • 25
2 votes
1 answer
140 views

Idiomatic replacement for a bespoke functor-with-callbacks class?

Background If you're looking at some codebase, and you see something like: class A { virtual ~A() = default; virtual void act() = 0; }; you would likely say: "Oh, this is basically just ...
einpoklum's user avatar
  • 121k
-1 votes
2 answers
658 views

In general, what is a pythonic way to write a decorator? [duplicate]

In general, I want to know how to write a simple decorator in python. However, it might help to have a specific example. Consider the following function: def pow(base:float, exp:int): ""&...
Toothpick Anemone's user avatar
2 votes
2 answers
134 views

What is (->) in Haskell?

If you type :i (->) in GHCi and hit Enter it returns the following: data (->) t1 t2 -- Defined in ‘GHC.Prim’ infixr 0 `(->)` instance Monad ((->) r) – Defined in ‘GHC.Base’ instance ...
Skab's user avatar
  • 33
0 votes
2 answers
72 views

C++ selective predefined functor initializaiton [duplicate]

Predefined functors need to be in-place instantiated (with empty parentheses) for use in algorithms but not as type parameters for container adapters such as priority_queue. Why the difference? #...
Mikhail's user avatar
  • 173
0 votes
0 answers
46 views

In C++, can a functor be a static member of another class? [duplicate]

For various reasons, I want to make a functor that is a static member of another class. However, this SOMETIMES causes a linker error. Is this not legal? If so, why not? Here's a minimal example. #...
Yen Lee Loh's user avatar
3 votes
2 answers
217 views

How to understand the type of (fmap fmap fmap) in Haskell?

In Haskell, the Functor has a function fmap which the type of it is: ghci> :t fmap fmap :: Functor f => (a -> b) -> f a -> f b This makes sense to me that fmap lifts a function from ...
Hao Yang's user avatar
0 votes
2 answers
105 views

In Scala cats-laws, why is the functor composition law different from canonical definition?

The (covariant) functor definition in cats-laws looks like this: def covariantComposition[A, B, C](fa: F[A], f: A => B, g: B => C): IsEq[F[C]] = fa.map(f).map(g) <-> fa.map(f.andThen(...
tribbloid's user avatar
  • 3,756
2 votes
3 answers
182 views

Haskell : what is the sense in: instance Functor ((->) r)

I admit, that my question may stem from a lack of knowledge and be rather vague. But I try to understand, have some doubts and can't resolve them. So GHC.Base have such definition, and what is the ...
Konstantin Kazartsev's user avatar
0 votes
0 answers
75 views

What is operator( ) instead of operator( ) ( )? [duplicate]

I have seen class in a library I am using, there is a use of operator overloading that I haven't seen and I can't understand. I can't call it, and it's not the typical parentheses operator overloading ...
Zebrafish's user avatar
  • 12.1k
0 votes
0 answers
90 views

In Scala cats 2.x, how to create a generic zero functor (given its type class)?

I'm looking for a method to initialise a zero functor z: F[_], given the type class Functor[F[_]]. The zero functor should ensure that any other function compose with it yields a fixed point. I've ...
tribbloid's user avatar
  • 3,756
1 vote
1 answer
206 views

Implementing Functor concept in C++

Recently I've been trying to create a C++ concept around Haskell's Functor. Something like this: template <template<class>class F> concept Functor = requires (F<A> f) { { fmap(...
Dragomir's user avatar
2 votes
1 answer
69 views

Pushing back lambda to vector of functors produces infinite constructor calls

I've got an interesting puzzle that I can't seem to completely solve. The following code is a snipped for my own function implementation. When I try to push_back a lambda into a vector of this ...
glades's user avatar
  • 3,925
1 vote
1 answer
486 views

TypeScript: How to pass function-type member variable by reference?

I have an object with a function-type member variable, and a setter function which sets any given function-type variable of that same signature to some function: class Foo { // This is the ...
goose_lake's user avatar
2 votes
1 answer
121 views

I can't get to work this Instances of Show / Functor for my Forest datatype

I've started programming in haskell like 2 months ago, im fairly new so don't expect me to be some top tier expect at monads or whatever please. I have tried in so many ways to get this Forest ...
haskellfan420's user avatar
0 votes
1 answer
248 views

The module Map.Make is a functor, it cannot have any components

I want to have a Map datastructure for "env" in my lisp interpreter. My code in env.mli is module Env = Map.Make(String) type t = (Ast.expression) Env.t val empty: t val add: string -> ...
Murphy's user avatar
  • 27
2 votes
2 answers
382 views

What is the difference between functor and monad Intuitively

I have learned the definitions of functor and monad, But I am still unable to figure out the difference between them except for the definitions. I have read some answers to this problem, In What is ...
yixing's user avatar
  • 430
1 vote
2 answers
178 views

std::accumulate won't' perform sum of elements with std::plus functor

I have a class which contains an integer among some other things: class Foo { public: Foo() = default; Foo(int x) : x(x) {} int operator+(const Foo& rhs) { ...
IRP_HANDLER's user avatar
1 vote
1 answer
183 views

What optimization allows Functors to be inlined? [duplicate]

At compile-time, I want to pass a (stateless) function as efficiently as possible. I see two options: template<typename Functor> bool PassAsArgument(int arg1, int arg2, Functor functor); and ...
mbang's user avatar
  • 855
0 votes
1 answer
26 views

Is a simple way to use `Belt.Set` (and others) fixing its type?

I'm creating a functor for opaque identifiers to allow the type system help catching issues where you're supposed to have an identifier of A, but you're being given an identifier of B. I've already ...
manu's user avatar
  • 3,614
1 vote
1 answer
142 views

save result of for_each algorithm inside a vector

I have a functor and the overloaded operator() of it returns a double. In an easy way I can write: int main(){ auto f=[](double t){return 1.0/(pow(7.0*t,2.0)+1.0);}; std::vector<double> ...
Suslik's user avatar
  • 1,011
5 votes
1 answer
263 views

Can `Fix` and `(,)` be seen as functors in some sense?

I've been wondering what a complete, all-encompassing context for instance Functor (f :.: g) would be. The immediate thought that pops into my head is: newtype (f :.: g) a = Comp (f (g a)) instance (...
fluffyyboii's user avatar

1
2 3 4 5
33