Let me start by saying I loved your interview on the Array Cast!

One example of /: I coincidentally noticed today is the following definition of 
lexicographic order for strings:

    lex_le =: 0 1 -: /:@(,&<)

I was about to program it in a much more complicated way when I suddenly 
remembered J aready knows how to sort everything if you just box it! Note that 
its correctness in the case of equality depends on /: being a stable sort. 
(Note also that it defines a total order on all nouns, of any rank and data 
type, not just strings; J's "secret" total order on all nouns!)

Another neat thing about grade up is that ({ /:) is a special combination: x ({ 
/:) y gives you the x-th smallest item of y without fully sorting y. The 
description in NuVoc stops short of saying that it uses the Quickselect [1] 
algorithm with a random pivot, but it sure makes it sound like it does. It 
seems to be limited to y that are 1-dimensional with numeric entries, sadly (or 
maybe I'm misunderstanding the phrase "y is an integer or floating-point list").

A third, somewhat tongue-in-cheek answer is that you can regard the dyadic sort 
up case, namely x /: y, as just shorthand for (/: y) { x, so really any example 
of grade up should count as a examples of using grade up (/:) and indexing ({).

A fourth, perhaps less essential example, that I've come across is while using 
inverted tables: I sometimes need to sort several sets of columns by one of the 
columns. Say I have one table of student grades with columns for 
student_number, name, homework1, homework2, exam1 and exam2 (each column a 
separate J variable, which I find is the most convenient way of working with 
tabular data) and I want tables in order of student_number (because that's the 
order the system where I enter the grades wants) of homeworks and exams 
separately. I could of course do:

    homework =: (name ,. <"0 homework1,. homework2) /: student_number
    exam =: (name ,. <"0 exam1,. exam2) /: student_number

which sorts student_number twice but I sometimes just keep a few orders around, 
to look at data various ways:

    by_student_number =: /: student_number
    by_exam_total =: \: exam1 + exam2
    ...
    homework =: by_student_number { name ,. <"0 homework1,. homework2
    exam =: by_student_number { name ,. <"0 exam1,. exam2

-- 
Omar
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to