Sometimes, especially when dealing with offsets in
UIScrollViews or other, we need to clamp a certain undefined range of numbers into another defined array of numbers.
A good example when needing this would be an automatic show/hide of a header (
offset) depending on the offset of an unknown size
You could go about solving this in an imperative way, for example:
This looks a bit clunky, so, you might feel inclined to make this a bit more elegant and shorter, like:
However, why simply not leverage Swift and functional paradigm to make this reusable, elegant and easy to read?
Now, using the method above would simply end up as: