1

~subsetpark/jnj

 2 years ago
source link: https://git.sr.ht/~subsetpark/jnj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

#JNJ (J iN Janet)

JNJ provides bindings to the J language to be used in Janet code.

J is a powerful array programming language in the APL lineage. It has a few features that might make it useful to expose from within a more general-purpose language:

  • Extremely terse and expressive notation for a very wide range of mathematical procedures
  • Highly optimized numeric code
  • Native support for arbitrary-dimensioned arrays

#Installation

JNJ requires the presence of libj.{so,dylib,dll}, provided by the J Language. You might need to install an -devel package for J depending on your package manager.

It will attempt to load libj.so at runtime. If libj.so is not present in your search path for dlopen(), you can specify the path when you install jnj:

LIBJ_DIR=/directory/containing/sofile jpm install https://git.sr.ht/~subsetpark/jnj

#Usage

See eval/eval* and j/j* for details.

All of the above functions have the same basic signature: they take a verb and 0, 1, or 2 arguments.

The verb can be any J sentence, verb or verb sequence. It can be a symbol or a string.

If there are no arguments, the sentence will be evaluated and returned.

repl:3:> (jnj/j "3 4 $ i. 10")
((0 1 2 3) (4 5 6 7) (8 9 0 1))

Arguments should be native Janet terms or the result of a jnj/eval call. If they're present, the verb will be applied to them. If there's a single argument, it will be evaluated in the form <VERB> <X>. If there are two arguments, X and Y, they will be evaluated in the form <X> VERB <Y>.

repl:4:> (jnj/j "$" [3 4] (range 10))
((0 1 2 3) (4 5 6 7) (8 9 0 1))
repl:5:> (def mat (jnj/eval "$" [3 4] (range 10)))
<jnj/jarray float [2/12]>
repl:6:> (jnj/j '$ mat)
(3 4)

#jnj

eval, eval*, j, j*, j-engine, to-matrix

#eval

function | source

(eval verb & args)

Evaluate verb and args with the default J engine instance (see eval* for details).

#eval*

function | source

(eval* je verb & args)

Evaluate verb with arguments args in the context of je. Returns a j-array abstract type which can be used for further evaluations, or converted into a tuple matrix.

#j

function | source

(j verb & args)

Evaluate verb with args in the default J engine instance (see j* for details).

#j*

function | source

(j* je verb & args)

Evaluate verb with args in the context of je, returning a native Janet datatype:

  • a matrix of nested tuples for anything with rank > 0
  • a Janet atom (string, number) otherwise

#j-engine

keyword | source

<jnj/je J Engine 0x56257e4fe000>

The default J instance, used by jnj/j.

#to-matrix

function | source

(to-matrix j-array)

Turn a j-array result abstract type into nested tuples of the appropriate shape.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK