J script file profile.js 
Author Message
 J script file profile.js

J executes script file profile.js during startup.
So it is commonly used to define frequently used verbs, etc.

I consider it would be useful to share some of these, rather than everyone
reinventing wheels. Has already been done before? It seems an obvious thing
to do.

I will start with following:




monad=. 'x. :''''':1          NB. e.g. cube=. 'y. ^3' monad
monadf=. '(read x.) monad':1  NB. e.g. m=. 'm.j' monadf
diad=. ''''':x.':1            NB. e.g. dist=. 'root (x.^2)+y.^2' diad
diadf=. '(read x.) diad':1    NB. e.g. d=. 'd.j' diadf

tr=. ('x.=.(2,<.-:#,x.)$,x.'; '((({.x.),~.,y.)i.y.){(({:x.),~.,y.)') diad

floor=. min=. <.
ceiling=. max=. >.
dec=. <:
inc=. >:
or=. +.
and=. *.
double=. +:
square=. *:
not=. less=. -.
halve=. -:
matdiv=. %.
root=. %:
log=. ^.
power=. ^:
reverse=. rotate=. |.
transpose=. |:
head=. take=. {.
tail=. {:
behead=. drop=. }.
curtail=. }:

sin=. 1&o.
cos=. 2&o.
tan=. 3&o.
sinh=. 5&o.
cosh=. 6&o.
tanh=. 7&o.
asin=. _1&o.
acos=. _2&o.
atan=. '_3 o. y.' : '12 o. x. j. y.' "_
Re=. 9&o.                NB. real part
Im=. 11&o.               NB. imaginary part

sum=. +/                 NB. sigma
am=. sum % #             NB. arithmetic mean
dev=. - am               NB. deviation from arithmetic mean



Many of these are just obvious aliases. Following is further description for
some of the others.

'read' reads an ascii file into a character matrix. It is assumed that lines
are delimited by a single character & this is the final character in the file.

'write' writes any (character or numeric) array to an ascii file. Lines are
delimited by newline characters.

Note that 'read' & 'write' follow unix conventions & will not work for DOS.

'monad' is an adverb which defines a monad from a explicit definition string
'monadf' is an adverb which defines a monad from a explicit definition file
'diad' & 'diadf' similarly define diads
Note that 'monadf' & 'diadf' use 'read', so also assume unix file conventions.

'tr' is a character mapping (translation) verb inspired by unix 'tr' utility
   'aAbB' tr 'abcbb'
bbcbb
   '-_' tr read 'file.dat' NB. converts each '-' to '_'

Diadic atan is inspired by fortran atan2.
Example:
   180p_1 * _1 atan 1
135

--
Harvey Davies,
CSIRO Division of Atmospheric Research,    Voice: +61 3 586 7574
Private Bag No. 1, Mordialloc,               Fax: +61 3 586 7600



Thu, 28 Mar 1996 17:08:14 GMT  
 J script file profile.js

But, that doesn't work quite right for higher ranked arrays, such as i. 2 2 2

I'd have done something more like:

which makes write rather trivial:

Of course, your read is somewhat problematical for higher ranked
arrays, because it's ambiguous..

Also, I'd be tempted to define monad as
monad=. :11

Or, since yours is more flexible (just produces a slower result), you
could write
monad=. :''

Similarly, you could diad as
dyad=. :11              NB. won't work with multi-line definitions, such as tr
or
dyad=. '':

Finally, I'd prefer to define tr so that instead of taking an argument
of the form 'beforeafter' it took an argument of the form

Quote:
>'before';'after'  This way, it would extend to higher ranked arrays,

etc.

tr=. '((({.x.),~."(${.x.)y.)i.y.) { (({:x.),~."(${.x.)y.)'

Or, to enhance tr so that any elements of the before argument without
'after' elements will be removed from the result, make the left
argument to tr be 'before';'after'.

.x.':11

