On 01-09-2020 01:56, tyson andre wrote: > Hi internals, > > I've created an RFC for https://wiki.php.net/rfc/any_all_on_iterable
Hi Andre, I like the RFC, it is small and the added value is clear. The RFC mentions the possibility of adding a first() method in the future. I think it would be great to generalize this to the very thing that excites me most about this RFC: It could be the starting point for ultimately arriving at a better set of tools for working with iterables. There is some discussion about the need for functions that work on keys in stead of values or on both keys and values. It was suggested to introduce option flags. It was also suggested to change the function names to allow introducing multiple variants in the future: all_values() all_keys() However, as Larry already hinted at, we may be able to do better. If we introduce a keys() function that generates the keys from a given iterable then we can use any value oriented function to work with the keys in stead. There is also the case where both the keys and values are relevant. It may be interesting to look at how Python handles this (I'm sorry, I just happen to know that language well). Python has dictionaries. Dictionaries have an items() method. This method is one of the most commonly used methods in Python. It is a generator that yields key/value pairs, one pair for each item in the dictionary. Perhaps we could do the same thing by introducing an items() function that takes an iterable and yields one array for each item in the iterable. The arrays contain the key/value pairs. Quoting the example Tyson gave: any($itemMap, fn($enabled, $itemId) => $enabled && meetsAdditionalCondition($itemId), ARRAY_FILTER_USE_BOTH) Using items(), the above can also be written as: any(items($itemMap), fn($item) => $item[0] && meetsAdditionalCondition($item[1])) This way we can do with just one any() variant and without option flags. It makes the example slightly less descriptive though, trading variable names for array indices. In Python I would probably use a comprehension here. Maybe the following will be possible in PHP at some point: any(foreach $itemMap as $enabled => $itemId yield $enabled && meetsAdditionalCondition($itemId)) Regards, Dik Takken -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php