Re: [PHP-DEV] Support negative indexes for arrays and strings
On Mon, June 4, 2012 2:08 pm, Marc Easen wrote: I have submitted a patch to support negative indexs in strings, as per the conversation adding them to arrays could possibly detract from the syntactical sugar they are indented to be. In summary: An alternative to: $var = 'abc'; echo $var[strlen($var) - 1]; Can be: $var = 'abc'; echo $var[-1]; This seems simple enough for a hard-coded -1, but... Would $var[-2] be strlen($var) - 2 and so on? And then one would expect some rather complex logic to compute -N for $var[-N] At that point, this becomes a pretty big WTF, imho. I've never honestly felt it to be a big burden to use strlen($var) - 1, so I don't really see the point, at least for me. -- brain cancer update: http://richardlynch.blogspot.com/search/label/brain%20tumor Donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclickhosted_button_id=FS9NLTNEEKWBE -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Support negative indexes for arrays and strings
Hi! Can be: $var = 'abc'; echo $var[-1]; This seems simple enough for a hard-coded -1, but... Would $var[-2] be strlen($var) - 2 and so on? The main question is what happens with foo[-4] or ['x'][-2]. And then one would expect some rather complex logic to compute -N for $var[-N] I don't see much of complex logic here, but $a[2] = 'a' would create a new array element if it does not exist, while $a[-2] can't. Not a big issue, but somewhat inconsistent I guess. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Support negative indexes for arrays and strings
On Mon, June 11, 2012 2:13 pm, Stas Malyshev wrote: And then one would expect some rather complex logic to compute -N for $var[-N] I don't see much of complex logic here, but $a[2] = 'a' would create a new array element if it does not exist, while $a[-2] can't. Not a big issue, but somewhat inconsistent I guess. $n = some_incredibly_long_and_complex_computation(); //and, for real fun, sometimes it returns positive, and sometimes negative. $s = $var[$n]; //a few hundred lines later, buried somewhere else if ($n 0){ //whatever } else{ //something entirely different } Sooner or later, somebody will do that, and I really don't want to have to un-tangle all that mess to figure out if $n is positive or negative for the sake of saving a few keystrokes... -- brain cancer update: http://richardlynch.blogspot.com/search/label/brain%20tumor Donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclickhosted_button_id=FS9NLTNEEKWBE -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Support negative indexes for arrays and strings
On Mon, Jun 11, 2012 at 12:13 PM, Stas Malyshev smalys...@sugarcrm.comwrote: Hi! Can be: $var = 'abc'; echo $var[-1]; This seems simple enough for a hard-coded -1, but... Would $var[-2] be strlen($var) - 2 and so on? The main question is what happens with foo[-4] or ['x'][-2]. The same thing that currently happens for: $f='foo'; echo $f[strlen($f)-4]; $a=['x']; $a[count($a)-2]; which appears to be notices: Notice: Uninitialized string offset: -1 in - on line 2 Notice: Undefined offset: -1 in - on line 3 And then one would expect some rather complex logic to compute -N for $var[-N] I don't see much of complex logic here, but $a[2] = 'a' would create a new array element if it does not exist, while $a[-2] can't. Not a big issue, but somewhat inconsistent I guess. Negative indices are currently valid for arrays. If anyone makes use of negative indices, this could break their script. Personally, I'd rather have Marc Easen's behavior.
Re: [PHP-DEV] Generators in PHP
Hi, it'd be really cool if the implementation would do the real context switch in c, which would be the true basis for spl_coroutine. The current implementation seems not to do that. Context switch in c would be comparable with threads. In fact coroutines would be a comensation for lacking threads functionality in php, as they are already implemented on most popular platforms. However, it would require some platform specific libs (fibers on windows, setjmp and longjmp on unix(like)). Coroutines in c are per se thread safe, so implementing them once would not hurt both ts and nts. Additionally, in some cases coroutines can even have advantages over the usual preemptive threads. Regards Anatoliy Am Mi, 6.06.2012, 04:42 schrieb Laruence: On Wed, Jun 6, 2012 at 10:27 AM, Laruence larue...@php.net wrote: On Wed, Jun 6, 2012 at 10:15 AM, Laruence larue...@php.net wrote: Hi Nikita: the most important part to me is how did you implemented `yield` keyword, is there a whole patch file I can look into? Nervermind, I will check the branch out later thanks After a quick look, I think the main idea should goes to two parts: 1. implement yield (Zend) 2. implement spl_generators but not generator class (Spl) then we can implement spl_coroutine later base on this. what do you think? thanks what will happen if you use a `yield` in a normal function? actually, I tried to implemented coroutine, but since I could not find a way to make zend_execute interruptable, then I didn't make it. so, I am really interesting of this tech-specific :) thanks On Wed, Jun 6, 2012 at 1:35 AM, Nikita Popov nikita@googlemail.com wrote: Hi internals! In the last few days I've created a proof of concept implementation for generators in PHP. It's not yet complete, but the basic functionality is there: https://github.com/nikic/php-src/tree/addGeneratorsSupport The implementation is outlined in the RFC-stub here: https://wiki.php.net/rfc/generators Before going any further I'd like to get some comments about what you think of adding generator support to PHP. If you don't know what generators are you should have a look at the Introduction section in the above RFC or in the Python documentation at http://wiki.python.org/moin/Generators. Nikita -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- Laruence Xinchen Hui http://www.laruence.com/ -- Laruence Xinchen Hui http://www.laruence.com/ -- Laruence Xinchen Hui http://www.laruence.com/ -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Support negative indexes for arrays and strings
On 11/06/12 20:28, Richard Lynch wrote: On Mon, June 11, 2012 2:13 pm, Stas Malyshev wrote: And then one would expect some rather complex logic to compute -N for $var[-N] I don't see much of complex logic here, but $a[2] = 'a' would create a new array element if it does not exist, while $a[-2] can't. Not a big issue, but somewhat inconsistent I guess. $n = some_incredibly_long_and_complex_computation(); //and, for real fun, sometimes it returns positive, and sometimes negative. $s = $var[$n]; If $n is a negative value, an E_NOTICE will be triggered. To not do if ($n 0){ //whatever } else{ //something entirely different } Prior to your string offset can be looked at being bad practice. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Support negative indexes for arrays and strings
On 11/06/12 20:13, Stas Malyshev wrote: Hi! Can be: $var = 'abc'; echo $var[-1]; This seems simple enough for a hard-coded -1, but... Would $var[-2] be strlen($var) - 2 and so on? The main question is what happens with foo[-4] or ['x'][-2]. And then one would expect some rather complex logic to compute -N for $var[-N] I don't see much of complex logic here, but $a[2] = 'a' would create a new array element if it does not exist, while $a[-2] can't. Not a big issue, but somewhat inconsistent I guess. Please note I'm not referring the negative indexes in arrays, just strings. As I see strings as being simpler to implement and produce a greater benefit compared to supporting negative indexes for arrays. As arrays are more complex structures and adding this behaviour would complicate things somewhat. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Generators in PHP
Can you really use setjmp and longjmp in that way safely? I thought it was only safe to longjmp back, not forward - you can use them to fake exception support but that's it because you'll smash the stack otherwise. Something like that... OK, I'm thinking of this: Similarly, C99 does not require that longjmp preserve the current stack frame. This means that jumping into a function which was exited via a call to longjmp is undefined.[6] However, most implementations of longjmp leave the stack frame intact, allowing setjmp and longjmp to be used to jump back-and-forth between two or more functions—a feature exploited for multitasking. It does not sound like something that can be done in portable C. On Mon, Jun 11, 2012 at 4:13 PM, Anatoliy Belsky a...@php.net wrote: Hi, it'd be really cool if the implementation would do the real context switch in c, which would be the true basis for spl_coroutine. The current implementation seems not to do that. Context switch in c would be comparable with threads. In fact coroutines would be a comensation for lacking threads functionality in php, as they are already implemented on most popular platforms. However, it would require some platform specific libs (fibers on windows, setjmp and longjmp on unix(like)). Coroutines in c are per se thread safe, so implementing them once would not hurt both ts and nts. Additionally, in some cases coroutines can even have advantages over the usual preemptive threads. Regards Anatoliy Am Mi, 6.06.2012, 04:42 schrieb Laruence: On Wed, Jun 6, 2012 at 10:27 AM, Laruence larue...@php.net wrote: On Wed, Jun 6, 2012 at 10:15 AM, Laruence larue...@php.net wrote: Hi Nikita: the most important part to me is how did you implemented `yield` keyword, is there a whole patch file I can look into? Nervermind, I will check the branch out later thanks After a quick look, I think the main idea should goes to two parts: 1. implement yield (Zend) 2. implement spl_generators but not generator class (Spl) then we can implement spl_coroutine later base on this. what do you think? thanks what will happen if you use a `yield` in a normal function? actually, I tried to implemented coroutine, but since I could not find a way to make zend_execute interruptable, then I didn't make it. so, I am really interesting of this tech-specific :) thanks On Wed, Jun 6, 2012 at 1:35 AM, Nikita Popov nikita@googlemail.com wrote: Hi internals! In the last few days I've created a proof of concept implementation for generators in PHP. It's not yet complete, but the basic functionality is there: https://github.com/nikic/php-src/tree/addGeneratorsSupport The implementation is outlined in the RFC-stub here: https://wiki.php.net/rfc/generators Before going any further I'd like to get some comments about what you think of adding generator support to PHP. If you don't know what generators are you should have a look at the Introduction section in the above RFC or in the Python documentation at http://wiki.python.org/moin/Generators. Nikita -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- Laruence Xinchen Hui http://www.laruence.com/ -- Laruence Xinchen Hui http://www.laruence.com/ -- Laruence Xinchen Hui http://www.laruence.com/ -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- Tom Boutell P'unk Avenue 215 755 1330 punkave.com window.punkave.com -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Generators in PHP
Hi, as i've mentioned, that's up to implementation, for more infos please pay attention to http://en.wikipedia.org/wiki/Coroutine#Implementations_for_C Well, the libtask mentioned there isn't thread safe (but could be made). Especially have earned some attention http://msdn.microsoft.com/en-us/library/windows/desktop/ms684847(v=vs.85).aspx#fiber_functions and https://github.com/stevedekorte/coroutine which is portable. Regards Anatoliy Am Mo, 11.06.2012, 23:12 schrieb Tom Boutell: Can you really use setjmp and longjmp in that way safely? I thought it was only safe to longjmp back, not forward - you can use them to fake exception support but that's it because you'll smash the stack otherwise. Something like that... OK, I'm thinking of this: Similarly, C99 does not require that longjmp preserve the current stack frame. This means that jumping into a function which was exited via a call to longjmp is undefined.[6] However, most implementations of longjmp leave the stack frame intact, allowing setjmp and longjmp to be used to jump back-and-forth between two or more functions?a feature exploited for multitasking. It does not sound like something that can be done in portable C. On Mon, Jun 11, 2012 at 4:13 PM, Anatoliy Belsky a...@php.net wrote: Hi, it'd be really cool if the implementation would do the real context switch in c, which would be the true basis for spl_coroutine. The current implementation seems not to do that. Context switch in c would be comparable with threads. In fact coroutines would be a comensation for lacking threads functionality in php, as they are already implemented on most popular platforms. However, it would require some platform specific libs (fibers on windows, setjmp and longjmp on unix(like)). Coroutines in c are per se thread safe, so implementing them once would not hurt both ts and nts. Additionally, in some cases coroutines can even have advantages over the usual preemptive threads. Regards Anatoliy Am Mi, 6.06.2012, 04:42 schrieb Laruence: On Wed, Jun 6, 2012 at 10:27 AM, Laruence larue...@php.net wrote: On Wed, Jun 6, 2012 at 10:15 AM, Laruence larue...@php.net wrote: Hi Nikita: the most important part to me is how did you implemented `yield` keyword, is there a whole patch file I can look into? Nervermind, I will check the branch out later thanks After a quick look, I think the main idea should goes to two parts: 1. implement yield (Zend) 2. implement spl_generators but not generator class (Spl) then we can implement spl_coroutine later base on this. what do you think? thanks what will happen if you use a `yield` in a normal function? actually, I tried to implemented coroutine, but since I could not find a way to make zend_execute interruptable, then I didn't make it. so, I am really interesting of this tech-specific :) thanks On Wed, Jun 6, 2012 at 1:35 AM, Nikita Popov nikita@googlemail.com wrote: Hi internals! In the last few days I've created a proof of concept implementation for generators in PHP. It's not yet complete, but the basic functionality is there: https://github.com/nikic/php-src/tree/addGeneratorsSupport The implementation is outlined in the RFC-stub here: https://wiki.php.net/rfc/generators Before going any further I'd like to get some comments about what you think of adding generator support to PHP. If you don't know what generators are you should have a look at the Introduction section in the above RFC or in the Python documentation at http://wiki.python.org/moin/Generators. Nikita -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- Laruence Xinchen Hui http://www.laruence.com/ -- Laruence Xinchen Hui http://www.laruence.com/ -- Laruence Xinchen Hui http://www.laruence.com/ -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- Tom Boutell P'unk Avenue 215 755 1330 punkave.com window.punkave.com -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Generators in PHP
I don't understand why you need to introduce two new keywords into the language - * and yield. Could you not solve it like follows? // Generator implements Iterable class AllEvenNumbers extends Generator { private $i; public __construct() { $this-i = 0; } function generate() { return ($this-i++) * 2; } } That way I think you can persist state (as part of the class instance) and implement rewind (per whichever way you do it - either caching the output, or serialising the class instance) without having to introduce yet more keywords. If this is instead a discussion to specifically implement yield, rather than simplifying the implementation of rewindable Iterators, it might be more useful to frame the discussion in that way. Jevon On Wed, Jun 6, 2012 at 5:35 AM, Nikita Popov nikita@googlemail.com wrote: Hi internals! In the last few days I've created a proof of concept implementation for generators in PHP. It's not yet complete, but the basic functionality is there: https://github.com/nikic/php-src/tree/addGeneratorsSupport The implementation is outlined in the RFC-stub here: https://wiki.php.net/rfc/generators Before going any further I'd like to get some comments about what you think of adding generator support to PHP. If you don't know what generators are you should have a look at the Introduction section in the above RFC or in the Python documentation at http://wiki.python.org/moin/Generators. Nikita -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php