Playground

From J Wiki
Jump to navigation Jump to search
Playground | Shortcuts

The J Playground runs an interactive J session.

To try it out, Click Here, or use the link in the wiki menu on the left.

JPlayground.png

There are two windows: Term, an interactive console, and Edit, a script editor. The windows can be resized and flipped to be stacked vertically.

Term

The Term window lets you type in and run J expressions.

  • if the cursor is on the last line of the display, press Enter to execute it.
  • if the cursor is on a line above the last line of the display, press Enter to copy it to the end of the display. You can then edit it and press Enter to execute it.

The window can be edited as you wish, for example to delete unwanted output.

The input log can be accessed either

  • by pressing the Ctrl+Shift up/down cursor keys. This steps through the inputs.
  • by pressing Ctrl+D or selecting menu View|Input Log. This shows a popup menu, where selecting an entry copies it to the end of the display.

Edit

The Edit window is for entering one or more J expressions for later execution.

You can select from the Examples menu to populate the Edit window with typical expressions, or enter your own expressions.

To run the current line, put the cursor on it and press Ctrl+Enter or Ctrl+Shift+Enter. The difference is that when the expression is an assignment, Ctrl+Enter has no output while Ctrl+Shift+Enter displays the assigned value.

You can run through all the J expressions in a window by holding down Ctrl or Ctrl+Shift and repeatedly pressing Enter. Alternatively, select menu Edit Run|All Lines.

If the current line starts a multi-line definition, the entire definition is read in.

Context Sensitive

Context sensitive help is available for J language primitives.

In either window, select the primitive, or put the cursor inside or to the left of a primitive. Press Ctrl+F1 for the vocabulary definition.

For example, with the cursor inside i. this will show the definition of Integers.

Also, in the Term window, put the cursor spaced out to the left or right of an expression, then press Ctrl+F1 to output J word formation on the line, e.g.

    2 3 4 +/ i. 5
 ┌─────┬─┬─┬──┬─┐
 │2 3 4│+│/│i.│5│
 └─────┴─┴─┴──┴─┘


Tips & Tricks

File handling

Standard library file handling verbs can be used, like dir, freads and fwrites. They operate on a temporary virtual filesystem that is isolated from the host filesystem, and lost when refreshing the page. Files can be loaded into the editor using Edit_plj_ for editing or copy-pasting elsewhere. Do note this might, without warning, overwrite any changes in the Editor. Edit_plj_ gets the content from the Editor into J when used monadically, and sets the content (overwriting the previous content without warning), when used dyadically. For instance:

   dir '~addons/math/misc/'
amoeba.ijs            7730 21-Aug-24 21:25:41
bigpi.ijs              501 21-Aug-24 21:25:41
brent.ijs             1799 21-Aug-24 21:25:41
build.ijs              138 21-Aug-24 21:25:41
cheby.ijs             2073 21-Aug-24 21:25:41
contfrac.ijs           814 21-Aug-24 21:25:41
det.ijs                444 21-Aug-24 21:25:41
fermat.ijs             851 21-Aug-24 21:25:41
gamesolver.ijs        2598 21-Aug-24 21:25:41
gcd.ijs                711 21-Aug-24 21:25:41
integer.ijs           2332 21-Aug-24 21:25:41
integrat.ijs          2879 21-Aug-24 21:25:41
jacobi.ijs            1387 21-Aug-24 21:25:41
legendre.ijs          1087 21-Aug-24 21:25:41
linear.ijs            4833 21-Aug-24 21:25:41
makemat.ijs           2266 21-Aug-24 21:25:41
manifest.ijs          1795 21-Aug-24 21:25:41
matfacto.ijs          3376 21-Aug-24 21:25:41
mathutil.ijs           886 21-Aug-24 21:25:41
matutil.ijs           3337 21-Aug-24 21:25:41
mean.ijs               431 21-Aug-24 21:25:41
... NB. output shortened for brevity
 
   Edit_plj_~ freads '~addons/math/misc/mean.ijs' NB. load mean.ijs in the Editor

   '~addons/math/misc/mean.ijs' fwrites~ Edit_plj_'' NB. After editing, save the file back in place (for this session only).

   freads '~addons/math/misc/mean.ijs' NB. read back changed file.
NB. math/misc/mean
NB. Various means
NB. version: 1.0.0

NB. arithmean   arithmetic mean
NB. geomean     geometric mean
NB. harmean     harmonic mean
NB. commonmean  common mean

NB.*arithmean v Arithmetic mean of y
arithmean=: +/ % #

NB.*geomean v Geometric mean of y
geomean=: # %: */

NB.*harmean v Harmonic mean of y
harmean=: arithmean &. (%"_)

NB.*commonmean v Common mean of y
commonmean=: {. @ ((geomean,arithmean) ^: _)

NB. I added this line in the Playground editor

Of course, written files do not remain, but are reset after reloading the web page.

Graphics

The J Playground lets you make nice plots, which are rather small for more complex plots or multi-plots. You can set the resolution of the plot canvas by e.g.

   load'plot'
   CANVAS_DEFSIZE_jzplot_
400 200
   CANVAS_DEFSIZE_jzplot_=:600 400
   plot _6 6;'sin'

Viewmat is also supported, but sometimes triggers the following error:

|assertion failure: enclength
|       (0<:ex)*.(30>:ex)
|[-0] 

This can be prevented by redefining enclength_jzlib_ as follows to increase the upper limit the assertion fails on:

enclength_jzlib_=: 3 : 0
ix=. <: +/({:"1 lz_length)<:y
code=. 257 + ix
ex=. y-(<ix,1){lz_length
assert. (0<:ex)*.(300>:ex) NB. upped limit from 30 to 300 here.
code, 1000+ex
)