# [RFC] Iteration tools

- Thread: https://externals.io/message/41624
- Original archive: https://news-web.php.net/php.internals/41624
- Started by Ionut Gabriel Stan on 2008-11-03T15:07:14+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #41624 — Ionut Gabriel Stan — 2008-11-03T15:07:14+00:00

URL: https://externals.io/message/41624#41624

Hello everybody,

As a result of the advice given in the thread "array_key_exists BC 
break", I have written an
RFC concerning some higher order functions that, I believe, could a 
helpful addition to the language.

Please note that I'm a userland developer and have no clue about PHP 
internals so please
forgive any stupidities I might have said in the RFC and make the 
appropriate corrections.
That also means that, if positive feedback is received, this RFC needs a 
developer with PHP
internals knowledge to implement it.

The RFC can be found here:  http://wiki.php.net/rfc/iteration-tools

Hoping that I've made sense in what I wrote there, please feel free to 
comment as you like.

Regards,
Ionut

---

## #41625 — Marcus Boerger — 2008-11-03T15:41:29+00:00

URL: https://externals.io/message/41624#41625 · In reply to: #41624

Hello Ionut,

  some remarks about your proposal:

1) You can turn any array into a Traversable using
(Recursive)ArrayIterator. Though this solution is still slow. The issue is
that the c-level code needs to verify the current pointer every single time
it uses it and that means traversing the array from the beginning. That
said we might want to:
a) Optimize that for cases where we don't have to. Basically when nothing
else is pointing to the array (refcount==1) we know when the array can be
changed and do not have to verify it always.
b) provide a lower level array to traversable mechanism that operates on
the current pointer provided by the array itself. That way having no need
to verify that pointer at all.

2) Ther are `iterator_apply()`, `iterator_to_array()`, `iterator_count()`
The first can be used for a lot of the things you were looking for,
especially when used together with lambdas. Also not that you can use it to
pass the iterator itself to the function being called easily.

Still a few of the things you mention can be implemented faster in the same
way `iterator_apply()` was done. In fact the code can be based on the lovel
c-implementation provided.

marcus

Monday, November 3, 2008, 4:07:14 PM, you wrote:

> Hello everybody,

> As a result of the advice given in the thread "array_key_exists BC 
> break", I have written an
> RFC concerning some higher order functions that, I believe, could a 
> helpful addition to the language.

> Please note that I'm a userland developer and have no clue about PHP 
> internals so please
> forgive any stupidities I might have said in the RFC and make the 
> appropriate corrections.
> That also means that, if positive feedback is received, this RFC needs a 
> developer with PHP
> internals knowledge to implement it.

> The RFC can be found here:  http://wiki.php.net/rfc/iteration-tools

> Hoping that I've made sense in what I wrote there, please feel free to 
> comment as you like.

> Regards,
> Ionut





Best regards,
 Marcus

---

## #41633 — Ionut Gabriel Stan — 2008-11-04T09:11:08+00:00

URL: https://externals.io/message/41624#41633 · In reply to: #41625

> Hello Ionut,
>
>    some remarks about your proposal:
>
> 1) You can turn any array into a Traversable using
> (Recursive)ArrayIterator. Though this solution is still slow. The issue is
> that the c-level code needs to verify the current pointer every single time
> it uses it and that means traversing the array from the beginning. That
> said we might want to:
> a) Optimize that for cases where we don't have to. Basically when nothing
> else is pointing to the array (refcount==1) we know when the array can be
> changed and do not have to verify it always.
> b) provide a lower level array to traversable mechanism that operates on
> the current pointer provided by the array itself. That way having no need
> to verify that pointer at all.
>
> 2) Ther are `iterator_apply()`, `iterator_to_array()`, `iterator_count()`
> The first can be used for a lot of the things you were looking for,
> especially when used together with lambdas. Also not that you can use it to
> pass the iterator itself to the function being called easily.
>
> Still a few of the things you mention can be implemented faster in the same
> way `iterator_apply()` was done. In fact the code can be based on the lovel
> c-implementation provided.
>
> marcus
>
> Monday, November 3, 2008, 4:07:14 PM, you wrote:
>
>    
> > […]
>
>    
> > […]
>
>    
> > […]
>
>    
> > […]
>
>    
> > […]
>
>    
> > […]
>
>
>
>
>
> Best regards,
>   Marcus
>
>
>    
Hi Marcus,

Thanks for your answer.

I had no idea there is an `iterator_apply()` function, I did a little bit 
of search after you mentioned it
and found it nowhere in the docs but apparently it is present in my 
5.2.6 installation. `iterator_to_array()`
and interator_count() are very useful functions which I had knowledge of 
but forgot about them in my wiki.

I recognize that my proposal can easily be implemented in userland code 
with the classes and functions
that we already have, as you mentioned already. The only real advantage 
is the speed given by the C level
code and a common, standard API for array and Traversables. This way 
 `array_map()`, `array_reduce()` and
the like could be deprecated. I always have to look in the docs or pay a 
little attention to my IDE to see
what param goes where.

Anyway, I see there's nobody else that would like this but it's ok, at 
least now we have namespaces so that
I don't have to come up with ugly names like my_map() for these kind of 
helper functions.


Cheers,
Ionut

---

## #41634 — troels knak-nielsen — 2008-11-04T09:39:51+00:00

URL: https://externals.io/message/41624#41634 · In reply to: #41633

On Tue, Nov 4, 2008 at 10:11 AM, Ionut Gabriel Stan
<ionut.g.stan@gmail.com> wrote:
> Anyway, I see there's nobody else that would like this but it's ok, at least
> now we have namespaces so that
> I don't have to come up with ugly names like my_map() for these kind of
> helper functions.

I like the proposal. With the advent of first-class functions,
functional-style iteration is becoming possible. An abstraction that
hides away the difference between arrays and traversables would help
further in this direction. And this is something that is much better
implemented in the language, than in userland, to ensure
interoperability.

--
troels

---

## #41635 — david.zuelke@bitextender.com — 2008-11-04T11:46:37+00:00

URL: https://externals.io/message/41624#41635 · In reply to: #41625

Am 03.11.2008 um 16:41 schrieb Marcus Boerger:

> 2) Ther are `iterator_apply()`

owww that sounds like it really needs docs :

---

## #41636 — Kalle Sommer Nielsen — 2008-11-04T12:21:42+00:00

URL: https://externals.io/message/41624#41636 · In reply to: #41635

2008/11/4 David Zülke <david.zuelke@bitextender.com>:
> Am 03.11.2008 um 16:41 schrieb Marcus Boerger:
>
> > […]
>
> owww that sounds like it really needs docs :>

http://bugs.php.net/bug.php?id=30185

Long time standing bug :)



-- 
Kalle Sommer Nielsen
