callable-object

How to document callable class with Sphinx?

房东的猫 提交于 2020-01-16 09:39:29
问题 I have a callable class with @dataclass annotation. I want to document it the way, so in the documentation, people can distinguish difference between __call__ and __init__ methods. Since I am using @dataclass __init__ is automatically generated. Example of the code in my case: @final @dataclass(frozen=True, slots=True) class Casting(object): """Cast one type of code to another. Constructor arguments: :param int_converter_function: function to convert to int :param float_converter_function:

Pyspark string pattern from columns values and regexp expression

…衆ロ難τιáo~ 提交于 2019-12-24 01:13:22
问题 Hi I have dataframe with 2 columns : +----------------------------------------+----------+ | Text | Key_word | +----------------------------------------+----------+ | First random text tree cheese cat | tree | | Second random text apple pie three | text | | Third random text burger food brain | brain | | Fourth random text nothing thing chips | random | +----------------------------------------+----------+ I want to generate a 3rd columns with a word appearing before the key_word from the

Capturing generic callable objects in nested lambdas - always forward?

有些话、适合烂在心里 提交于 2019-12-22 18:01:25
问题 I have various functions in my codebase that take a generic callable object and pass it to a series of nested lambdas before calling it. Example: template <typename TF> void interface(TF&& f) { nested0([/*...*/]() { nested1([/*...*/](auto& x) { nested2([&x, /*...*/]() { f(x); }); }); }); } Note that interface is taking a callable object of type TF by forwarding reference (previously known as universal reference) . The callable object is usually a lambda with various captured variables, both

Capturing generic callable objects in nested lambdas - always forward?

可紊 提交于 2019-12-06 14:04:45
I have various functions in my codebase that take a generic callable object and pass it to a series of nested lambdas before calling it. Example: template <typename TF> void interface(TF&& f) { nested0([/*...*/]() { nested1([/*...*/](auto& x) { nested2([&x, /*...*/]() { f(x); }); }); }); } Note that interface is taking a callable object of type TF by forwarding reference (previously known as universal reference) . The callable object is usually a lambda with various captured variables, both by value and by reference. What is the best (in terms of performance) way of capturing f in the nested

No matching function error when passing lambda function as argument

怎甘沉沦 提交于 2019-11-30 19:29:29
I have a list of numbers. I am trying to filter the list and only keep the positive numbers. I am trying to do it by passing a lambda as an argument. I wonder why I get function mismatch error. #include <vector> #include <algorithm> #include <functional> template<typename T> std::vector<T> keep( const std::vector<T> &original, std::function<bool(const T&)> useful) { std::vector<T> out; for(T item:original) { if(useful(item)) out.push_back(item); } return out; } int main() { std::vector<int> a={4,6,2,-5,3,-8,13,-11,27}; a=keep(a,[](const int& x)->bool{return x>0;}); for(int y:a) { std::cout<<y<

How to check if template argument is a callable with a given signature

ⅰ亾dé卋堺 提交于 2019-11-30 04:55:18
Basically, what I want to achieve is compile-time verification (with possibly nice error message) that registered callable (either a function, a lambda, a struct with call operator) has correct signature. Example (contents of the static_assert are to be filled): struct A { using Signature = void(int, double); template <typename Callable> void Register(Callable &&callable) { static_assert(/* ... */); callback = callable; } std::function<Signature> callback; }; Most of the answers are focused on basically answering the question: can you call the given function object with values of these types.

No matching function error when passing lambda function as argument

你离开我真会死。 提交于 2019-11-30 04:03:54
问题 I have a list of numbers. I am trying to filter the list and only keep the positive numbers. I am trying to do it by passing a lambda as an argument. I wonder why I get function mismatch error. #include <vector> #include <algorithm> #include <functional> template<typename T> std::vector<T> keep( const std::vector<T> &original, std::function<bool(const T&)> useful) { std::vector<T> out; for(T item:original) { if(useful(item)) out.push_back(item); } return out; } int main() { std::vector<int> a

How to check if template argument is a callable with a given signature

末鹿安然 提交于 2019-11-29 02:31:45
问题 Basically, what I want to achieve is compile-time verification (with possibly nice error message) that registered callable (either a function, a lambda, a struct with call operator) has correct signature. Example (contents of the static_assert are to be filled): struct A { using Signature = void(int, double); template <typename Callable> void Register(Callable &&callable) { static_assert(/* ... */); callback = callable; } std::function<Signature> callback; }; 回答1: Most of the answers are

What is the difference between __init__ and __call__?

断了今生、忘了曾经 提交于 2019-11-26 19:10:44
I want to know the difference between __init__ and __call__ methods. For example: class test: def __init__(self): self.a = 10 def __call__(self): b = 20 Cat Plus Plus The first is used to initialise newly created object, and receives arguments used to do that: class Foo: def __init__(self, a, b, c): # ... x = Foo(1, 2, 3) # __init__ The second implements function call operator. class Foo: def __call__(self, a, b, c): # ... x = Foo() x(1, 2, 3) # __call__ avasal Defining a custom __call__() method in the meta-class allows the class's instance to be called as a function, not always modifying the

What is the difference between __init__ and __call__?

帅比萌擦擦* 提交于 2019-11-26 06:50:42
问题 I want to know the difference between __init__ and __call__ methods. For example: class test: def __init__(self): self.a = 10 def __call__(self): b = 20 回答1: The first is used to initialise newly created object, and receives arguments used to do that: class Foo: def __init__(self, a, b, c): # ... x = Foo(1, 2, 3) # __init__ The second implements function call operator. class Foo: def __call__(self, a, b, c): # ... x = Foo() x(1, 2, 3) # __call__ 回答2: Defining a custom __call__() method in the