Obverse and Under
Tags: j
Published: 2019-09-14 20:59:52

---
title: Obverse and Under
published: true
tags: J
canonical_url: http://www.petecorey.com/blog/2019/09/13/obverse-and-under/
---

I previously wrote about [plotting an “amazing graph”](/blog/2019/08/26/prime-parallelograms/) using [the J programming language](https://www.jsoftware.com/). The solution I landed on looked something like this:

```
require 'plot'
f =: ] - [: #. [: |. #:
'type dot' plot f"0 p: i. 10000
```

Our verb, `f`, is taking a very explicit approach by making judicious use of “capped” ([`[:`](https://www.jsoftware.com/help/dictionary/d502.htm)) verb trains. We’re essentially saying that `f` is ([`=:`](https://www.jsoftware.com/help/dictionary/d001.htm)) the given number ([`]`](https://www.jsoftware.com/help/dictionary/d500.htm)) minus ([`-`](https://www.jsoftware.com/help/dictionary/d120.htm)) the base two ([`#.`](https://www.jsoftware.com/help/dictionary/d401.htm)) of the reverse ([`|.`](https://www.jsoftware.com/help/dictionary/d231.htm)) of the antibase two ([`#:`](https://www.jsoftware.com/help/dictionary/d402.htm)) of the given number.

Several members of the J community pointed out to me that this verb could be simplified with the help of the “under” ([`&.`](https://www.jsoftware.com/help/dictionary/d631.htm)) conjunction. Let’s dig into what “under” is, and how we can use it.

## Under What?

The best way to think about “under” ([`&.`](https://www.jsoftware.com/help/dictionary/d631.htm)), [as explained by the NuVoc page on “under”](https://code.jsoftware.com/wiki/Vocabulary/ampdot), is to think in terms of [domains](https://en.wikipedia.org/wiki/Domain_of_a_function) and transformations in and out of those domains.

> Verb v defines a transformation of the argument(s) (x and) y into the v-domain. Next, verb u operates on the transformed argument(s). Lastly the result is transformed back from the v-domain to the original domain.

In our example, the domain of our input is base ten, but the transformation we want to apply (reversal) needs to happen in the base two domain. “Under” ([`&.`](https://www.jsoftware.com/help/dictionary/d631.htm)) can be used to transform our input into base two ([`#:`](https://www.jsoftware.com/help/dictionary/d402.htm)), apply our reversal ([`|.`](https://www.jsoftware.com/help/dictionary/d231.htm)), and transform the result of that reversal back to our original base ten domain with the obverse, or opposite, of our base two verb, anti base ([`#.`](https://www.jsoftware.com/help/dictionary/d401.htm)):

```
f =: ] - |. &. #:
```

Notice that we’re not explicitly stating how to transform the result of our reversal back into our original domain. J knows that the obverse of [`#:`](https://www.jsoftware.com/help/dictionary/d402.htm) is [`#.`](https://www.jsoftware.com/help/dictionary/d401.htm), and automatically applies it for us.

Out of the box, J comes with many obverse pairings. “Open” ([`>`](https://www.jsoftware.com/help/dictionary/d020.htm)), for example, is the obverse of “box” ([`<`](https://www.jsoftware.com/help/dictionary/d010.htm)), and visa versa. This pairing is especially useful when applying transformations to boxed values:

```
1+&.>1;2;3
┌─┬─┬─┐
│2│3│4│
└─┴─┴─┘
```

Check out a full listing of obverse pairs [at the end of this Shades of J article](https://code.jsoftware.com/wiki/Fifty_Shades_of_J/Chapter_12#Obverse_to_Adverse).

## Inferred Obverses

Even compound verbs built up of verbs with well-defined obverse pairings can be used with “under” ([`&.`](https://www.jsoftware.com/help/dictionary/d631.htm)). J will correctly infer and apply the compound obverse without any intervention or instruction.

For example, if we wanted to unbox a list of values and then work with them in the “square root domain” (whatever that means), we could do something like this:

```
1+&.([:%:>)1;2;3
┌────────────────┐
│4 5.82843 7.4641│
└────────────────┘
```

J takes each value, opens it and finds its square root (`[:%:>`), adds one to the result, and then squares and boxes up (`[:*:<`) the incremented value.

## Explicit Obverses

Even more interestingly, if an obverse pairing isn’t defined or inferable for a given verb, J lets us define our own pairing using the “obverse” ([`:.`](https://www.jsoftware.com/help/dictionary/d311.htm)) verb.

As an example, imagine that we have a JSON string holding an array of values. We want to parse our string, perform some operation on those values, and then serialize the resulting list back into JSON.

We can use the `dec_json` and `enc_json` verbs provided by [the `convert/json` package](https://github.com/jsoftware/convert_json), and tell J that the obverse of `dec_json` is `enc_json`:

```
json =: dec_json :. enc_json
```

Running `dec_json` on a JSON array like `'[1, 2, 3]'` will return a list of boxed numbers, so we’ll want to open each of these boxes, perform our operation, and box the results back up. This sounds like another job for “under” ([`&.`](https://www.jsoftware.com/help/dictionary/d631.htm)):

```
transform =: 1&+&.>
```

All together, we can perform our `transform` “under” ([`&.`](https://www.jsoftware.com/help/dictionary/d631.htm)) the `json` domain:

```
transform &. json '[1, 2, 3]'
[2,3,4]
```

And our result is the JSON string `'[2,3,4]'`!

“Under” is definitely a very powerful conjunction, and I can see myself using it extensively in the future. Thanks to everyone in the J community who was kind enough to point it out and teach me something new!