One of the virtues of J is its conciseness; I think this is underrated in exploratory programming. Here I present some J one-liners alongside samples of other languages.

GCD of Random Integers

Problem: pick two random integers between 1 and 1,000,000,000 and find their GCD.

This Python solution is from @MemeArcana:

>>> from random import randint >>> from math import gcd >>> gcd(*[randint(1, 1_000_000_000) for _ in range(2)]) 1

Compare to J

+./ 2?1000000000 1

Shuffle a 1-D Array

Python:

>>> import random >>> random.shuffle(array)

J:

(?~@#{[)

This is characteristic of the APL family: it doesn't depend on a library but it still uses fewer characters!

This can be extended to an array of arbitrary dimension:

($ $ (?~@#{[)@,)

Linear Regression

Python and R from Richard Dinga:

lm(y ~ x)

>>> import numpy as np >>> from sklearn.linear_model import LinearRegression >>> reg = LinearRegression().fit(x, y)

J:

y %. 1 ,. x

Sample Without Replacement

To sample x items from a 1-D array y:

x {.?~# y

Generate a Random Permutation and Find its Inverse

C example from the GSL manual:

#include <stdio.h> #include <gsl/gsl_rng.h> #include <gsl/gsl_randist.h> #include <gsl/gsl_permutation.h>

int main (void) { const size_t N = 10; const gsl_rng_type * T; gsl_rng * r;

gsl_permutation * p = gsl_permutation_alloc (N); gsl_permutation * q = gsl_permutation_alloc (N);

gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc (T);

gsl_permutation_init (p);

gsl_ran_shuffle (r, p->data, N, sizeof(size_t));

gsl_permutation_inverse (q, p);

gsl_permutation_free (p); gsl_permutation_free (q); gsl_rng_free (r);

return 0; }

In J:

/: ?~ 10