passing functor as function pointer

You cannot directly pass a pointer to a C++ functor object as a function pointer to C code (or even to C++ code). Additionally, to portably pass a callback to C code it needs to be at least declared as an extern “C” non-member function. At least, because some APIs require specific function call conventions … Read more

How do I get the argument types of a function pointer in a variadic template class?

You can write function_traits class as shown below, to discover the argument types, return type, and number of arguments: template<typename T> struct function_traits; template<typename R, typename …Args> struct function_traits<std::function<R(Args…)>> { static const size_t nargs = sizeof…(Args); typedef R result_type; template <size_t i> struct arg { typedef typename std::tuple_element<i, std::tuple<Args…>>::type type; }; }; Test code: struct … Read more

Why are Promises Monads?

UDATE. See this new library providing functor and monad operators for plain callback-based functions that do not have the issues with theneables: https://github.com/dmitriz/cpsfy The JS Promise is neither a Functor nor an Applicative nor a Monad It is not a functor, because the composition preservation law (sending compositions of functions to compositions of their images) … Read more

Differences between functors and endofunctors

A functor may go from one category to a different one, an endofunctor is a functor for which start and target category are the same. Same as with endomorphisms versus morphisms. Now, why must monads be endofunctors? There is the famous quote that “Monads are just monoids in the category of endofunctors”. Fortunately, somebody else … Read more

Fun with repeated fmap

I can’t explain why, but here’s the proof of the cycle: Assume k >= 2 and fmap^(4k) :: (a -> b) -> F1 F2 F3 a -> F1 F2 F3 b, where Fx stands for an unknown/arbitrary Functor. fmap^n stands for fmap applied to n-1 fmaps, not n-fold iteration. The induction’s start can be verified … Read more

Why does a js map on an array modify the original array?

You’re not modifying your original array. You’re modifying the objects in the array. If you want to avoid mutating the objects in your array, you can use Object.assign to create a new object with the original’s properties plus any changes you need: const freeProduct = function(products) { return products.map(x => { return Object.assign({}, x, { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)