language-details

How to yield empty generator?

…衆ロ難τιáo~ 提交于 2019-12-19 05:17:28
问题 I have a method which takes a generator plus some additional parameters and yields a new generator: function merge(\Generator $carry, array $additional) { foreach ( $carry as $item ) { yield $item; } foreach ( $additional as $item ) { yield $item; } } The usual use case for this function is similar to this: function source() { for ( $i = 0; $i < 3; $i++ ) { yield $i; } } foreach ( merge(source(), [4, 5]) as $item ) { var_dump($item); } But the problem is that sometimes I need to pass empty

Why is (void) 0 a no operation in C and C++?

依然范特西╮ 提交于 2019-12-17 15:16:06
问题 I have seen debug printfs in glibc which internally is defined as (void) 0 , if NDEBUG is defined. Likewise the __noop for Visual C++ compiler is there too. The former works on both GCC and VC++ compilers, while the latter only on VC++. Now we all know that both the above statements will be treated as no operation and no respective code will be generated; but here's where I've a doubt. In case of __noop , MSDN says that it's a intrinsic function provided by the compiler. Coming to (void) 0 ~

How to write abstract class constructors so that it will be flexible for extending in sub classes

江枫思渺然 提交于 2019-12-13 00:38:28
问题 I am trying to implement a persistent Stack data structure. I want to implement this as an algebraic data type, so it has two concrete subtypes: empty and non empty : abstract class Stack<T> { factory Stack.empty() => const _EmptyStack._(); T get data; Stack<T> get bottom; bool get isEmpty; Stack<T> put(T item) => new _StackImpl(item, this); } class _StackImpl<T> extends Stack<T> { final T _data; final Stack<T> _bottom; _StackImpl(T this._data, Stack<T> this._bottom); T get data => _data;

How to yield empty generator?

半城伤御伤魂 提交于 2019-12-01 02:58:03
I have a method which takes a generator plus some additional parameters and yields a new generator: function merge(\Generator $carry, array $additional) { foreach ( $carry as $item ) { yield $item; } foreach ( $additional as $item ) { yield $item; } } The usual use case for this function is similar to this: function source() { for ( $i = 0; $i < 3; $i++ ) { yield $i; } } foreach ( merge(source(), [4, 5]) as $item ) { var_dump($item); } But the problem is that sometimes I need to pass empty source to the merge method. Ideally I would like to be able to do something like this: merge(\Generator:

range for integer values of chars in c++

被刻印的时光 ゝ 提交于 2019-11-28 12:00:50
I'm reading The C++ Programming Language and in it Stroustrup states that the int value of a char can range from 0 to 255 or -127 to 127, depending on implementation. Is this correct? It seems like it should be from -128 to 127. If not, why are their only 255 possible values in the second implementation possibility, not 256. Roddy You're stuck in two's complement thinking - The C++ standard does not define the representation used for negative numbers! If your computer (god forbid) uses ones's complement to represent negative numbers, you have a range of -127 to + 127 in an 8-bit byte. On the

Why is (void) 0 a no operation in C and C++?

馋奶兔 提交于 2019-11-27 17:23:29
I have seen debug printfs in glibc which internally is defined as (void) 0 , if NDEBUG is defined. Likewise the __noop for Visual C++ compiler is there too. The former works on both GCC and VC++ compilers, while the latter only on VC++. Now we all know that both the above statements will be treated as no operation and no respective code will be generated; but here's where I've a doubt. In case of __noop , MSDN says that it's a intrinsic function provided by the compiler. Coming to (void) 0 ~ Why is it interpreted by the compilers as no op? Is it a tricky usage of the C language or does the

When should I use @classmethod and when def method(self)?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 10:31:25
While integrating a Django app I have not used before, I found two different ways used to define functions in classes. The author seems to use them both very intentionally. The first one is one I myself use a lot: class Dummy(object): def some_function(self,*args,**kwargs): do something here self is the class instance The other one is one I do not use, mostly because I do not understand when to use it, and what for: class Dummy(object): @classmethod def some_function(cls,*args,**kwargs): do something here cls refers to what? In the Python docs the classmethod decorator is explained with this

range for integer values of chars in c++

二次信任 提交于 2019-11-27 06:40:34
问题 I'm reading The C++ Programming Language and in it Stroustrup states that the int value of a char can range from 0 to 255 or -127 to 127, depending on implementation. Is this correct? It seems like it should be from -128 to 127. If not, why are their only 255 possible values in the second implementation possibility, not 256. 回答1: You're stuck in two's complement thinking - The C++ standard does not define the representation used for negative numbers! If your computer (god forbid) uses ones's

When should I use @classmethod and when def method(self)?

孤者浪人 提交于 2019-11-26 15:13:58
问题 While integrating a Django app I have not used before, I found two different ways used to define functions in classes. The author seems to use them both very intentionally. The first one is one I myself use a lot: class Dummy(object): def some_function(self,*args,**kwargs): do something here self is the class instance The other one is one I do not use, mostly because I do not understand when to use it, and what for: class Dummy(object): @classmethod def some_function(cls,*args,**kwargs): do