One of the things I was most looking forward to at Dyalog ’17 was Marinus Oosters' 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"
#.Py
⎕NEW
to create a new instance of the bridge and assign it to a name, in this case `py`:
py←⎕NEW Py.Py
The Basics
You can perform simple operations with it, like evaluate basic math expressions:
py.Eval '15 + 42'
57
a←12
b←3
'⎕/⎕' py.Eval a b
4
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'
a←'abcdef'
b←''
pyperclip.copy ⊂a
+pyperclip.paste ''
abcdef
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:
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 ⍬
uhohIaccidentallyputallofmystringsinonebasket
py.Exec 'import wordsegment'
ws←py.Eval 'wordsegment'
ws.load ⍬
+ws.segment⊂+pyperclip.paste ⍬
┌──┬──┬─┬────────────┬───┬───┬──┬──┬───────┬──┬───┬──────┐
│uh│oh│i│accidentally│put│all│of│my│strings│in│one│basket│
└──┴──┴─┴────────────┴───┴───┴──┴──┴───────┴──┴───┴──────┘
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.
Conclusion
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?
References
https://github.com/marinuso/pynapl
https://www.python.org/
https://pypi.python.org/pypi/pyperclip
https://pypi.python.org/pypi/wordsegment/1.1.6
APL Team Leader
James is an APL Programmer with a keen interest in mathematics. His love for computing was almost an accident. From a young age he always enjoyed using them- playing video games and such- but it was never considered that anything more would come from it. James originally had plans to pursue a career in finance. More about James.
Ask James about APL / APL Consultancy / APL Legacy System Support
- Setting coding priorities and quality bars as project groundwork
- Small Business Network Setup and Cabling
- Nudge Theory – What drums do you dance to?
- Dyalog ’17 – Reflections on Day 1
- APL Full-service Support
- Smart speakers – justified paranoia or am I just behind the times?
- Out of the office…well my usual one at least!