Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

J : creating a family of verbs from an array

Tags:

j

The following expression shows a truth table for each of the 16 primitive Boolean operations:

   (0 b./; 1 b./; 2 b./; 3 b./; 4 b./; 5 b./; 6 b./; 7 b./; 8 b./; 9 b./; 10 b./; 11 b./; 12 b./; 13 b./; 14 b./; 15 b./) ~ i.2
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│0 0│0 0│0 0│0 0│0 1│0 1│0 1│0 1│1 0│1 0│1 0│1 0│1 1│1 1│1 1│1 1│
│0 0│0 1│1 0│1 1│0 0│0 1│1 0│1 1│0 0│0 1│1 0│1 1│0 0│0 1│1 0│1 1│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

How can I refactor the part in parentheses to remove the duplication?

clarification:

The goal here isn't to produce the table, but rather to learn how to produce new verbs dynamically. In order to reduce the parenthesized expression, I would want to factor out the symbols ; , / and b. , and then replace the numbers with i.10.

The ; symbol is easy enough:

   ;/i.16
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬──┬──┬──┬──┬──┐
│0│1│2│3│4│5│6│7│8│9│10│11│12│13│14│15│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴──┴──┴──┴──┴──┴──┘

But I'm having a hard time finding a way to produce a new verb from each element in the list.

I think maybe I'm looking for some kind of higher-order combinators that allow using & and @ with something other than constants.

For example, nn leftBondEach v might make an array of verbs equivalent to n0 & v; n1 & v; ... ; nn & v :

   bverbs =: (i.16)(leftBondEach)b. NB. would mean (0 b.; 1 b.; ...; 15 b.)
   0 bverbs 0
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│0│0│0│0│1│1│1│1│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
   0 bverbs 1
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│0│0│0│0│1│1│1│1│0│0│0│0│1│1│1│1│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘

I think I need something similar in order to append the / to each function.

Then again, this is J and there's probably a completely different way to approach these problems that I just haven't grokked yet. :)

like image 382
tangentstorm Avatar asked Mar 23 '23 00:03

tangentstorm


1 Answers

What about:

<"2@|:@( (i.16) b./~) 0 1

    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
    |0 0|0 0|0 1|0 1|0 0|0 0|0 1|0 1|1 0|1 0|1 1|1 1|1 0|1 0|1 1|1 1|
    |0 0|0 1|0 0|0 1|1 0|1 1|1 0|1 1|0 0|0 1|0 0|0 1|1 0|1 1|1 0|1 1|
    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

Just noticing that $ (i. 16) b. /~ 0 1 is 2 2 16, and you'd want to have 16 boxes with size 2x2 ...

like image 157
jpjacobs Avatar answered May 16 '23 07:05

jpjacobs