**You can become a serverless blackbelt**. Enrol to my 4-week online workshop **Production-Ready Serverless** and **gain hands-on experience** building something from scratch using serverless technologies. At the end of the workshop, you should have a broader view of the challenges you will face as your serverless architecture matures and expands. You should also have a firm grasp on when serverless is a good fit for your system as well as common pitfalls you need to avoid. Sign up now and get **15% discount** with the code yanprs15!

Having heard so much about APL from Phil Trelford (who is one of the leaders in the F# community) I decided to try it out too. For the uninitiated, APL code looks every bit as mind-bending and unbelievable as scenes from Johnny Depp’s *Fear and Loathing in L**as Vegas*.

For example, the **life** function below implements Conway’s game of life:

I know, right? WTF and mind blowing in equal measures…

As I was learning through Mastering Dyalog APL, I took down plenty of notes which I hope will help get you up to speed with the basics quickly.

You can try out the examples at TryAPL, just click on the “APL Keyboard” button and use the virtual keyboard to enter the symbols you need.

## Learn APL in 10 Minutes

and does what you’d expect but multiplication and division becomes and (just like in maths).

in APL is power, i.e.

/ in APL also means something else, which we’ll come to later.

Also note the difference between negative sign and subtraction , e.g.

Use to assign a value (either scalar or array) to a name, e.g.

and remember, variable names are **case sensitive**.

Variables are **mutable**, and you can assign multiple variable at once:

but only if the length match, otherwise you’ll get an error

=>

You can perform arithmetic operations between two arrays with the same shape:

=>

=>

look ma, no loops

If the shape doesn’t match, then you get an error:

=>

But, if one of the variables is a scalar then the other variable can be any shape:

=>

=>

The same rule applies to other operations, e.g. max and min .

=>

=>

=>

Most symbols in APL have double meaning, just like they do in algebra, e.g.

where – means subtraction, a **dyadic** use of the symbol

where – means negative, a **monadic** use of the same symbol

(before you freak out, *monadic* here is of the Mathematics definition, and not the Haskell variant )

To find the length of an array, you can use Rho monadically:

=> 5

You can also Rho dyadically to organize items into specified shapes, i.e.

=>

where 4 2 describes the shape – 4 rows, 2 columns, followed by Rho and the array of items to be organized.

If the number of items in the array doesn’t match the number of slots in the shape we’re trying to organize into, then the array is repeated, e.g.

=>

=>

If there are too many items then extra items are ignored:

=>

Utilizing the way items are repeated, there’s a neat trick you can do, e.g.

=>

In general, APL has special names for data depending on its shape:

**scalar** – single value

**vector** – list of values

**matrix** – array with 2 dimensions

**array** – generic term for any set of values, regardless of no. of dimensions

**table** – common term for matrix

**cube** – common term for array with 3 dimensions

Ok, now that we’ve introduced the notion of dimensions, it’s time to confess that I slightly misled you earlier – Rho actually returns the **shape** of an array (not just its length, which only applies to a vector), so it works with multi-dimensional data too.

=> 2 2

Furthermore, since the result of Rho is itself a vector, applying Rho again will give us the number of dimensions an array has – otherwise known as its **Rank**.

=> 2

i.e. scalars have 0 rank, vectors have 1 rank, and so on…

You can also reduce over a set of values using / e.g. a plus reduction to sum up all the numbers in an array can be written as

=>

similarly, factorial can be written using multiply reduction:

=>

The / symbol is an operator, whereas etc. are functions. / can accept any of these functions and uses it to reduce over an array.

=> 9

=> 1

APL calls programs **defined functions**, and you can create a defined function like this:

=> 3

where:

– program name

– generic symbol for array passed on the right

– generic symbol for array passed on the left

for example, if we have a function such that

=> 16

what we’ve done here is to sum the array on the right of – 4 5 6 7 – and subtract it with the sum of the array on the left – 1 2 3.

Simple, right? Let’s try a few more.

=> 63

and you can use your custom functions with reduction:

=> 15

To index into an array, you can use the standard [ ] notation:

=> 4

noticed that? APL is **one-indexed**!

What’s more, just like everything else, you can index into array with either a scalar or an array:

=> 1 3 4

=> 1 3 4 1

This also works when it comes to updating values in an array:

=> 42 2 42 4 42

=> 1 2 3 4 5

=>

Note that it follows the same rule as functions with regards to the input being either a scalar or a same-shaped array.

If the shape of the array doesn’t match, then it’ll error:

=>

Equally, if you use an invalid index, you’ll also get an error:

=>

You can use Iota to generate an array of integers from 1 to N, e.g.

=> 1 2 3 4

=> 1 2 3 4

Booleans are represented as 1 and 0, and you can use any one of these relational functions: < = =/ >

=> 0 1 1

=> 1 0 0

AND and OR semantics are represented by and respectively.

=> 1 0 1

=> 0 1 1

you can use them to count no. of employees based on salary for instance:

=> 2

You can also use an array of boolean values as masks too:

=> 1 3

=> iu

this is called **compression**, and is useful for selecting items conforming to some criteria, e.g.

=> 1 1 0 0 0 1 0

=> 7

=> 1 2 3 4 5 6 7

therefore, to find the indices of values that’s greater than 10

=> 1 2 6

and to get the corresponding values, just use compression:

=> 11 13 15

Given two arrays, A and B, you can return a new array with all the elements of A minus all the elements of B using the **without** operator ~

=> 1 2

this **doesn’t modify** A or B though.

=> 1 2 3

=> 3 4 5

**memberships**

To find items from an array that exists in another array, you can use the membership operator

=> 1 0 1 0 1

it also works with strings

=> 0 1 0 1 0 1 0 1 0 0 1 0 0 1 0 1 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 1 0 1 0

you can (like before) get the indices of matches using compression:

=> 2 4 6 8 11 14 16 20 23 24 30 33 36 41 43 45

or find the matching characters using compression:

=> aaaiaaaeeeaiaaii

inspecting the result tells us that those are indeed the characters we’re looking for!

**search**

We saw how we can combine memberships and iota-rho to find the indices in Phrase that corresponds to a member in ‘aeiouy’.

What if we want to find the first index each character in ‘aeiouy’ appears in Phrase, i.e. *“aeiouy”.Select(char -> Phrase.IndexOf(char))* if you’re coming from a .Net background.

We can use the dyadic form of Iota :

=> 2 20 8 47 47 47

note that the length of Phrase is 46

=> 46

so the result 47 is essentially saying that ‘o’, ‘u’, ‘y’ never appeared in Phrase.

You can model an ‘else’ semantic using two arrays whose lengths are off by 1. For instance:

based on the way search works (not found = length + 1), we can map the specified Area codes to corresponding discount rates: 17 -> 9, 50 -> 8, … 89 -> 4, and all others to 2.

=> 6 6 5 6 6 2 6 2 4 6 1 5

so where D does not exist in Area, you’ll get index 6 which does not exist in Area, BUT exists in Discount – i.e. the ‘other’ case we’re trying to model.

So if we use the result of as indices in Discount we’ll find the corresponding discount rate for the values in D:

=> 2 2 4 2 2 8 2 8 5 2 9 4

this is an algorithm for **changing the frame of reference**, i.e. changing a list of area codes into a list of discount values, a general form can be described as:

where = + 1

**take and drop**

You can use the Take and Drop functions like you do with Enumerable.Take and Enumerable.Drop in LINQ:

=> 1 2 3 4

=> 5 6 7 8 9 10

if the count is negative then you take from the end of the list, e.g. take last 3 items or drop last 7 items:

=> 8 9 10

=> 1 2 3

Given a list of values

suppose you want to find the change from one number to the next, i.e. 3 *(+5)* 8 *(-3)* 5 *(+9)* 14…

considering that

=> 8 5 14 34 5 17 21 18

=> 3 8 5 14 34 5 17 21

so now you can subtract the two arrays (of equal length) to get the answer:

=>

clever, right? It’s reminiscent of the RX approach to tracking mouse moves.

**mirrors and transposition**

You can also pivot data about any direction easily:

(screenshot taken from Mastering Dyalog APL)

**outer products**

You can use the outer product operator to calculate the cartesian products of two lists:

=>

using the small circle + dot + an operation to apply, in this case multiply , but could easily be anything else:

=>

here’s even more examples taken from Mastering Dyalog APL:

Unlike some of the operations we’ve seen so far, the outer product operator works across different sized arrays too:

=>

Finally, there are some common patterns (or idioms) you’ll see in APL.

- find indices in A that exists in B

- find the members (not indices) of A that exists in B

- find indices in A where B first appeared in (i.e.
*IndexOf*)

- change the frame of reference

- generate indices with incrementing steps

So that’s it, I hope this post has given you a flavour of APL and help you learn the basics quickly. I find the array programming paradigm absolutely mind blowing and a completely different way to think about problems.

Also I find it has a similar transformative effect in changing the way I think about variables – as a point-in-time snapshot of some scalar value – to FRP (e.g. signals in Elm or observables in RX).

In the next post, let’s see how we can use APL to solve some simple problems (that’s all I’m able to manage for now!).

## Links

Hi, I’m **Yan**. I’m an **AWS Serverless Hero** and I help companies go faster for less by adopting serverless technologies successfully.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

### Skill up your serverless game with this hands-on workshop.

My 4-week Production-Ready Serverless online workshop is back!

This course takes you through building a production-ready serverless web application from testing, deployment, security, all the way through to observability. The motivation for this course is to give you **hands-on** experience building something with serverless technologies while giving you a broader view of the challenges you will face as the architecture matures and expands.

We will start at the basics and give you a firm introduction to Lambda and all the relevant concepts and service features (including the latest announcements in 2020). And then gradually ramping up and cover a wide array of topics such as API security, testing strategies, CI/CD, secret management, and operational best practices for monitoring and troubleshooting.

If you **enrol now** you can also get **15% OFF** with the promo code “yanprs15”.

Check out my new podcast **Real-World Serverless** where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.

Check out my new course, **Learn you some Lambda best practice for great good!** In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. **Enrol now and start learning**!

Check out my video course, **Complete Guide to AWS Step Functions**. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. **Enrol now and start learning**!

Further reading

Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.

- All you need to know about caching for serverless applications
- Choreography vs Orchestration in the land of serverless
- Are Lambda-to-Lambda calls really so bad?
- Lambda optimization tip – enable HTTP keep-alive
- You are wrong about serverless and vendor lock-in
- You are thinking about serverless costs all wrong
- Check-list for going live with API Gateway and Lambda
- How to choose the right API Gateway auth method
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- Guys, we’re doing pagination wrong
- Top 10 Serverless framework best practices
- I left full-time employment, here’s what happened since
- How to break the “senior engineer” career ceiling
- My advice to junior developers