bartfeenstra / fu-php

🛠 Functional programming tools for PHP 🐘
https://twitter.com/BartFeenstra
MIT License
2 stars 1 forks source link

Replace Iterator::min() and ::max() with reusable fold callbacks #61

Open bartfeenstra opened 7 years ago

bartfeenstra commented 7 years ago

Allow min() and max() to take a sort callback, so they can compute the lowest and highest values themselves.

bartfeenstra commented 7 years ago

Or not, because if you want to sort first, use sort*() first.

bartfeenstra commented 7 years ago

min() and max() can either be expressed as folds (current situation, but not reusable) or sorts, followed by ::first() or ::last(). Maybe it's worth removing these two methods and using one of these other two approaches instead.

bartfeenstra commented 7 years ago

min() and max() can either be expressed as folds (current situation, but not reusable) or sorts (which falls back to asort() and is already more powerful than ::min() and ::max() now), followed by ::first() or ::last(). Maybe it's worth removing these two methods and using one of these other two approaches instead.

bartfeenstra commented 7 years ago

The advantage of folds is the lower memory consumption since no new (sorted) iterator has to be generated under the hood. If we go this approach, and even if we keep the current ::min() and ::max() methods, we will want to allow them to take custom sorts. We can use those without creating new data sets, because they'll simply tell us which item is greater than the other, or if they're the time. Any values not matching the condition are discarded, and a single end result is produced.