taobert wrote:
Yes, I didn't really go further into the question. It was just a first draft
I didn't remember how works the Python's function Range(). In fact I hesitated over the decision to start with 0 or 1 (my Logo experience decided in favour of 1, probably wrongly).
I admit to having struggled to find a solution for the descending order and I lazily chose the easier one for me.
And what is more, I don't know anything about PHP.
Quote:
So which of the two implementations are we going to optimize?
Personaly, I would opt for the simpler and faster implementation or for a solution which satisfies Killrami's request in a better way (so, not my solution) ...

OK, with all this in mind, first we'll work out the design rules for our more-or-less optimized
Python-like implementation of Range() function:
1. Macros are generally handy but not here: PrintItem(item) would require an additional Print to go to a new line after the entire array contents are printed out while Array_Print(vector) would imply a call to an extra function which means additional time overhead for setting up Map() calls;
2. The implementation should provide for a special case with only 1 parameter given which is essentially the Range(end - start) parameter with a step = 1;
3. The generic parameter format is Range(start, end, (+/-)step) with start <= end for a positive step and start >= end for a negative step while any other parameter set (barring case 2 above) is illegal.
4. Array_Print is essentially an application-specific feature. Other tasks may require other ways to intercept/handle/respond to invalid parameter values or combinations so "If Count(params) = 0 Then" may need to be logically Or'ed accordingly with any other invalid conditions to produce the desired result for early error detection in the caller code.
Following these guidelines, I can suggest the following implementation:
Code:
#AppType Console
Array_Print(Range(10))
Array_Print(Range(3, 12))
Array_Print(Range(5, 15, 3))
Array_Print(Range(3, 12, 2.5))
Array_Print(Range(4, 2, -0.25))
Array_Print(Range(8, 1)) ' invalid params ignored => no console printout
Array_Print(Range(4, 2, 0.25)) ' invalid params ignored => no console printout
Array_Print(Range()) ' invalid params ignored => no console printout
Pause
Function Range(ParamArray params)
Dim seq[] = {Nothing} ' initialization allows for early detection of invalid params
' volatile Count used below, and Count = 3
' is naturally expected in For/Next that follows
If Count(params) = 0 /* Or any_other_invalid_case */ Then
Return seq
ElIf Count = 1 Then
params[1] = params[0] - 1: params[0] = 0: params[2] = 1
ElIf Count = 2 Then
params[2] = 1
End If
For Dim i = 0 To %((params[1] - params[0]) / params[2])
seq[i] = params[0] + i * params[2]
Next
Return seq
End Function
Sub Array_Print(vector)
ForEach Dim o In vector
If o Is Nothing Then Exit Sub ' early detection working else empty CRLF's printed
Printf("%s ", $o)
Next
Print
End Sub
Does this implementation still produce a "patched-up job" feeling?
Mike
