functor

How to call the __invoke method of a member variable inside a class

送分小仙女□ 提交于 2019-12-06 19:52:14
问题 PHP 5.4.5, here. I'm trying to invoke an object which is stored as a member of some other object. Like this (very roughly) class A { function __invoke () { ... } } class B { private a = new A(); ... $this->a(); <-- runtime error here } This produces a runtime error, of course, because there's no method called a. But if I write the call like this: ($this->a)(); then I get a syntax error. Of course, I can write $this->a->__invoke(); but that seems intolerably ugly, and rather undermines the

I can't understand Wikipedia's definition of “applicative functor”

旧时模样 提交于 2019-12-06 19:05:09
问题 Studying functors, applicative functors and monads in Haskell, I found this definition on Wikipedia: In functional programming, specifically Haskell, an applicative functor is a structure that is like a monad ( return , fmap , join ) without join , or like a functor with return . I can't understand: it seems to me that providing return (i.e. pure ) to a functor is not sufficient to obtain an applicative functor, because you need to provide ap (i.e. <*> ) too, which cannot be defined in terms

Functors with multiple arguments in OCaml

試著忘記壹切 提交于 2019-12-06 18:09:19
问题 I've the following situation: module type M = sig type s = ... end module Make(P: Something) : (M with type s = P.t) = struct type s = P.t ... end that works fine to generate modules of M type that use specific implementation of modules of type Something inside their implementation. Now suppose I have another module defined as module type AU = sig val feed : float -> unitv val nth : int -> (float -> float) val reset : unit -> unit end that has various implementations module SUAlg : AU =

Using a non-static class member inside a comparison function

左心房为你撑大大i 提交于 2019-12-06 16:02:47
I'm currently developing a syntaxic analyser class that needs, at a point of the code, to sort structs holding info about operators. Each operator has a priority, which is user-defined through public member functions of my analyser class. Thus, when sorting, I need my sorting function to order elements based on the priority of the corresponding operator. I'm using the following code to compare elements: bool parser::op_comp(const op_info& o1, const op_info& o2) { op_def& op1 = operators[o1.op_char]; op_def& op2 = operators[o2.op_char]; return op1.priority > op2.priority; } Note that I had to

How to use functor as a member in class template?

∥☆過路亽.° 提交于 2019-12-06 08:41:07
问题 I was trying to use a functor as a std::function object inside a class template. Below is what I have done so far. //! the functor class template template<typename T> struct func { void operator ()(T t) { std::cout << t << "\n"; } }; //! the class template that holds a std::function object as a member template<typename T> struct Foo { std::function<void(T)> bar = func<T>(); }; int main() { Foo<int> foo; return 0; } It was complained that error: conversion from 'func<int>' to non-scalar type

OCaml recursive modules across compilation units

给你一囗甜甜゛ 提交于 2019-12-06 05:45:57
I'm trying to split the following recursive modules into separate compilation units. Specifically, I'd like B to be in its own b.ml, to be able to reuse it with other A's. module type AT = sig type b type t = Foo of b | Bar val f : t -> b list end module type BT = sig type a type t = { aaa: a list; bo: t option } val g : t -> t list end module rec A : (AT with type b = B.t) = struct type b = B.t type t = Foo of b | Bar let f = function Foo b -> [ b ] | Bar -> [] end and B : (BT with type a = A.t) = struct type a = A.t type t = { aaa: a list; bo: t option } let g b = let ss = List.flatten (List

Is it possible to retrieve the argument types from a (Functor member's) function signature for use in a template?

旧时模样 提交于 2019-12-06 05:16:33
问题 Assume you have a functor: struct MyFunctor { bool operator ()( int value ) { return true; } }; Is it possible to retrieve a functor's member's argument type for use within your template? The following is a use of this mythical functionality: template < typename FunctorType > bool doIt( FunctorType functor, typename FunctorType::operator()::arg1 arg ) { return functor( arg ); } Is there a valid syntax that would substitute for my mythical FunctorType::operator()::arg1 ? 回答1: No there is not.

Am I reinventing the wheel with this trivial method call forwarding class?

心已入冬 提交于 2019-12-06 03:53:57
问题 I just found myself creating a class template <typename T> struct invoker { void operator()(T& it) const {it();} }; so I could pass an invoker<foo> to something (which isn't under my control) which wants to call invoker<foo>::operator()(foo&) on it repeatedly with different foo instances, to get it to forward those calls to the foo 's foo::operator()() method. I know it's only a few lines, but this seems like the sort of thing which is probably already provided for by STL's functional, or

functor generation from member function pointer type

倖福魔咒の 提交于 2019-12-06 03:33:01
问题 I am trying to simplify (via make_fn() ) the generation of functors that preprocess parameters (via wrap() ) for member functions of arity n . Generating the functors is basically working, but until now only by explicitly specifying the parameter types for the member function. Now i'd like to generate the correct functor from the member function type it handles: struct X {}; template<class C, typename T1, bool (C::*F)(T1)> inline // there are more for T1..TN bool wrap(C* c, X x) { return (c->

How to add a class constraint to a Functor instance declaration in Haskell?

徘徊边缘 提交于 2019-12-06 02:45:45
问题 I have defined the following data type: data Probability a = PD { mass :: [(a, Ratio Int)] } Now I want to write that it is an instance of Functor : collect :: (Eq a, Num b) => [(a, b)] -> [(a, b)] collect al = map collect' keys where keys = nub $ map fst al collect' k = (k, sum (map snd (matches k))) matches k = filter ((==) k . fst) al instance (Eq a) => Functor (Probability a) where fmap f p = PD (collect $ map (first f) (mass p)) However, I get the following error: Kind mis-match The