# NYCJUG/2013-12-10

simple simulation, sophisticated interface, APL in a browser, spawn J process using sockets, why learn Javascript, example of online interactive tool, education for computational thinking, possible signs of the Apocalypse

## Meeting Agenda for NYCJUG 20131210

1. Beginner's regatta: detailed explanation of a simple simulation in J: see "More Boys or Girls.doc". 2. Show-and-tell: an example of a sophisticated interface - see "Some Interface Examples.doc". Running APL in a browser: see "Using APL to Write Browser Code.doc". 3. Advanced topics: distributed processing with J using sockets - see "Spawning J Processes Using Sockets.doc". Another reason to understand JavaScript - see "If you want to code from home - learn Javascript.doc". 4. Learning and teaching J: a good idea for an interactive, online tool: see "explainShell-parseAndExplainUnixShellCmds.doc". Some ideas for teaching programming - see "EducationTowardComputationalThinking.doc".

## Beginners Regatta

### More Boys or Girls?

Here’s a question from Math Overflow: In a country in which people only want boys, every family continues to have children until they have a boy. If they have a girl, they have another child. If they have a boy, they stop. What is the proportion of boys to girls in the country?

Here's a simple simulation of this problem:

simChildrenUntilBoy=: 3 : 0 gen=. 0 [ nbg=. 0 0 NB. Which generation we're on, # boys (0), girls (1). while. y>0 do. y=. +/births [ gen=. >:gen [ nbg=. nbg++/0 1 =/~births=. y?@$2 end. gen,nbg )

#### Detailed explanation of the code

We initialize gen to keep track of how many iterations we need to run and `nbg` to keep count of the number of boys (`0{nbg`) and girls (`1{nbg`).

In our while loop, we simulate the birth of y boys and girls by picking random zeros and ones; the phrase `y?@$2` combines reshape (`$`) and roll (`?`). Roll’s right argument of "2" restricts the random selection to the first two integers (0 and 1): we assign this binary vector to the variable births because we’ll need to re-use the result.

We count how many of each – assuming zero stands for a boy and one stands for a girl – by comparing the vector of births to both zero and one (`0 1=`) with the table adverb (`/`), reversing the order of the arguments (`~`) to give us a two-column, rather than a two-row table, so that the summation (`+/`) can act on the default axis (the 0th). Finally, we add this sum to our running total in `nbg` and update this variable with the new value.

The last two things we do are to increment the `gen` counter, and add up the number of girls born to give us the number of families who will try again the next time, per the problem statement. Once each of the families has at least one boy, we’ll exit the while loop and return the result of number of generations, number of boys, number of girls.

simChildrenUntilBoy 10 NB. Does result looks reasonable for small number? 7 10 10 NB. 7 generations, 10 boys, 10 girls.

After we check that the result looks reasonable for a small sample, we’ll run again with a larger number - simulating 1000 families - and time how long this takes.

6!:2 'gbg=. simChildrenUntilBoy 1000' 0.0416744 gbg 12 1000 997

It seems to run very quickly for 1000, so let’s time it for a million, then with higher numbers if this seems reasonably quick.

6!:2 'gbg=. simChildrenUntilBoy 1e6' 6.99011 gbg 22 1000000 999247 6!:2 'gbg=. simChildrenUntilBoy 1e7' 0.363471 gbg 23 10000000 9998654 6!:2 'gbg=. simChildrenUntilBoy 1e8' 3.07639 gbg 27 100000000 100007501 6!:2 'smoutput simChildrenUntilBoy 1e8' 25 100000000 100014012 3.40787

Since this runs so quickly, even for a simulation of 1e8, but the results vary randomly, let’s run it multiple times for this many cases to get a sense of how the results might converge. We give a left argument to the timing foreign conjunction `6!:2` to specify how many iterations we should run. In order to see the intermediate results of the multiple timings, we need to explicitly output them using `smoutput`.

(10) 6!:2 'smoutput simChildrenUntilBoy 1e8' 31 100000000 99978672 26 100000000 100011669 27 100000000 99983365 26 100000000 100009676 26 100000000 99982089 28 100000000 100001521 29 100000000 100011019 27 100000000 100014002 27 100000000 100022427 27 100000000 100016146 3.04042

So, we can run a simulation for 100 million families in about three seconds and the result is that we seem to end up with the same number of boys and girls, more or less.

#### A note on summing along the default axis: a performance consideration

Interestingly, my decision to write the summation this way

+/0 1 =/~births

using the tilde to reverse the order of the arguments so the summation could be applied along its default axis, was done with the idea that this would be the more efficient way to do this, as opposed to writing it this way that simply sums across the rank 1 frames:

+/"1 ] 0 1 =/births

However, subsequent comparison of the timings for both versions revealed that the latter runs several times as quickly: 0.99 s/iteration versus 4.6 s/iteration for 1e8 cases.

## Show-and-Tell

We looked at an example of a sophisticated software interface of the type we'd like to be able to emulate in J but which would currently be rather difficult. So, this might serve as a goal for which we can aim with some of our interface-building options in J.

Moving us a little way in this direction, we looked at how to compose simple browser interface code in an online version of APL from Paul Jackson.

## Advanced Topics

We looked at some different ways to spawn and communicate with multiple J processes using a socket interface, one from Joe Bogner and another, originated by Scott Locklin, to work with the ZeroMQ - "an intelligent transport layer for your distributed apps" - bindings, and subsequently modified by Pascal Jasmin. None of these seemed particularly easy to use immediately, perhaps due to the lack of a simple example.

Emphasizing one avenue for achieving a browser interface, we discussed reasons for learning Javascript as an adjunct to J.

## Learning and Teaching J

As an example of the sort of thing we might find useful in a J browser interface, we looked at an example of an application that explains Unix shell commands in detail. Such a facility applied to J could very educational.

We looked at some ideas for improving computing education and discussed how they might be applied to teaching and learning J.

Finally, we considered some likely signs of the Apocalypse: the Waffle Taco and an online tool for students to create Garfield cartoons.

## Materials

*File:EducationTowardComputationalThinking.pdf**File:If you want to code from home - learn Javascript.pdf**File:More Boys or Girls.pdf**File:Some Interface Examples.pdf**File:Spawning J Processes Using Sockets.pdf**File:Using APL to Write Browser Code.pdf**File:ExplainShell-parseAndExplainUnixShellCmds.pdf**File:HorsemenOfTheApocalypse.pdf*

-- Devon McCormick <<DateTime(2014-04-15T00:31:28-0200)>>