fear and loathing with apl (oredev)

101
Fear and Loathing with APL

Upload: yan-cui

Post on 18-Jan-2017

860 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Fear and loathing with APL (oredev)

Fear and Loathing with APL

Page 2: Fear and loathing with APL (oredev)

Hi, my name is Yan Cuiaka @theburningmonk

Page 3: Fear and loathing with APL (oredev)

What is APL?

created at IBM in 1957, by Ken Iverson

designed to simplify math notations

“A Programming Language”

Page 4: Fear and loathing with APL (oredev)

What is APL?

famed for use of special symbols

array programming

influenced Mathematica, Matlab, etc.

Page 5: Fear and loathing with APL (oredev)

hey, I’m learning

APL

Page 6: Fear and loathing with APL (oredev)

what the hell is an

APL?

Page 7: Fear and loathing with APL (oredev)

have you lost your

mind?

Page 8: Fear and loathing with APL (oredev)

Conway’s Game of Life

https://www.youtube.com/watch?v=a9xAKttWgP4

Page 9: Fear and loathing with APL (oredev)
Page 10: Fear and loathing with APL (oredev)

“Beauty is in the Eye of the Beholder”

beauty doesn't exist on its own but is created by observers

Page 11: Fear and loathing with APL (oredev)

“…We followed up with two studies on the accuracy rates of novices using a total of six programming languages: Ruby, Java, Perl, Python, Randomo, and Quorum. Randomo was designed by randomly choosing some keywords from the ASCII table (a metaphorical placebo). To our surprise, we found that languages using a more traditional C-style syntax (both Perl and Java) did not afford accuracy rates significantly higher than a language with randomly generated keywords, …”

“An Empirical Investigation into Programming Language Syntax”- Andreas Stefik and Susanna Siebert

Page 12: Fear and loathing with APL (oredev)

“…We followed up with two studies on the accuracy rates of novices using a total of six programming languages: Ruby, Java, Perl, Python, Randomo, and Quorum. Randomo was designed by randomly choosing some keywords from the ASCII table (a metaphorical placebo). To our surprise, we found that languages using a more traditional C-style syntax (both Perl and Java) did not afford accuracy rates significantly higher than a language with randomly generated keywords, …”

“An Empirical Investigation into Programming Language Syntax”- Andreas Stefik and Susanna Siebert

Page 13: Fear and loathing with APL (oredev)

http://bit.ly/2eM3Qvr

Page 14: Fear and loathing with APL (oredev)

“A language that doesn't affect the way you think

about programming, is not worth knowing.”

- Alan Perlis

Page 15: Fear and loathing with APL (oredev)

“in an era of mainstream programming languages, APL is a rare opportunity to truly challenge the way you think about

programming”

- me

Page 16: Fear and loathing with APL (oredev)
Page 17: Fear and loathing with APL (oredev)
Page 18: Fear and loathing with APL (oredev)

Let’s learn some APL

http://tryapl.org/

Page 19: Fear and loathing with APL (oredev)

2 + 2 4

4 - 1 3

+ and - works the way you expect

Page 20: Fear and loathing with APL (oredev)

2 * 3 8

4 * 4 256

but * means power

Page 21: Fear and loathing with APL (oredev)

2 / 3 3 3

and / means replicate

it means replicate the scalar value 3, 2 timesand returns an array of 2 elements

Page 22: Fear and loathing with APL (oredev)

2 × 2 4 4 ÷ 2 2

multiplication (×) and division (÷)

just like in Maths!

Page 23: Fear and loathing with APL (oredev)

x ← 42 y ← 1 2 3 4

assignment (←)

variable names are case sensitive

Page 24: Fear and loathing with APL (oredev)

variables are mutable

x ← 42 x ← 1 2 3 4 x

1 2 3 4

Page 25: Fear and loathing with APL (oredev)

assign multiple values at once(kinda like pattern matching)

x y z ← 1 2 3 x

1 y

2