That's ugly and indecipherable, but it has the advantage of not
cluttering up the workspace with junk definitions, and being fast
besides.  [Consider it object code.]

For the curious,
   ('beforeh';'after') tr 'this is a test'

computes the same result as:
   simpletr=. '((>{.x.) i. y.)   {  >{:x.' dyad
   ('beforthis ae';'afterthis ae') simpletr  'tis is a tfst'

Finally, I wouldn't bother putting simple aliases in a profile.js --
   sin=. 1&o.
is probably worthwhile (if you're using transcendental functions), but
  tail=. {:
does nothing but give more to memorize, and more to type.  Which is to
say,

(((tally unbox last x.)first under unbox x.)concatenate under unbox box nub y.)((unbox atop first atop left indices right)from unbox atop last atop left)y. without (tally unbox last x.)(behead without first)unbox first x.

is no clearer than

since it's the lack of abstraction that's the problem, not the shape
of the symbols.

Raul D. Miller



Thu, 28 Mar 1996 21:28:36 GMT  
 J script file profile.js
|> J executes script file profile.js during startup.
|> So it is commonly used to define frequently used verbs, etc.
|> I consider it would be useful to share some of these, rather than everyone
|> reinventing wheels. Has already been done before? It seems an obvious thing
|> to do.
|> I will start with following:
...

We've had some before. I tend not to like tons of word definitions. I
prefer 1 o. to sin, since sin doesn't exist in the basic language and
could be redefined. On the other hand, I do some things:

- modify the source so it reads my ~sam/J/profile.js, rather than the
local one in the current ws. I much prefer this behaviour.

- make a library function:

script=. 0!:2 &<
sscript=. 0!:3 &<

NB. 'log.fil' session ''
session=. 0!:2&<

libs=. '/home/kalessin/sam/J/basic.js'; '/home/kalessin/sam/J/draw.js'
libs=. ,. libs, <'/home/kalessin/sam/J/rand.js'
libs=. ,. libs, <'/home/kalessin/sam/J/text.js'
libs=. ,. libs, <'/home/kalessin/sam/J/util.js'
libs=. ,. libs, <'/home/kalessin/sam/J/readn.js'

lib=. 'sscript ,>y.{libs' :11 f.
libs=. (<"0 ,. i. #libs),"1 libs

Then typing "libs" gives me a list of my current libraries, with
numbers, whereas "lib 1" loads library 1 etc. Yes, I did grow up with
the old APL library system.

- while we're on the subject of trigonometry, I do find an atan
function useful as it isn't just _3. This is basicly transliterated
from APL:

catb =. 'y.' : ' 1 |. y. ; x. '
com=. ' '''''''',y.,'''''''' ' : ' 1 |. (com y.); x. '

a=. com 'r=. a1 atan2 a2'
a=. a com 'Fortran atan2 function'
a=. a com '  atan(a1/a2)'
a=. a com ' '
a=. a catb 'p=. _3 o. x. % y.+ i=. 0 = y.'
a=. a catb 'p=. (p * -.i) + i*(*x.)*o. %2'
a=. a com  '  regions (ccw starting from top right) are 1 2 4 3'
a=. a catb 'add=. o. 0 1 0 _1'
a=. a catb 'p=. p + ( (y.<0) + 2*x.<0 ) {add'
atan2=. '' : a

--
Sam Sirlin



Sat, 30 Mar 1996 02:38:31 GMT  
 J script file profile.js

Quote:
>Finally, I wouldn't bother putting simple aliases in a profile.js --
>   sin=. 1&o.
>is probably worthwhile (if you're using transcendental functions), but
>  tail=. {:
>does nothing but give more to memorize, and more to type.  Which is to
>say,
>(((tally unbox last x.)first under unbox x.)concatenate under unbox box nub y.)((unbox atop first atop left indices right)from unbox atop last atop left)y. without (tally unbox last x.)(behead without first)unbox first x.
>is no clearer than

>since it's the lack of abstraction that's the problem, not the shape
>of the symbols.

I think it is important to include 'tail' in profile.js.  Why?  Many
reasons:
1) I can't remember the difference between }. }: {. {:, but I can remember
tail.  If I want a quick function I just type tail when I want tail
and don't search for the correct symbol.  In a final version of a function
I would not include the same expanded name repeatedly because of the
length, but it is more readable to those who know English better than
glyphish.

2)  It makes clear whether {: is meant to be tail or drop (it is drop,
isn't it? I'm really not sure and don't have a reference handy.)

3)  People who don't know J can read it.  For example, APLers often don't
know J, but know what signum is even if they don't know what * is
monadically.

4)  It's easier on my parenthesis matchers who get confused when I use
a lot of { and [ things without ] and }.  (there... balanced and
satisfied).

5)  Some verbs are easier to read in long form, others not.  I prefer not
to expand conjunctions for example.

Learning J would have been easier for me if it came with a profile
redefining the primitives and popular idioms.  Maybe future versions
will include such.  (maybe a primitives.js and idioms.js instead to
keep the profile under control, but so people can use readable stuff when
they feel a need.
primitives.js:

tail =. '}:' : ''
...
oh dang.. got it wrong again.

Alan Baljeu

--
-------------------------------------------------------------------------------
        Alan Baljeu    
(signature still under construction)                    HAND! :)
-------------------------------------------------------------------------------



Sat, 30 Mar 1996 03:38:36 GMT  
 J script file profile.js
Alan Baljeu:
   I think it is important to include 'tail' in profile.js.  Why?  Many
   reasons:

Well, first off, you're entitled to think what you wish.  However,
viewing your statement as communication generally applicable to the
programming community, I feel I must differ.

   1) I can't remember the difference between }. }: {. {:, but I can remember
   tail.  If I want a quick function I just type tail when I want tail
   and don't search for the correct symbol.  In a final version of a function
   I would not include the same expanded name repeatedly because of the
   length, but it is more readable to those who know English better than
   glyphish.

First graders master more complex feats of memorization.  However,
most users of J are long past first grade and perhaps are reluctant to
exercise those skills.  Here's how I remember the difference between
{. {: }: and }.

 The { verbs are takes.  The . verb takes off the front, and the : verb
 takes off the end.  The } verbs are drops.  The . verb drops off the
 front, and the : verb drops off the end.  For a simple two item list,
 the result of {: has the same appearance as }.  But }. is everything
 but the first item, and {: is the last item -- and these have
 different shapes.  I think of a tail as something that has length, so
 I don't usually associate tail with {: unless I first remember that }.
 is behead.  So I can remember that the tail doesn't have length,
 because it's different from behead.

    {. 1 2
 1
    {: 1 2
 2
    }: 1 2
 1
    }. 1 2 3
 2

    {. 1 2 3
 1
    {: 1 2 3
 3
    }: 1 2 3
 1 2
    }. 1 2 3
 2 3

   2)  It makes clear whether {: is meant to be tail or drop (it is drop,
   isn't it? I'm really not sure and don't have a reference handy.)

drops begin with }

And, you can run a j session to find out what something that you're
not sure about does.  I find an active j session an invaluable tool
for programming in j.

   3) People who don't know J can read it.  For example, APLers often
   don't know J, but know what signum is even if they don't know what
   * is monadically.

