1,636
questions
1
vote
2
answers
133
views
std::function to functions with default argument
#include <iostream>
#include <functional>
using MyFunctionAlias = std::function<void(int, double)>;
void exampleFunction(int value, double value2 = 3.14) {}
void exampleFunction2(...
1
vote
0
answers
43
views
How to restrict the type of an instance when the instance requires a "* -> *" type in Haskell? [duplicate]
I'm trying to design a type that holds some value and keeps track of all operations done upon it. It does this by storing them in string format (this is not for a project, just learning)
I tried to ...
1
vote
1
answer
126
views
Constructor classes: why not mention the content's type?
Following on from this q about monad transformers ...
Before constructor classes were introduced [M.P.Jones 1993], Haskell class decls:
Must have a single parameter;
That parameter must be kind Type (...
0
votes
2
answers
51
views
Implementing functions in module types
I have the following module type:
module type Element = sig
type t
type mystruct = {x: int; y: int}
val (<) : t -> t -> bool
val string_of_element : t -> string
(* val (>) : t ...
4
votes
3
answers
154
views
How does <$ = (fmap . const) in Functor even work in Haskell?
I know that the dot (.) operator takes two functions which both take an argument respectively, and the third argument for the second argument.
Its type is (.) :: (b -> c) -> (a -> b) -> a -...
2
votes
1
answer
87
views
Why I get error "The type variable 'a occurs inside 'a t" with enabled -rectypes flag
I would like to write catamorphism in OCaml for any endofunctor (in terms of types) as a functor (in terms of OCaml):
module type Functor = sig
type 'a t
(* ... *)
end
module Make(F : ...
1
vote
1
answer
66
views
Menhir Parser Functor?
Is it possible to make Menhir generate a functor? Or something to that effect?
For a small example, say you have a family of languages that share the same structure except for the value type and ...
1
vote
1
answer
163
views
Efficiency advantage of C++ 23 static operator()
I'm looking at the motivation for static operator() laid out in this article
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p1169r2.html
where they explain that function objects, frequently ...
1
vote
1
answer
102
views
How to deal with Monad, Functor and Applicative in order to write stateful code
I am attempting to write some stateful code in Haskell. To this end, I follow this material. At this point, I got my way to monads and functors, and, roughly put, I am confused and can't make progress ...
0
votes
0
answers
72
views
c++ How to design/declare "callbacks" that support a comparison operation - need to find and remove callbacks from list
I believe this is not a duplicate question. Other similar questions ask something like "how can I compare std::function objects for equivalence/equality?". The answer seems to be simply &...
0
votes
1
answer
65
views
Functors not recognized as transform_view args
In my mind I have this mental model of considering these two expressions being synonymous
R | std::views::transform([](auto&& e) { return f(e); });
R | std::views::transform(f); // good, ...
0
votes
1
answer
78
views
Declaration of operator function inside struct
I was reading this document for templates:
An Idiot's Guide to C++ Templates - Part 2
I came across one definition that I did not understand:
struct Currency
{
int Dollar;
int Cents;
operator ...
0
votes
1
answer
46
views
Searching a way to use different variadic functors basing on condition
I have a runtime boolean verbose that if false excludes some prints to stdout.
To fix ideas, consider to have a lot of parts like:
void add(const int a, const int b, const bool verbose)
{
//...
...
0
votes
1
answer
48
views
What causes this type mismatch when applying a functor?
I have the following files:
SetMaker.mli
module type Element = sig
type t
val create : 'a -> t
val compare : t -> t -> int
val to_string : t -> string
end
module type Set = sig
...
1
vote
3
answers
80
views
What is the right way to put type declarations in OCaml signatures
I have written the following two files, and although it works, it feels odd that the code would duplicate these long type declarations. On the other hand, if I remove either module type declaration ...
2
votes
1
answer
69
views
Using infix operator in the module in OCaml
I am trying to define a SCALAR signature with its own infix operator in
module type SCALAR =
sig
type t (* type of scalar element *)
val zero : t
val one : ...
0
votes
1
answer
149
views
is there a predefined functor for conditional operator in c++?
Predefined functors are used as wrappers for operators in c++ to use, for example, in STL algorithms. There seems to be a wrapper for all operators except conditional. Is it missing? If yes, why?
...
12
votes
3
answers
760
views
Every Lens' is a Traversal'... how?
Control.Lens.Tutorial says:
type Traversal' a b = forall f . Applicative f => (b -> f b) -> (a -> f a)
type Lens' a b = forall f . Functor f => (b -> f b) -> (a -> f ...
0
votes
0
answers
11
views
QUERY Lambda Expression "[this]" : StackOverflow
With regards to lambda expressions, what is the meaning of "[this]"?
I have rarely seen "[this]" and Google does not turn up any answers.
According to https://en.cppreference.com/w/...
0
votes
1
answer
81
views
Why does this program crash or hang?
The following code crashes or hangs until memory exhaustion:
#include <iostream>
#include <functional>
using namespace std::placeholders;
struct Goal {float x; float y;};
class Boxbot
{
...
1
vote
3
answers
189
views
How to write typed wrapper for a thread pool?
I have a simple thread pool. It takes tasks and distributes them among threads using round-robin.
The task looks like this
using TaskFn = void (*)(void*);
struct Task {
TaskFn fn;
void* args;
...
1
vote
2
answers
179
views
Linear combination of functions in C++
Let's say we have the following:
// some functions
double f1( double x );
double f2( double x );
double f3( double x );
// coefficients
double c1, c2, c3;
// input variable
double x;
We need to ...
0
votes
0
answers
69
views
is_callable emulation in C++14: update on old question
Looking a trait to tell if a class can be used as a callable, whatever the call parameters, I found this interesting post: Find out whether a C++ object is callable.
I found this trait more powerful ...
0
votes
1
answer
41
views
Idris: Cannot use functions as applicative functors?
In Haskell I am wont to use liftA2 on functions as the S' combinator. This works because the Haskell STL instantiates Functor and Applicative for functions (see https://hackage.haskell.org/package/...
-1
votes
1
answer
90
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 (...
0
votes
1
answer
98
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 ...
0
votes
1
answer
85
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 ...
0
votes
4
answers
143
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]] -- ...
3
votes
1
answer
131
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 ...
1
vote
1
answer
46
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 ...
3
votes
0
answers
122
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 ...
0
votes
1
answer
99
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}} ...
6
votes
1
answer
186
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 ...
4
votes
3
answers
239
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>
...
0
votes
1
answer
83
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
{
...
1
vote
1
answer
161
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
{
...
2
votes
1
answer
179
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(...
2
votes
3
answers
117
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)
{
...
3
votes
1
answer
129
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 ...
1
vote
0
answers
22
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<...
1
vote
1
answer
128
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 ...
2
votes
0
answers
199
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), ...
2
votes
2
answers
239
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/...
1
vote
0
answers
72
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(...
0
votes
0
answers
102
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 ...
1
vote
1
answer
91
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 ...
0
votes
1
answer
222
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)
{
...
0
votes
1
answer
38
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, ...
0
votes
1
answer
608
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 ...
1
vote
2
answers
86
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
...