Page 26: Fear and loathing with APL (oredev)

but the patterns must match

x y ← 1 2 3 LENGTH ERROR x y ← 1 2 3

Page 27: Fear and loathing with APL (oredev)

1 2 3 - 3 2 1 ¯2 0 2

1 2 3 × 3 2 1 3 4 3

arithmetic operations work on arrays too

Page 28: Fear and loathing with APL (oredev)

1 2 3 + 1 2 3 4

2 × 1 2 3 2 4 6

if one side is scalar then the other side can be any shape

Page 29: Fear and loathing with APL (oredev)

4 ⌊ 2 2 4 ⌈ 2 4

min (⌊) and max (⌈)

Page 30: Fear and loathing with APL (oredev)

1 2 3 4 5 ⌈ 4 4 4 4 4 5 1 2 3 ⌈ 3 2 1 3 2 3

min (⌊) and max (⌈)

same rules apply for arrays

Page 31: Fear and loathing with APL (oredev)
Page 32: Fear and loathing with APL (oredev)
Page 33: Fear and loathing with APL (oredev)

dyadic

2 ⌈ 4 1 2 ⌈ 3 1

monadic

⌈ 2 ⌈ 1 3

Page 34: Fear and loathing with APL (oredev)

⌊ 3 4.1 4.5 5.6 3 4 4 5

monadically, ⌊ means floor

Page 35: Fear and loathing with APL (oredev)

⌈ 3 4.1 4.5 5.6 3 5 5 6

and ⌈ means ceiling

Page 36: Fear and loathing with APL (oredev)
Page 37: Fear and loathing with APL (oredev)

3 2 ⍴ 1 2 3 4 5 6 1 2 3 4 5 6

dyadically, ⍴ reshapes right hand side

rows

columns

Page 38: Fear and loathing with APL (oredev)

⍴ 1 2 3 3 ⍴ (3 2 ⍴ 1 2 3 4 5 6)

3 2

monadically, ⍴ returns shape of argument

Page 39: Fear and loathing with APL (oredev)

3 2 ⍴ 1 2 3 1 2 3 1 2 3

⍴ recycles elements to fill space

Page 40: Fear and loathing with APL (oredev)

2 2 ⍴ 1 2 3 4 5 6 1 2 3 4

and ignores additional elements

Page 41: Fear and loathing with APL (oredev)

3 3 ⍴ 1 0 0 0 1 0 0 0 1 0 0 0 1

here’s a neat trick to get diagonals :-P

Page 42: Fear and loathing with APL (oredev)

⍴ 2 2 ⍴ 0 1 0 2 2 ⍴ (2 2 ⍴ 0 1 0)

2 2

oh, and APL is read from RIGHT-TO-LEFT

Page 43: Fear and loathing with APL (oredev)

⍴ 2 2 ⍴ 0 1 0 2 2 ⍴ (2 2 ⍴ 0 1 0)

2 2

oh, and APL is read from RIGHT-TO-LEFT

Page 44: Fear and loathing with APL (oredev)

+/ 1 2 3 6

btw, / also means reduce

ie, reduce over the array [1, 2, 3] with the plus function

Page 45: Fear and loathing with APL (oredev)

×/ 1 2 3 4 5 120

btw, / also means reduce

and here’s factorials!

Page 46: Fear and loathing with APL (oredev)

Defined Functions

Page 47: Fear and loathing with APL (oredev)

Avg ← { (+/ ⍵) ÷ ⍴⍵ }

anatomy of a defined functionfunction name

assignment function body

Page 48: Fear and loathing with APL (oredev)

Avg ← { (+/ ⍵) ÷ ⍴⍵ }

Avg 1 2 3

anatomy of a defined function

omega ⍵ represents the right argument

Page 49: Fear and loathing with APL (oredev)

Diff ← { (+/ ⍺) - (+/ ⍵) }

3 4 5 Diff 1 2 3

anatomy of a defined function

alpha ⍺ represents the left argument

Page 50: Fear and loathing with APL (oredev)

Plus ← { ⍺ + ⍵ } Plus/ 1 2 3

6

defined functions can be used with / too

Page 51: Fear and loathing with APL (oredev)

Indexing an Array

Page 52: Fear and loathing with APL (oredev)

Xs ← 1 2 3 Xs[2]

2

APL is one-indexed!

Page 53: Fear and loathing with APL (oredev)

Xs[1 2 1] 1 2 1

Xs[2 2 ⍴ 1 2] 1 2 1 2

indices can be array too

Page 54: Fear and loathing with APL (oredev)

Xs[3 2] ← 6 4 Xs

1 4 6

updating an array

Page 55: Fear and loathing with APL (oredev)
Page 56: Fear and loathing with APL (oredev)

⍳9 1 2 3 4 5 6 7 8 9

index generator

Page 57: Fear and loathing with APL (oredev)

Booleans

Page 58: Fear and loathing with APL (oredev)

1 < 2 1

1 2 3 < 3 2 1 1 0 0

1 is true, 0 is false

yup, works with arrays too ;-)

Page 59: Fear and loathing with APL (oredev)

~1 0

~1 0 1 0 1 0

monadically, ~ means negate

Page 60: Fear and loathing with APL (oredev)

(1 2 3 < 3 2 1) ∧ (1 1 1) 1 0 0

(1 2 3 < 3 2 1) ∨ (1 1 1) 1 1 1

logical AND ∧ and logical OR ∨

Page 61: Fear and loathing with APL (oredev)

(⍳10) > 7 0 0 0 0 0 0 0 1 1 1

+/ (⍳10) > 7 3

count no. of positives

Page 62: Fear and loathing with APL (oredev)

(⍳10) > 7 0 0 0 0 0 0 0 1 1 1 ⍳10 > 7

?

are these two expression equivalent?

Page 63: Fear and loathing with APL (oredev)

(⍳10) > 7 0 0 0 0 0 0 0 1 1 1 ⍳10 > 7

1

are these two expression equivalent?

nope! remember, APL reads from RIGHT-TO-LEFT!

Page 64: Fear and loathing with APL (oredev)

(⍳10) > 7 0 0 0 0 0 0 0 1 1 1

7 < ⍳10 0 0 0 0 0 0 0 1 1 1

are these two expression equivalent?

Page 65: Fear and loathing with APL (oredev)

Compression (aka filtering)

Page 66: Fear and loathing with APL (oredev)

1 0 1 / 3 2 1 3 1

1 0 1 / 'iou' i u

booleans can be used to as a mask

Page 67: Fear and loathing with APL (oredev)

Values ← 1 3 5 7 9 Values > 4

0 0 1 1 1 (Values > 4) / Values

5 7 9

a LINQ-style Where can be written as

Page 68: Fear and loathing with APL (oredev)

Values > 4 0 0 1 1 1 ⍳⍴Values

1 2 3 4 5 (Values > 4) / (⍳⍴Values)

3 4 5

and to get the indices instead

Page 69: Fear and loathing with APL (oredev)

1 2 3 ~ 3 4 3 5 1 2

‘ab’ ‘cd’ ‘ef’ ~ ‘cd’ ‘ef’ ab

dyadically, ~ means without

Page 70: Fear and loathing with APL (oredev)
Page 71: Fear and loathing with APL (oredev)

1 1 3 5 3 7 ∊ 1 2 3 1 1 1 0 1 0

'hello world' ∊ 'aeiou' 0 1 0 0 1 0 0 1 0 0 0

dyadically, ∊ checks for membership

Page 72: Fear and loathing with APL (oredev)

Phrase ← ‘hello world’ (Phrase ∊ 'aeiou') / Phrase

eoo

which vowels appeared in a phrase?

Page 73: Fear and loathing with APL (oredev)

2 2 ⍴ 1 2 1 2 1 2

