# EzDev.org

## j

semi clone of autojump (<a href="http://github.com/joelthelion/autojump">http://github.com/joelthelion/autojump</a>) in shell/awk

#### examples to compare tradtional math notations vs APL/J notations [closed]

I am reading a review to compare Mathematica to APL/J. One question raised in the article seems very interesting to me:

Is Mathematica really the way to go to express our creative thoughts – viz back to a 17th century notation designed for parchment instead of forward to a twentieth-century one designed for computers?

Can one share examples of Iverson's notation vs traditional math notation to demonstrate the edge of APL/J on expressing and solving math problems? This would be greatly helpful for new comers.

Source: (StackOverflow)

#### what are some of J's unique features?

I come from a background of C, Fortran, Python, R, Matlab, and some Lisp - and I've read a few things on Haskell. What are some neat ideas/examples in J or other languages from the APL family that are unique and not implemented in more common languages? I'm always interested in finding out what I'm missing...

Source: (StackOverflow)

#### What's the most efficient way to implement Haskell's foldl1 in J?

In Haskell there are two functions that allow one to perform an operation on a list of items in order to reduce it to a single value. (There are more than two, of course, but these are the two I'm interested in.) They are `foldl1` and `foldr1`. If the operation to be performed is commutative (such as addition), it doesn't matter which of these you use. The result will be the same. However, if the operation is not commutative (e.g., subtraction), then the two produce very different results. For example:

``````foldr1 (-) [1..9]
foldl1 (-) [1..9]
``````

The answer to the first one is 5 and to the second, -43. The J equivalent of `foldr1` is the insert adverb, `/`, e.g.,

``````-/ 1+i.9
``````

which is the equivalent of `foldr1 (-) [1..9]`. I want to create an adverb in J that works like the insert adverb, but folds left instead of right. The best I could come up with is the following:

``````foldl =: 1 : 'u~/@|.'
``````

Thus, one could say:

``````- foldl 1+i.9
``````

and get -43 as the answer, which is what is expected from a left fold.

Is there a better way to do this in J? For some reason, reversing the `y` argument does not seem efficient to me. Perhaps there is a way to do this without having to resort to that.

Source: (StackOverflow)

#### Chain verbs in J

Suppose a boxed matrix containing various types:

``````matrix =: ('abc';'defgh';23),:('foo';'bar';45)
matrix
``````
```+---+-----+--+
|abc|defgh|23|
+---+-----+--+
|foo|bar  |45|
+---+-----+--+
```

And a column descriptor:

``````columnTypes =: 'string';'string';'num'
``````

I want to apply verbs on this matrix by column according to types. I'll be using verbs DoString and DoNum:

``````chain =: (('string';'num') i. columnTypes) { DoString`DoNum
``````

EDIT: The column descriptors are important, the decision on which verb to use is based on them, not on the type itself. In reality, I could have several types of strings, numerics, and even dates (which would be numeric in J).

How do I apply `chain` to each row of `matrix`? The verbs themselves can take care of whether the passed value is boxed or not, that's fine. Also, I'd rather avoid transposing the matrix (`|:`) as it could be quite large.

Source: (StackOverflow)

#### J Primes Enumeration

J will answer the n-th prime via p:n.

If I ask for the 100 millionth prime I get an almost instant answer. I cannot imagine J is sieving for that prime that quickly, but neither looking it up in a table as that table would be around 1GB in size.

There are equations giving approximations to the number of primes to a bound, but they are only approximations.

How is J finding the answer so quickly ?

Source: (StackOverflow)

#### J turns carriage return into newline

I'm trying to implement a progress bar for a command line application, e.g.

``````[#####     ] 50% complete
``````

I know I can just backspace to the start of the line and overwrite, but that seems so gross. I'd rather use the carriage return to put the cursor at the first column and then overwrite.

The problem is that the J engine appears to not render the carriage return character, instead rendering a newline+carriage return.

Here is what I have tried:

`echo 'hi',(10{a.),'world'` (where `10{a.` is ASCII 10, i.e. carriage return) which prints

``````hi
world
``````

`echo 'hi',(13{a.),'world'` (newline) which prints

``````hi
world
``````

`shell 'printf "%s\r%s" hi world'` which prints

``````hi
world
``````

`shell 'printf "%s\n%s" hi world'` which prints

``````hi
world
``````

Finally, I tried all of the above in JHS instead of Jconsole, with identical results.

From this, three things are apparent:

1. The J front ends turn the carriage return into a carriage return + newline.
2. The J front end also processes carriage returns generated externally (for example by printf) into newlines.
3. J does recognize a newline by itself as shown in the last example.

Any help?

Source: (StackOverflow)

#### Creating a recursive tacit function in J

I'm a newcomer to J and I've been trying to create a Fibonacci function as an exercise (always the second function I create when learning a language). I just can't figure out what exactly is wrong in my way of doing it. I have tried to define it as tacit, but it gets hung if argument is greater than one.

``````fib =: [ ` ((\$: (]-1)) + (\$: (]-2))) @. (>&1)
``````

I've also attempted to create it explicitly, and that worked fine.

``````fib =: 3 : 'if. y>1 do. (fib (y-1)) + (fib (y-2)) else. y end.'
``````

I tried to create a tacit out of that by replacing 3 with 13, but it threw an error.

``````   fib =: 13 : 'if. y>1 do. (fib (y-1)) + (fib (y-2)) else. y end.'
|spelling error
|   if. y>1 do. (fib (y-1)) + (fib (y-2)) else. y end.
|   ^
|   fib=:    13 :'if. y>1 do. (fib (y-1)) + (fib (y-2)) else. y end.'
``````

So, I'm asking for someone to explain what exactly I am doing wrong here.

Source: (StackOverflow)

#### J and L-systems

I'm going to create a program that can generate strings from L-system grammars.

Astrid Lindenmayer's original L-System for modelling the growth of algae is:

```variables : A B
constants : none
axiom     : A
rules     : (A → AB), (B → A)
```

which produces:

```iteration | resulting model
0 | A
1 | AB
2 | ABA
3 | ABAAB
4 | ABAABABA
5 | ABAABABAABAAB
```

that is naively implemented by myself in J like this:

``````   algae =: 1&algae : (([: ; (('AB'"0)`('A'"0) @. ('AB' i. ]))&.>"0)^:[) "1 0 1
(i.6) ([;algae)"1 0 1 'A'
┌─┬─────────────┐
│0│A            │
├─┼─────────────┤
│1│AB           │
├─┼─────────────┤
│2│ABA          │
├─┼─────────────┤
│3│ABAAB        │
├─┼─────────────┤
│4│ABAABABA     │
├─┼─────────────┤
│5│ABAABABAABAAB│
└─┴─────────────┘
``````

Step-by-step illustration:

``````   ('AB' i. ]) 'ABAAB' NB. determine indices of productions for each variable
0 1 0 0 1
'AB'"0`('A'"0)@.('AB' i. ])"0 'ABAAB' NB. apply corresponding productions
AB
A
AB
AB
A
'AB'"0`('A'"0)@.('AB' i. ])&.>"0 'ABAAB' NB. the same &.> to avoid filling
┌──┬─┬──┬──┬─┐
│AB│A│AB│AB│A│
└──┴─┴──┴──┴─┘
NB. finally ; and use ^: to iterate
``````

By analogy, here is a result of the 4th iteration of L-system that generates Thue–Morse sequence

``````   4 (([: ; (0 1"0)`(1 0"0)@.(0 1 i. ])&.>"0)^:[) 0
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
``````

That is the best that I can do so far. I believe that boxing-unboxing method is insufficient here. This is the first time I've missed linked-lists in J - it's much harder to code grammars without them.

What I'm really thinking about is:
a) constructing a list of gerunds of those functions that build final string (in my examples those functions are constants like `'AB'"0` but in case of tree modeling functions are turtle graphics commands) and evoking (``:6`) it,
or something that I am able to code:
b) constructing a string of legal J sentence that build final string and doing (`".`) it.
But I'm not sure if these programs are efficient.

• Can you show me a better approach please?

Any hints as well as comments about a) and b) are highly appreciated!

Source: (StackOverflow)

#### How to install new modes in emacs 23 on OS X?

I just downloaded the Haskell and J modes off of SourceForge, and I'm having trouble figuring out how to make them interface with emacs 23. Google searches yield detailed instructions for emacs 22, but it looks like changes have been made that make it hard to figure out where I'm supposed to stick the source files. The internal documentation seems to assume more experience with emacs internals than I currently have, and the problem has resisted solution for several days. Does anyone know how to get these modes up and running?

EDIT: Thanks to Untwisted for supplying the answer. Ultimately, my real problems were these:

1) I didn't know that .emacs was invisible to the OSX finder but was visible through the terminal.

2) It never occurred to me that I could modify .emacs while an instance of emacs was running.

