Index   <<   >>

Have a fit

Fit the first: How would you write a verb to shift a list left k places and shift in k 0’s at the right? Perhaps you would drop k items from the left of the list and append k 0’s at the right. J provides a variation (fit) (!.) of the rotate verb (|.) to do this:

   (k=.7) |. !. 0 (y=.i.9)
7 8 0 0 0 0 0 0 0

And if you wanted a right shift with fill 99 :

   (-k) |. !. 99 y
99 99 99 99 99 99 99 0 1

The specified fill must be an atom:

   _7|.!.'WXYZ' 'abcdefghijklmn'
|length error
|   _7    |.!.'WXYZ''abcdefghijklmn'
   _7|.!.'W' 'abcdefghijklmn'
WWWWWWWabcdefg

Fit the second: Have you ever wanted the fill used with take ({.) to be arbitrary? J provides a variation of take ({.) to do this:

   20{.!.'*' '$12,345.00'
$12,345.00**********

Fit the third: How does J handle variations in comparison tolerance if it doesn’t have the comparison tolerance system variable? This is another use of the variation conjunction (!.).

   1=(eps=.1e_14)+1
1
   1=!.0 eps+1 NB. zero tolerance
0
   <.1-eps NB. floor is <.
1
   <.!.(0) 1-eps
0

Fit the fourth: The variant x^!.d n of the power verb is defined as */x+d*i.n . Thus, ^!.0 is equivalent to ^ , and ff=.^!._1 is the falling factorial, and rf=.^!.1 is the rising factorial.

   (10 ff 3),10*9*8
720 720
   (10 rf 3),10*11*12
1320 1320