Skip to content

Instantly share code, notes, and snippets.

@semperos
Last active May 11, 2018 15:06
Show Gist options
  • Save semperos/527669d642adb9043fa8e221e1e0b53d to your computer and use it in GitHub Desktop.
Save semperos/527669d642adb9043fa8e221e1e0b53d to your computer and use it in GitHub Desktop.
APL Bar Chart Example
⍝ Array of values
V←6 4 10 7 15 8
V
6 4 10 7 15 8
⍝ What's the biggest number in there?
⌈/V
15
⍝ Horizontal bar chart has to be that wide
⍳⌈/V
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
RANGE←⍳⌈/V
⍝ Make table of 0s & 1s where each item in V ≥ each of these numbers
V∘.≥RANGE
1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
TABLE←V∘.≥RANGE
⍝ Replace 0s & 1s with shapes to make the bar chart
'.⎕'[(TABLE+1)]
⎕⎕⎕⎕⎕⎕.........
⎕⎕⎕⎕...........
⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕.....
⎕⎕⎕⎕⎕⎕⎕........
⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
⎕⎕⎕⎕⎕⎕⎕⎕.......
⍝ All of above, done according to APL idiom
V←6 4 10 7 15 8
'.⎕'[(V∘.≥⍳⌈/V) + 1]
⎕⎕⎕⎕⎕⎕.........
⎕⎕⎕⎕...........
⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕.....
⎕⎕⎕⎕⎕⎕⎕........
⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
⎕⎕⎕⎕⎕⎕⎕⎕.......
⍝ Or to avoid having to do + 1, we can set indexing to start at 0
⎕io←0
'.⎕'[V∘.≥⍳⌈/V]
⎕⎕⎕⎕⎕⎕⎕........
⎕⎕⎕⎕⎕..........
⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕....
⎕⎕⎕⎕⎕⎕⎕⎕.......
⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
⎕⎕⎕⎕⎕⎕⎕⎕⎕......
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment