functor

sigc unbind? (or, deconstruct a functor?)

旧时模样 提交于 2019-12-13 04:35:14
问题 Suppose I have used sigc::bind to construct a callback functor. Suppose I have registered that callback with another class -- in my case it's a scheduling class. At the right time, the scheduling class will call my callback, with all the correctly-bound arguments. And that works fine. But suppose I want a debugging interface on that other class, that will give me my callbacks back (as data objects, that is, not by calling them). That is, in the case of the scheduling class, suppose I want a

Prolog: Replace an atom by other atom in compound terms

旧时模样 提交于 2019-12-13 00:03:51
问题 I am trying to write a simple prolog program which should replace an atom by another atom. The program can take complex functor as an input and would replace all the atoms by another atoms. e.g. In the below term, I want to replace atom a by ax only where I encounter a leaf (i.e. not a functor name). replace(put(a,table,aside(a(dont,replace),a)),X). which should produce output, X = put(ax,table,aside(a(dont,replace),ax)); false. In the above output, replaced a with ax everywhere except the

Typeclass instances for functions

随声附和 提交于 2019-12-12 17:35:20
问题 I've just realized, that Functions have instances for Monad, Functor and Applicative. What I usually do, when I see some typeclass instance I don't get, is write some well-typed expression and see what it returns: Can somebody explain these instances? You usually hear about the instances for List and Maybe, which by now are natural to me, but I don't understand how Functions can be a Functor or even a Monad. EDIT: Okay, this is a valid well-typed expression that doesn't compile: fmap (+) (+)

C++ struct sorting error

六眼飞鱼酱① 提交于 2019-12-12 16:14:02
问题 I am trying to sort a vector of custom struct in C++ struct Book{ public:int H,W,V,i; }; with a simple functor class CompareHeight { public: int operator() (Book lhs,Book rhs) { return lhs.H-rhs.H; } }; when trying : vector<Book> books(X); ..... sort(books.begin(),books.end(), CompareHeight()); it gives me exception "invalid operator <" What is the meaning of this error? Thanks 回答1: sort expects a function that returns bool , which is true iff the lhs precedes the rhs: bool operator() (const

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

北战南征 提交于 2019-12-12 15:03:43
问题 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 want to do the sorting in. Basically, my code looks like this (simplified to only show the relevant parts): MappingTechnique.h struct MappingTechnique { vector<int> usedIndexCount; }; struct SimpleGreedyMappingTechnique : MappingTechnique { bool sortByWeights(int index1, int index2); };

Difference between higher order and curried functions

你说的曾经没有我的故事 提交于 2019-12-12 10:54:11
问题 I'm reading a book, Functional Programming Using F#, which says (page 33), in the section Declaration of higher-order functions We have seen higher-order built-in functions like (+) and (<<) and at the end of the section Higher-order functions may alternatively be defined by supplying the arguments as follows in the let-declaration: let weight ro s = ro * s ** 3.0;; However there were some helpful comments at the bottom of a question that I asked earlier today (which was originally titled

Functors and Applicatives for types of kind (* -> *) -> *

你离开我真会死。 提交于 2019-12-12 09:31:45
问题 I ran into a situation where my code would benefit from using Functor and Applicative -like abstractions, but for types of kind (* -> *) -> * . Defining a higher-kinded functor can be done with RankNTypes like this class HFunctor f where hfmap :: (forall x. a x -> b x) -> f a -> f b But the higher kind version of Applicative is a bit trickier. This is the best I could come up with: class HFunctor f => HApplicative f where hpure :: (forall x. a x) -> f a (<**>) :: f (a :-> b) -> f a -> f b

How to use Scala Cats Validated the correct way?

╄→гoц情女王★ 提交于 2019-12-12 08:25:01
问题 Following is my use case I am using Cats for validation of my config. My config file is in json. I deserialize my config file to my case class Config using lift-json and then validate it using Cats. I am using this as a guide. My motive for using Cats is to collect all errors iff present at time of validation. My problem is the examples given in the guide, are of the type case class Person(name: String, age: Int) def validatePerson(name: String, age: Int): ValidationResult[Person] = {

Is an is_functor C++ trait class possible?

余生长醉 提交于 2019-12-12 07:09:58
问题 How can I deduce statically if an argument is a C++ function object (functor)? template <typename F> void test(F f) {} I tried is_function<F>::value , but this doesn't work. It also seems there is no is_functor trait, so perhaps it's not possible. I appear to be only looking for a specific member function, in this case the function call operator: F::operator() . 回答1: It is possible to create such a trait, with two restrictions: For the compiler, a free function is something fundamentally

How do I define a sort predicate for a templated container class in C++

为君一笑 提交于 2019-12-12 04:28:50
问题 My C++ is a little rusty as of late. Can one of you gurus help me define a SORT predicate, for a Container Class, with a template parameter which it self is a another class. template <class Element> class OrderedSequence // Maintains a sequence of elements in // ascending order (by "<"), allowing them to be retrieved // in that order. { public: // Constructors OrderedSequence(); OrderedSequence(const OrderedSequence<Element>&); // Destructor ~OrderedSequence(); // destructor OrderedSequence