## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
• Oct 20 2019 22:59
@dockimbel banned @SmackMacDougal
• Dec 03 2017 05:53
@PeterWAWood banned @matrixbot
• Sep 28 2016 12:19
@PeterWAWood banned @TimeSeriesLord
• Aug 13 2016 03:23
@PeterWAWood banned @Vexercizer
Boleslav Březovský
@rebolek
There is actually an easier way, just use sort/compare with a comparatur function:
 sort/compare block func [this that] [all [this/1 > that/1 this/2 > that/2]]
Of course you would need to improve the comaparator function, this is just an example how to do it.
Boleslav Březovský
@rebolek
>> block: collect [loop 15 [keep/only reduce [random 9 random 9 random 9]]]
== [[3 5 1] [7 8 5] [4 7 3] [5 7 4] [2 3 3] [7 6 4] [6 4 7] [3 6 5] [9 7 1] [3 7 1] [6 4 9] [2 4 3] [6 9 5] [7 2 8] [2 4 9]]
>> sort/compare block func [this that] [any [this/1 < that/1 this/2 < that/2]]
== [[2 3 3] [2 4 9] [2 4 3] [3 5 1] [3 6 5] [3 7 1] [4 7 3] [5 7 4] [6 4 7] [6 4 9] [6 9 5] [7 2 8] [7 6 4] [7 8 5] [9 7 1]]
hiiamboris
@hiiamboris
In the comparator: use sort!
Galen Ivanov
@GalenIvanov
Will block in /compare => Comparator offset, block (TBD) or function. serve this function - to specify the priority of the fields when sorting?
GiuseppeChillemi
@GiuseppeChillemi
I have looked at sort.r source code and it is really cryptic: no comments and one or 2 letters argument names. I was trying to understand of the comparer function works. @rebolek: you have built a function with two arguments but what is one argument and what is the other? How the whole sorting process works?
@hiiamboris Why and how should be used sort in the comparator?
Also, I see this refinement /all => Compare all fields (used with /skip) which seems near to what is needed. In place of All fields it could compare just few.
Galen Ivanov
@GalenIvanov
@GiuseppeChillemi the anonymous function takes two arguments - each two values that sort compares in order to place at their respective places in the sorted list.
hiiamboris
@hiiamboris

@hiiamboris Why and how should be used sort in the comparator?

I'll leave that for you as an exercise ☻

Tip: sort does not error out if things are not "comparable"
GiuseppeChillemi
@GiuseppeChillemi
@GalenIvanov Thanks, so the whole process consists into scanning the first and next element and running the comparator function. I imagine it does it into multiple steps and creating intermediary blocks until it reaches the "SORTED" condition.
@hiiamboris I need to know more about the sorting process as a whole, otherwise, you know myself, I will end-up considering every possible scenario, even the most complicated.
Galen Ivanov
@GalenIvanov
@GiuseppeChillemi Yes, in principle. I haven't looked at the source and don't know the details - the sorting might be in place.
hiiamboris
@hiiamboris
Of course it's in-place.
Galen Ivanov
@GalenIvanov
:+1:
GiuseppeChillemi
@GiuseppeChillemi
What you mean as "in place"? On the source block with no temporary intermediate blocks?
Gregg Irwin
@greggirwin
@rebolek, of course! Do you know if using a comparator honors /stable. I remember issues under R2 at some point, but confess I haven't tried it in Red.
Boleslav Březovský
@rebolek

@greggirwin I’m not sure. I know two things:

1) this comparison is not stable, sometimes it needs three passes to produce the right result, and doing the fourth pass produces the wrong result again (but sometimes it’s just two so it’s not predictable).

2) with /stable it’s even more unstable as I got hard crash that I wasn’t able to reproduce later, unfortunately, otherwise I would fill a bug.

Gregg Irwin
@greggirwin
Thanks. Under R2 I believe it did finally honor results of [-1 0 1] for stable sorting.
hiiamboris
@hiiamboris
Gregg Irwin
@greggirwin
Almost a year to the day! I know @qtxie is busy, but sort is important.
hiiamboris
@hiiamboris
Issues got neglected after the decision to not do stable releases :)
(or may be because I've found way too many..)
hiiamboris
@hiiamboris
@greggirwin By the way, in your FOR design: do you allow user to modify the index and then continue from that modified index, or do you replace the index no matter what user does with it?
Something to consider.
Right now only forall allows to alter it's offset at iteration time.
Gregg Irwin
@greggirwin
It's TBD. Some cases do and others don't, but not by design...yet. This comes from delegating some loops down to loop, repeat, or foreach internally, but if there's no internal func to do it, the final logic goes through Ladislav's old cfor (for how C does it), which gives you total control.
hiiamboris
@hiiamboris
I'm in favor of total control too. The only cases where I modify the index in repeat are when I'm using it with an offset, in place of missing for. (so if we had for, I'd vote for repeat to support reading index back between iterations)
It becomes a bit trickier though when iterating over a range of pairs..
Ideally, let user be dumb and add 1x0 even if it goes over the current line, then skip the extra number of pixels on the next line.
Gregg Irwin
@greggirwin
The design problem is consistency. We want to leverage natives, but having some calls allow it, and others not, is bad for users. If everything goes through cfor, every loop is slower.
I'd like for most loops to be safe, as long as there is one way you can be in control.
hiiamboris
@hiiamboris
Meaning?
Gregg Irwin
@greggirwin
Basic iteration (count or series end-test) can never spinlock, as long as you have something like while that puts you in control of the end-test.
hiiamboris
@hiiamboris
Not sure how this relates to for :)
Gregg Irwin
@greggirwin

for [i 1 to 10 step 2][...] doesn't let you change i and potentially spinlock.

while or for [[i: 1] [i <= 10] [i: i + 2]] [...] gives you all the rope you need to shoot yourself in the foot.

hiiamboris
@hiiamboris
So you'd rather not let to change i because of spinlock danger?
Clearly, for will have to be consistent about it.
Gregg Irwin
@greggirwin
Currently the first example "compiles down" to the second example, except when delegating to natives.
I'd like to help the user, and their users.
hiiamboris
@hiiamboris
Help can take different forms :)
Gregg Irwin
@greggirwin
hiiamboris
@hiiamboris
Haha OK :)
Gregg Irwin
@greggirwin
But we agree that consistency is a must.
hiiamboris
@hiiamboris
I asked because I thought I'm going to allow that in foreach. Because that's a powerful feature of forall to skip 1 or more items, and even zero. I'm using it a lot, and I would hate to throw away foreach and fall back to forall when I need that.
But these 2 are positional iterators, not numeric. So consistency with for is less important.
Gregg Irwin
@greggirwin
It is powerful, like goto is powerful. Constraints are a pain sometimes but they also provide a lot of value. In this case, the ability to reason about iteration logic without having to read every line of the body.
hiiamboris
@hiiamboris
Good argument.
Gregg Irwin
@greggirwin
If only there was one right answer. :^\
hiiamboris
@hiiamboris
Indeed..