Well, write for your audience, of course.  But a profile is [in my
opinion] a fairly minimal set of tools that help you write in the
language.

   4)  It's easier on my parenthesis matchers who get confused when I use
   a lot of { and [ things without ] and }.  (there... balanced and
   satisfied).

That can be an issue, but usually parenthesis matchers can be
reconfigured (good) or disabled (not so good).

   5) Some verbs are easier to read in long form, others not.  I
   prefer not to expand conjunctions for example.

I'm not sure what this means.

   Learning J would have been easier for me if it came with a profile
   redefining the primitives and popular idioms.  Maybe future
   versions will include such.  (maybe a primitives.js and idioms.js
   instead to keep the profile under control, but so people can use
   readable stuff when they feel a need.

I'm not so sure.  At one point, I wrote myself a profile that did
exactly that, and I found myself wasting even more time because I had
to look things up in the profile (to see the exact spelling) and in
the dictionary (to resolve whatever subtle point of definition I was
concerned with).

I am usually much happier when I define words which are relevant to
the model of abstraction I'm using.  Normally, I try to define words
which only have 0 or 1 sets of parenthesis [anything more than 2 and I
feel I need to apologize].  But I feel that defining words for the
primitive symbols is analogous to the practice some people have, in
procedural languages writing comments which don't illustrate the
purpose of the code:

        For i = 1 to 10                 //  increment i from 1 to 10

Raul D. Miller



Sun, 31 Mar 1996 07:20:29 GMT  
 J script file profile.js

I really appreciated Raul's paragraph telling his trick for remembering
how to distinguish the various take and drop extensions. Does anyone have
a similar help trick for the , ,. ,: ; ;. ;: conundrum?

--

College of Business Administration                         404-651-4070
Georgia State University                  interests: approx. reasoning,
Atlanta, Georgia USA 30303-3083     (B=)       decision support systems



Wed, 03 Apr 1996 03:35:11 GMT  
 J script file profile.js
Brian Schott:
   I really appreciated Raul's paragraph telling his trick for
   remembering how to distinguish the various take and drop
   extensions. Does anyone have a similar help trick for the , ,. ,: ;
   ;. ;: conundrum?

Thanks :-)