Once I realized that those were my problems, I used C-x C-f to open ~/.emacs, copied the text of Untwisted's fix, and modified it to reflect my personal file paths. After that, everything worked.

Source: (StackOverflow)

#### J: Tacit adverb of Newton's method

I've found in `'addons/math/misc/brent.ijs'` implementation of Brent's method as and adverb. I would like to build a Newton's method as an adverb too but it's much harder than building tacit verbs.
Here is a explicit version of Newton's iteration:

``````   newton_i =: 1 : '] - u % u d.1'
``````

With such usage:

``````   2&o. newton_i^:_ (1) NB. (-: 1p1) must be found
1.5708
2 o. 1.5708 NB. after substitution we get almost 0
_3.67321e_6
``````

And of course, for convenience:

``````    newton =: 1 : 'u newton_i^:_'
``````

What's a tacit equivalent?

Source: (StackOverflow)

#### How to remove an element from a list in J by index?

The rather verbose fork I came up with is

``````({. , (>:@[ }. ]))
``````

E.g.,

``````3 ({. , (>:@[ }. ])) 0 1 2 3 4 5
0 1 2 4 5
``````

Works great, but is there a more idiomatic way? What is the usual way to do this in J?

Source: (StackOverflow)

#### "Reversing" a hook in J

I want to put the operation which takes all the items in a list which are greater than 2 into a pointless (as in not explicitly capturing the argument in a variable) function in J. I wanted to do this by using `~` with a hook, like `f =: ((> & 2) #)~` but it seems like neither that nor `((> & 2) #~)` works.

