 # PRange behavior

There’s something that bothers me about the PRange function (and PTri). There are several cases where the function returns an empty pattern.
e.g. :

print(PRange(1,5,1)) -> P[1, 2, 3, 4]
print(PRange(1,5,-1)) -> P[]
print(PRange(5,1,1)) -> P[]
print(PRange(5,1,-1)) -> P[5, 4, 3, 2]
print(PRange(5,5)) -> P[]

I know it’s the way range() in python act, but I was wondering if we could modify it to always return something.
I try this, but don’t know if it’s a good way.

def PRange(start, stop=1, step=1):
“”" Returns a Pattern equivalent to `Pattern(range(start, stop, step))` “”"
if start == stop:
return Pattern(start)
if (start > stop and step > 0) or (start < stop and step < 0):
step = step*-1
return Pattern(list(range(*[val for val in (start, stop, step) if val is not None])))

Hmm, good point. Having a Pattern always containing the start is probably a good idea. Not sure about always inverting the step size, but it makes sense I guess. I’ve updated the function a little bit so it looks like this:

``````def PRange(start, stop=None, step=1):
if stop is None:

start, stop = 0, start

if start == stop:

return Pattern(start)

if (start > stop and step > 0) or (start < stop and step < 0):

step = step*-1

return Pattern(list(range(start, stop, step)))
``````

Otherwise supplying only one value e.g. `PRange(4)` would give you `P[4, 3, 2]`, where it should return `P[0, 1, 2, 3]`.