And, I guess I'll give it a go.  First off, note that ;. and ;: are
completely separate from the rest of this batch.  ;. is the cut
conjunction, and all the rest are verbs.  ;: is J's lexical analyzer
(which happens to treat newline as a token forming character).

It's true, however, that the rest are somewhat related to ,

,. is just ,"_1
,: is more complicated, and
;  gets into boxed arrays

In monadic form,
,  is Ravel
,. is ,"_1


In dyadic form
,  is listed as 'Append Items' in my doc, but I think of it as 'Over'
,. is ,"_1
,: is ,&,:  (using the monadic definition of ',:')
;  is link (box left arg, box right arg if not already boxed).

I hope I got that right -- I found a copy of Arithmetic, but not a
copy of the dictionary.  Anyways, consulting J directly, I got
   a=.i. 3 4 5 6
   ($ , a);($ ,. a);($ ,: a);($ ; a)        NB. monadic tests
+---+-----+---------+---+
|360|3 120|1 3 4 5 6|360|
+---+-----+---------+---+
   ($ 1, a);($ 1,. a);($ 1,: a);($ 1; a)    NB. dyadic tests
+-------+-------+---------+-+
|4 4 5 6|3 5 5 6|2 3 4 5 6|2|
+-------+-------+---------+-+

   $ itemize a
1 3 4 5 6
   $ 1 ,&itemize a
2 3 4 5 6
   $(,1),:a
2 3 4 5 6
   $(,1)(,&itemize)a
2 3 4 5 6

Anyways... I don't know what sort of confusion a person might have
about these, but maybe some of this will be useful to somebody.

Raul D. Miller



Thu, 04 Apr 1996 08:09:28 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. JS-EAI with *JS*-callback

2. js.exception 3279.js

3. profile.js

4. Script error: c:\windows\repair\rollback.js ??

5. NB. gray.js: a J verb that generates a grayscale postscript image from a 2d array

6. lapackTest.js

7. JS valueOf() in RB ?

8. JS for functional programming?

9. JS for functional programming?

10. prefs.js question

11. JS / VRML / HTML / camera binding

12. help: js generated vrml

 

 
Powered by phpBB® Forum Software