Phabstractic: Range Data Type

I’m proud to announce that a new abstract data type has been added to asherwunk/phabstractic, the Range.

The Range Class

When you use an if statement you often times poll whether a given value is equal to, or lesser/greater than a given value. When I published my PHP Restrictions Predicate object (Primus2/Falcraft) I was interested in being able to abstract away the nature of an if clause. I thought, what if the idea of something coming out as true or false was encapsulated in an object as a ‘first class citizen’? This was interesting to me, because I could then set up if-clauses that checked for certain parameters (filters as I called them in my post) and then use them as swappable and configurable components in an algorithm’s logic.

Think about it in terms of a sorting function. A sorting function takes two items given to it by an internal sorting function and returns which one is first and which one is second. You can plug a sorting function into a sorting algorithm and produce custom list-processing functionality purely based on that sorting function. I transformed the notion of a ‘sorting function’ into an abstract one of a ‘filter’. Plug any filter into anywhere that an algorithm may have a variable if-clause comparing pre-determine data types. Heck, you could build a multi-data type predicate function that you could plug into a generic algorithm which would then allow you to add additional data-types without having to rewrite the algorithm. If-clause polymorphism baby!

But I digress. There was a particular set of if-clause functionality that I hoped to reproduce in a more succinct and less abstract sense, and that was inequalities. In mathematics you get equality statements with less-than, greater-than, equal-to, greater-than-or-equal-to, etc.

What if you could define a range of values, say, the domain of a function perhaps, and then test if a value was in that range? This could prove useful for pre-emptive debugging of function bounds.

Thus, I bring to you the Range class (also on github):

Yes, it does use the dreaded eval function, so be very cautious what kind of information you send it. However, I thought it was more elegant to use the built in comparison functions, and I think it’s pretty secure given that the only thing it references outside of itself is min and max.

It was either this or write all the comparison logic on top of comparison logic, which I thought was kind of ugly.

Now, you can test if a number is in a given range by simply calling $myRange->isInRange($number, array('maximum'=>true,)) [This example includes the maximum (less than and equal to maximum)]


It is useful to be able to differentiate in an object oriented sense if a value is null. Sometimes you want to be able to return o, false, or null, but still need a way of indicating that there was an error. A custom null class/object can fulfill that role for you.

Predicate objects are an interesting concept, which could have many uses in functional programming, in mathematical polymorphism, in input validation, etc. Ranges are a specific version of a predicate object that tests if any given value is within the maximum and minimum range defined in the object.

This is part of the Phabstractic Library.

If you appreciate my programming please help support me through my Patreon.

photo credit: New Product – Fibonacci Clock Kit via photopin (license)


I'm just a wunk, trying to enjoy life. I am a cofounder of http// and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: