


o1 = new stzList([ :one, :two, :three, :four, :five ])
o1.Perform('@item = Q(@item).Uppercased()')
? @@(o1.Content()) #--> [ "ONE", "TWO", "THREE", "FOUR", "FIVE" ]
I was faced with the need of transforming a list of sections of the form, say [ [2,5], [7,9 ] ] to a contiguous list of numbers like this [ 2, 3, 4, 5, 7, 8, 9 ]...
The good reason of doing this is that I already have a derivative function of Perform() called PerfomOn(panPositions) that we can feed with a list of items by their positions, and let it perform any action we want on them, like this:
o1 = new stzList([ :one, :two, :three, :four, :five ])
o1.PerformOn([2, 4], '@item = upper(@item)')
? @@(o1.Content()) #--> [ "TWO", "FOUR", "three", "four", "five" ]
So, the question I had: should I do it specifically for this case using a plain-Ring solution like this:
anPositions = []
for aSection in paSections
for i = aSection[1] to aSection[2]
if find(anPositions, i) = 0
anPositions + i
next
next
next
sort(anPositions)
Or, generalise this feature in a new function I add to stzList class, called ExtedIfPairOfNumbers(), that we can use like this:
? StzListQ([2,5]).ExtendedIfPairOfNumbers() #--> [ 2, 3, 4, 5 ]
By evaluating the situation. I see that the same code I would have written specifically in my current context can be used, with additional minimal cheks, in a new general function, in writing a new general function, so I can express myself naturally like this:
# Getting all the positions from the provided sections
# Example: [ [2,5], [7,9 ] --> [ 2, 3, 4, 5, 7, 8, 9 ]
# Performing the ExtendIfPairOfNumbers() action on all items of paSections list
Q(paSections).PerformW('{
@item = Q(@item).ExtendedIfPairOfNumbers()
}')
PS: PerformW() is a derivation function of Perform() that performs an action upon a given condition (W for Where).
Advantages are obvious:
1. The code is lean and readable.
2. A new useful yet reusable feature is added.
3. The library grows based on practical needs.
In reality, all the functions you see in the library (excluding of course the alternative names which serve for other needs I discussed previously) were added this way!
All the best,
Mansour
--
---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ring-lang/f2154451-5f8c-402d-acf0-159cac054fc9n%40googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/ring-lang/5252e293-3fac-46e4-b7ac-4f754252c735n%40googlegroups.com.