∊ 2 2 ⍴ 1 2 1 2 1 2

monadically, ∊ flattens a matrix

Page 74: Fear and loathing with APL (oredev)

Take & Drop

Page 75: Fear and loathing with APL (oredev)

2 ↑ 1 2 3 4 1 2

2 2 ↑ (3 3 ⍴ ⍳9) 1 2 4 5

↑ to take items from an array

!?

Page 76: Fear and loathing with APL (oredev)

3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9

Page 77: Fear and loathing with APL (oredev)

2 2 ↑ 3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9

take a 2 (rows) by 2 (cols) matrix

Page 78: Fear and loathing with APL (oredev)

2 ↓ 1 2 3 4 3 4

2 2 ↓ (3 3 ⍴ ⍳9) 9

↓ to drop items from an array

!?

Page 79: Fear and loathing with APL (oredev)

3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9

Page 80: Fear and loathing with APL (oredev)

2 2 ↓ 3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9

drop first 2 rows and 2 cols

Page 81: Fear and loathing with APL (oredev)

¯2 ↑ 1 2 3 4 3 4

¯2 ↓ 1 2 3 4 1 2

use negative count to take/drop from end of array

Page 82: Fear and loathing with APL (oredev)

¯2 2 ↑ (3 3 ⍴ ⍳9) ?

¯2 2 ↓ (3 3 ⍴ ⍳9) ?

what about these?

http://tryapl.org/ try it out yourself

Page 83: Fear and loathing with APL (oredev)

Mirrors & Transportations

Page 84: Fear and loathing with APL (oredev)

3 3 ⍴ ⍳9 1 2 3 4 5 6 7 8 9

given a matrix

Page 85: Fear and loathing with APL (oredev)

3 3 ⍴ ⍳9 3 2 1 6 5 4 9 8 7

mirrors the matrix horizontally

Page 86: Fear and loathing with APL (oredev)

3 3 ⍴ ⍳9 7 8 9 4 5 6 1 2 3

mirrors the matrix vertically

Page 87: Fear and loathing with APL (oredev)

⍉ 3 3 ⍴ ⍳9 1 4 7 2 5 8 3 6 9

transposes the matrix⍉

Page 88: Fear and loathing with APL (oredev)

Outer Products

Page 89: Fear and loathing with APL (oredev)

cartesian product

Page 90: Fear and loathing with APL (oredev)

1 2 3 .+ 4 5 6 5 6 7 6 7 8 7 8 9

. creates a cartesian product

Page 91: Fear and loathing with APL (oredev)

1 2 3 .× 4 5 6 4 5 6 8 10 12 12 15 18

. creates a cartesian product

Page 92: Fear and loathing with APL (oredev)

(⍳3) .= (⍳3) 1 0 0 0 1 0 0 0 1

. creates a cartesian product

Page 93: Fear and loathing with APL (oredev)

(⍳3) .< (⍳3) 0 1 1 0 0 1 0 0 0

. creates a cartesian product

Page 94: Fear and loathing with APL (oredev)

(⍳3) .⌈ (⍳3) 1 2 3 2 2 3 3 3 3

. creates a cartesian product

Page 95: Fear and loathing with APL (oredev)

(⍳3) .+ (⍳3) 2 3 4 3 4 5 4 5 6

. creates a cartesian product

Page 96: Fear and loathing with APL (oredev)

what about this?

http://tryapl.org/ try it out yourself

∊ (⍳3) .× (⍳5) ?

Page 97: Fear and loathing with APL (oredev)

Demo Time…

Page 98: Fear and loathing with APL (oredev)

Conclusions

Page 99: Fear and loathing with APL (oredev)

should I learn APL?

absolutely! it’s the shock to the system your brain needs

Page 100: Fear and loathing with APL (oredev)

is it gonna help my career?

does it matter?

Page 101: Fear and loathing with APL (oredev)

@theburningmonktheburningmonk.comgithub.com/theburningmonk