User:Martin Kreuzer/Playground

From J Wiki
Jump to navigation Jump to search


(Simple) Fork Example

Working this example step by step (sections* active are in regular print, rest is <f>faded</f>):
* Each section, taken isolated, may not neccessarily be valid J.

NB. dyadic case:

   <f>2 1 3</f> (<f>+ 4 <. >./ -</f> <./) 3 1 4 1 5 9   <f>NB.</f> get Min of y
   <f>2 1 3</f> (<f>+ 4 <.</f> >./ <f>- 1</f>) 3 1 4 1 5 9     <f>NB.</f> get Max of y
   <f>2 1 3</f> (<f>+ 4 <.</f> 9 - 1)<f> 3 1 4 1 5 9</f>       <f>NB.</f> calc the difference
   <f>2 1 3</f> (<f>+</f> 4 <. 8)<f> 3 1 4 1 5 9</f>           <f>NB.</f> take the lesser of 4 and 8
   2 1 3 (+ 4)<f> 3 1 4 1 5 9</f>                <f>NB.</f> add 4 to the list x
6 5 7                                     <f>NB.</f> result

NB. monadic case:

   ∙∙∙∙
   (+ 4) 3 1 4 1 5 9                      <f>NB.</f> add 4 to the list y
7 5 8 5 9 13                              <f>NB.</f> result     

• Example: Parsing HTML
Given: Table Structure and Content.
mytable=: 0 : 0
<table ...>
 <tr ...>-1----------row1----------------1-</tr>
 <tr ...>-2----------row2-----------------------------2-</tr>
 <tr ...>-3----------row3---------------------3-</tr>
</table>
)
   dataypte mytable
literal

Task: Parse the table and pick content of Row2.
Remark: Script strings.ijs (providing dyadic verbs taketo and takeafter) is part of the J standard library.

   y=. mytable;

   '<tr' takeafter y                                           NB. that's aiming at Row1
 ...>-1----------row1----------------1-</tr>
 <tr ...>-2----------row2-----------------------------2-</tr>
 <tr ...>-3----------row3---------------------3-</tr>
</table>

   '<tr' takeafter(^:2) y                                      NB. that's better, near Row2
 ...>-2----------row2-----------------------------2-</tr>
 <tr ...>-3----------row3---------------------3-</tr>
</table>

   '>' takeafter '<tr' takeafter(^:2) y                        NB. getting rid of tag closing
-2----------row2-----------------------------2-</tr>
 <tr ...>------------row3---------------------3-</tr>
</table>

   '</tr' taketo '>' takeafter '<tr' takeafter(^:2) y          NB. Row2's content, as requested
-2----------row2-----------------------------2-