On 2020-07-02 03:48, ethiejiesa via Programming wrote:
Hey J,

Wanted to share this and see what you think.

For Project Euler solutions in J, I have a single file and run it like this:

$ ./project-euler.ijs <1> [<m> ..] # output answers to problem n, m, ...

Initially, I had a scheme where all solutions are named like p1, p2, etc. with
the following at the bottom of the script:

    echo ".&> (#~ 0 = 4!:0) 'p'&,&.> ARGV
    exit@0:^:(e.~ <@'./project-euler.ijs') ARGV

It just straightforwardly tacks 'p' to the beginning of all arguments, filters out the missing/nonsense stuff, and executes the result. The second line ensures the script exits when called directly but not when 'load'ed in the
jconsole.

Anyway, the above scheme works but execution starts to get slow as more
solutions accumulate since it ends up computing *all* p1, p2, etc. before output. So I wanted to "lazily" evaluate only those solutions requested on the
command line.


Right now you filter ARGV for names of nouns (#~ 0 = 4!:0) and
then print their values, which therefore have to have already
been computed. What you could do instead is filter for names of
verbs (#~ 3 = 4!:0) and then execute them.

     p1 =: echo bind 'called p1'
     p2 =: echo bind 'called p2'
     p3 =: echo bind 'called p3'
     vs =: (#~ 3 = 4!:0) 'p'&,&.> '1' ; '3' ; 'ignored'
     vs@.] i.#vs
  called p1
  called p3

'p1' then would have to be a verb that performs the calculation.
You might be able to get by with taking your existing code and
just wrapping it:

  n =: ...
  m =: ...
  p1 =: ...

might become:

  p1 =: 3 : 0
  n =: ...
  m =: ...
  ...
  )

My first approach was to "lazy load" by making all p1, p2, etc. strings and
evaluating with

    0!:111 ".&> (#~ 0 = 4!:0) 'p'&,&.> ARGV

However, this strikes me as a bit ugly. It also makes it a bit awkward to
investigate and iterate on solutions when loading into jconsole.

So the scheme I came up with is a simple modification of the initial approach. Instead of p1, p2, etc. we now have [x1], v1, y1, [x2], v2, y2, etc. (where [x1] means that x1 is optional). Effectively we want to expand the argument '1' into 'x1 v1 y1' or 'v1 y1' as appropriate and then execute. This is how I am
achieving that:

    echo ". ,"_1 ,&' '&> |: (#~"1 (0 3 0) = 4!:0) 'xvy' ,&.>/ ARGV

It's quite nice how closely this mirrors the original approach. After the hook, we have a table whose columns encode the executions we want. Finally, a bit of
munging gives the desired result.

... oh, OK, this is a way to only make some of the more
expensive computations verbs to be run.


Thoughts? Is this Good J TM?

For your purposes what I would really do is skip the script
entirely and use the JQt environment, with my p1, p2, etc. code
in an editor. When I want to calculate p1, just ctrl-enter down
that part of the file, or select it and ctrl-e. All the code is
visible and can be edited. None of it's run automatically. You
can have multiple versions right next to each other, next to your
thoughts about them, next to some benchmark or tests of them.
The console right next to the editor lets you inspect things,
and then you have additional tools like debug/dissect.

It's really J's terseness that makes this an option at all.

You could go further and make it a lab. The format's very easy,
even the old format, you can interactively run what you like
just the same, but now there's the option of someone stepping
through it as a lab.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to