citizen428.blog()

Try to learn something about everything

More Tinkering With J

Since my previous post on APL and J I’ve read the 45 or so pages of Easy J and am quite impressed by this nifty language. It may seem strange at first, but is very logically structured and not difficult to understand at all. That said I still need to constantly have the vocabulary open to write anything remotely useful, but I’m slowly starting to remember more and more of it.

Here are a couple of examples of J’s expressiveness, with the first two based on exercises from our “Core Ruby” course on RubyLearning.org.

How old is a person if he/she is 1 billion seconds old?

1
2
0 365 24 60 60 #: 1000000000
31 259 1 46 40

Using Antibase (#:) it’s easy to see that the answer is 31 years, 259 days, 1 hour, 46 minutes and 40 seconds.

Convert degress Fahrenheit to degrees Celsius.

1
2
3
toc =: %&1.8@-&32
  toc 100
37.7778

Divide by 1.8 after (Atop, @) subtracting 32. I suspect there’s a better way to write this, but it’s the first thing that came to my mind…

Create a sorted set from a list of numbers.

1
2
3
toset =: ({~/:)@~.
  toset 3 4 1 2 4 2 3 5
1 2 3 4 5

Sort (/:) the distinct items (nub) of the list. Don’t concern yourself too much with the passive (~) for now, it’s just there so the arguments to take end up in the right spot.

That’s it for today, in my next post on J I want to focus on the language’s excellent plotting capabilities.

Comments