My reasoning was that my function has the form `(f y) g y` where `y` is the list, `f` is `(> & 2)`, and `g` is `#`. I would appreciate any help!

Source: (StackOverflow)

#### Pros / Cons of Tacit Programming in J

As a beginner in J I am often confronted with tacit programs which seem quite byzantine compared to the more familiar explicit form.

Now just because I find interpretation hard does not mean that the tacit form is incorrect or wrong. Very often the tacit form is considerably shorter than the explicit form, and thus easier to visually see all at once.

Question to the experts : Do these tacit forms convey a better sense of structure, and maybe distil out the underlying computational mechanisms ? Are there other benefits ?

I'm hoping the answer is yes, and true for some non-trivial examples...

Source: (StackOverflow)

#### Is there a J idiom for adding to a list until a certain condition is met?

Imagine you're generating the Fibonacci numbers using the obvious, brute-force algorithm. If I know the number of Fibonaccis I want to generate in advance, I can do something like this using the power conjunction `^:`:

``````(, [: +/ _2&{.)^:20 i.2
``````

How can I instead stop when the Fibonaccis reach some limit, say `1e6`? (I know how to do this inside a function using `while.`, but that's no fun.)

I want to stress that this is a general question about J, not a specific question about Fibonacci. Don't get too distracted by Fibonacci numbers. The heart of the question is how to keep appending to a list until some condition is met.

Source: (StackOverflow)

#### Multi-core J -- Parallelisation

Is there a way to get J to use multiple cores ? I thought part of the benefit of APL/J was that the language constructs lent themselves well to parallel solutions.

Looking at my CPU usage (I'm on OSX) there's clearly only a single processor in use.

I've got a heavy-ish function f acting on a list, and I don't see why it couldn't divide the list into 4 pieces, and re-assemble the results ?

Source: (StackOverflow)