Bridging the gap between APL and Python

James HeslipAPL, Dyalog0 Comments

One of the things I was most looking forward to at Dyalog ’17 was Marinus Ooster’s presentation of Py’n’APL. I caught a brief glimpse of his work on a webinar beforehand, and it was enough to inspire me to start learning Python in my spare time. Py’n’APL is a bridge between Python and APL; It allows you to execute Python expressions in APL, and vice versa. This is extremely powerful, as it allows you access to Python’s vast expanse of code libraries without the need to learn Python.

The files necessary to use the Py’n’APL library can be found on his GitHub profile

To initialise a Python instance in APL, you first have to load in `Py.dyalog`. This will be located in the folder you clone from the Github repository.

      ]load "C:\Users\jamesh\Desktop\pynapl-master\pynapl-master\pynapl\Py.dyalog"

You can then use ⎕NEW to create a new instance of the bridge and assign it to a name, in this case `py`:

py←⎕NEW Py.Py


You can perform simple operations with it, like evaluate basic math expressions:

     py.Eval '15 + 42'

Or perform math expressions using parameters declared in APL:

      '⎕/⎕' py.Eval a b

You can perform all of the typical programming operations, for example: string manipulation, logical comparisons, etc, but that’s no fun since you can already do this in native APL. (Disclaimer, it’s possible to perform everything in this article using just APL, but it’s a damn sight harder and often there are perfectly good libraries for the task ).

Working with Libraries

Pyperclip is my favourite Python library; it allows you to interact with the clipboard, copying and pasting items. We can import it to our Python session:

py.Exec 'import pyperclip'

And evaluate the library to gain access to its methods in APL.

pyperclip←py.Eval 'pyperclip'
pyperclip.copy ⊂a
+pyperclip.paste ''
b←+pyperclip.paste ''

In a very roundabout way we’ve done b←a. So what’s the point? Why bother if it’s just adding a layer of complexity? Well we don’t just have to use Pyperclip to copy and paste values between variables. Behold:

Practical Example

	strv←'uh' 'oh' 'I' 'accidentally' 'put' 'all' 'of' 'my' 'strings' 	'in' 'one' 'basket'
	pyperclip.copy ⊂∊strv  
	⍝ notice I “accidentally” flattened” the vector before copying, this 	is simply for an example
	+pyperclip.paste ⍬
	py.Exec 'import wordsegment'
	ws←py.Eval 'wordsegment'
	ws.load ⍬
	+ws.segment⊂+pyperclip.paste ⍬

Here is an example: say you’ve received a flattened string from an incompetent colleague, who just so happens to also be out of the office that day. You need to process that work and send it off to your boss or you’ll both be fired. Instead of whining about how much you hate your colleague, you come up with a smart way to segment the words, and get on with your life.
In this situation the quantity of defects is not significant, and doing it by hand is probably easier, but in real life it could be, and I for one don’t want to have to segment the works of Shakespeare.


Python has a lot of cool functionality, and if you can think of a problem, there’s probably a Python library to solve it. Combining this with the good parts of APL (matrix operations, powerful primitives, terse expressions), you can very easily cut down the amount of time you spend working on a problem. Why reinvent the wheel?


Share this Post

About the Author
James Heslip

James Heslip

James is the newest member of our development team. He is a keen mathematician and enjoys playing video games in his spare time.