Addons/general/misc

From J Wiki
Jump to navigation Jump to search

general/misc

Before using these scripts you will need to ensure they have been installed. This can be done on a connected machine through J's package manager, or from the command line:

   load 'pacman'
   'install' jpkg 'general/misc'

If you are working offline and need to install these scripts you will need to obtain a copy of an archive containing them and unpack it in J's addons directory.

You can find the absolute location of these scripts using jpath. For example, on windows, you might get behavior like this:

   jpath '~addons/general/misc/prompt'
c:/users/username/j64-803/addons/general/misc/prompt

Usually, require is sufficient to use any of these scripts within a session, for example:

   require 'numeric validate'
   1 (],roundbanker,:rounddist) 0.1*?10#100
9.4 8.2 3.5 7.9 9 2.3 8.1 5.2 6.7 3.4
  9   8   4   8 9   2   8   5   7   3
  9   9   3   8 9   2   8   6   6   4
   ismatrix 1 (],roundbanker,:rounddist) 0.1*?10#100
1

If open and edit are defined in your session, they can be useful for inspecting the code. For example:

   open 'general/misc/jdll'
   edit 'roundbanker'

If edit is not defined but open is, you could use, for example:

   open (4!:4<'roundbanker'){4!:3''

... and then search for the definition of roundbanker within that file. (This lets you read the comments on the code, if there are any.)

'guid'

May also be referred to as 'general/misc/guid'

Create guids in various formats

All guid verbs take the prefix of the shape of guids to return, eg

   require 'general/misc/guid'
   $guids $0     NB. create guid as a 16-byte character string
16
   $guids 2 3    NB. create 2x3 array of 16-byte strings
2 3 16
guids return guids as 16-byte strings
guidsn return guids as 24-character file names
guidsr return guids as 36-character strings, without braces
guidss return guids as 38-character strings, with braces
guidsx return guids as extended precision integers

'inverted'

May also be referred to as 'general/misc/inverted'

See: Essays/Inverted Table

'numeric'

May also be referred to as 'general/misc/numeric'

Various numeric utilities

baserep y in base x
clean clean y to tolerance of x (default 1e_10)
colsum sum data columns of matrix by key
e.g. if column 2 of mat is age, then
   2 colsum mat
sums the remaining columns by age
groupndx group indices of y in x
x is an integer vector of the starting numbers of each group,
assumed to be in ascending order. e.g.
   0 3 6 groupndx i.8
0 0 0 1 1 1 2 2
int01 interval in n steps from 0 to 1 (= steps 0 1,n)
linsert linear insert x (default 2) steps into y
randomize sets a random value into random link
range range from a to b [in steps of c]
recur solves recurrence r(i)=a(i)+r(i-1)*m(i-1)
if: r = m recur a
 r(0) = a(0)
 r(i) = a(i)+r(i-1)*m(i-1)
 m is repeated cyclically as necessary, e.g.
   1.05 1.10 recur 100 100 100 100 100
100 205 325.5 441.775 585.953
round round y to nearest x (e.g. 1000 round 12345)
roundbanker round y to nearest x with bankers rounding
rounddist round y to nearest x preserving total, e.g.
   0.1 rounddist 6$0.45
0.5 0.4 0.5 0.4 0.5 0.4
rounddown round y down to nearest x
roundint round to nearest integer
roundup round y up to nearest x
steps steps from a to b in c steps

'pack'

May also be referred to as 'general/misc/pack'

Package utilities

a package is a 2-column matrix of: name, value that can be used to store nouns, or otherwise associate names and values.

A name is any character vector. pack and pdef work only when the names are proper J names.

definitions for nouns only:

  pk=. pack nl create package from namelist
  nl=. pdef pk define package

definitions for any names:

  text=. pk1 pcompare pk2 compare packages
  val=. name pget pk get value of name in package
  pk=. new pset old merge new and old packages
  pk=. nl pex pk remove namelist from package
  pk=. nl psel pk select namelist from package
  res=. packlocale locs package all nouns in locales

'parts'

May also be referred to as 'general/misc/parts'

Partition functions

Partitions are typically given by a boolean with 1's marking the beginning or end of each partition. Partitions are also sometimes given in terms of the lengths of each partition.

Verbs defined here assume that boolean 1's mark the beginning of each partition - see comments at the end for examples of end-partition verbs.

The verbs psum and psumscan illustrate definitions appropriate for verbs that return scalar or non-scalar results, respectively. utility used to build non-scalar partition functions:

Examples:

   require 'general/misc/parts'
   firstones 1 1 0 1 1 1 0
1 0 0 1 0 0 0
   lastones 1 1 1 0 1
0 0 1 0 1
   lfp 1 0 1 0 0 1
2 3 1
   partition 1 9 9 4 4 4 9
1 1 0 1 0 0 1
   runindices 3 1 2 1
0 0 0 1 2 2 3
   runlengths 1 9 9 4 4 4 9
1 2 3 1

   x=: 1 0 1 0 0
   y=: 5 3 2 2 7

   x preverse y
3 5 7 2 2
   x psort y
3 5 2 2 7
   x psum y
8 11
   x psumscan y
5 8 2 4 11

'trace'

May also be referred to as 'general/misc/trace'

Execution trace utilities

The main functions are "trace" and "paren".

x trace y trace sentence y to x levels of function calls
trace y same as _ trace y

For example:

   require 'trace'
   trace '3+i.4'
 --------------- 1 Monad ------
 i.
 4
 0 1 2 3
 --------------- 2 Dyad -------
 3
 +
 0 1 2 3
 3 4 5 6
 ==============================
3 4 5 6

Tracing provides information on results within a line; the action labels 0 monad, 1 monad, 9 paren, etc., are from the parse table in Section II.E of the J dictionary.

paren y fully parenthesize sentence y

For example:

   require 'trace'
   paren '+/i.4'
((+/)(i.4))
   paren '(3 4$i.12)+/ .**:i.4'
((3 4$(i.12))((+/) .*)(*:(i.4)))

See also: Scripts/Tracer

'validate'

May also be referred to as 'general/misc/validate'

Data validation functions

data validation

verbs return 1 if true, 0 if false

data type verbs check the data, not the internal representation. For example, 1j1-0j1 is considered boolean, though stored as complex.

Examples:

   require 'general/misc/validate'
   5 10 inrange i.12

   '()' isbalanced '(123(45))'

The provided verbs are:

inrange (low, high) inrange data
isbalanced pair isbalanced string
isboolean data is all 0 or 1
isboxed is boxed
ischaracter data is character
iscounter data is non-negative integer (counting number)
iscomplex data is complex
isdate is date (as yyyy mm dd)
isempty data is empty
isinteger data is all integer
ismatrix data is a matrix
isnumeric data is numeric
isrational data is rational
isreal data is all real
isscalar data is a scalar
isunicode data is unicode
isunique data has no duplicates
isutf8 text is valid UTF-8
isvector data is a vector

'general/misc/*'

These scripts do not have short aliases.

So, for example,    require 'general/misc/format general/misc/prompt' but not:    require 'format prompt'

'general/misc/bigfiles'

Handle files larger than 2^31 bytes (for 32 bit windows).

'general/misc/clippaste'

microsoft windows specific alternative to wd'clippaste'.

See: Guides/Clipboard

See also: Guides/Window Driver/Command Reference, Addons/gui/droidwd, and Addons/gui/gtkwd

'general/misc/evolute'

See http://www.jsoftware.com/papers/play132.htm

'general/misc/fndef'

Supposedly support for beginners to write code in "a more literate style".

Needs considerably more documentation (book(s), lab(s), tutorial(s), etc...) for this approach to be accessible to beginners. So far, all we have is Guides/Primitives

'general/misc/fndisplay'

This script allows you to see how hooks, forks, and other structural elements affect the order of processing verbs.

First, select the kind of display you want with 'setfnform'.

The operands are 'math' (display like f(x,y)), 'J' (display like x f y), and 'Jv' (like J, but monads are followed by '_' and dyads are preceded and followed by '_'). Example:

   require 'general/misc/fndisplay'
   setfnform 'math'

Then, define the verb-names you want to use:    defverbs 'f g h'

Finally, define any names you want to use for nouns:    defnouns 'x y'

Then, entering combinations of your verbs and nouns will show you how the combinations will be executed.

By default a verb has infinite rank; you can append "r to a verb-name in defverbs to assign the rank r to the verb.

Example:

   x f&.g y
+----------------+
|g`(f(g(x),g(y)))|
+----------------+

where g` means the inverse of g.

See also: Vocabulary/Dissect or 'trace'

'general/misc/font'

Utilities to manipulate font family names.

See: Guides/Window Driver/Fontspec

This is similar to http://www.w3.org/TR/CSS21/fonts.html but not quite the same.

'general/misc/format'

Formatting utilities

center width center text
clipfmt format data for clipboard
clipunfmt unformat data read from clipboard
colhdr column headers
expandby expand data with fit value
expandn expand data at every nth item
flatten flatten array to a character string
fold fold text to width
hexdump show text as hex and ascii characters
nfmt simple numeric formatter
ruler returns formatted ruler
sqzint squeeze list of positive integers into short form
sqzrun squeeze list of numbers into short form
xfmt format extended integers

'general/misc/jdll'

This script gives examples of calling the J DLL from J. Similar routines may be written in other languages that can call DLLs.

The internal representation used is that for intel machines.

main definitions:

jclear clear J session
jcmd send sentence, return result
jdo send sentence to be executed
jget retrieve value of J noun
jset assign value to J noun

utilities:

jinit initialize J instance
jfree free J instance

Loading the script initializes a J instance, defining global pJ as the handle. Use jfree to free it.

See examples at the end of the script itself, and http://www.jsoftware.com/help/dictionary/dx003.htm

See also: http://www.jsoftware.com/help/user/dlls.htm

http://www.jsoftware.com/docs/help602/user/script_dll.htm might also be of historical interest.

'general/misc/prompt'

prompts for input, optionally with a default result

form: [default] prompt prompt_text

examples:

   require 'general/misc/prompt'
   prompt 'start date: '
   '2001 5 23' prompt 'start date: '

Notes:

. - the default is only available in JQt . - this will not work in a script . - in Windows console a newline is added after the prompt