-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial: Vectorization
Typically, most programming languages are not vectorized. An operation like +
works on atomic/scalar datatypes:
1 + 1 // works
[1, 2, 3] + 3 // doesn't work
Extending these scalar operations to molecular/vector datatypes requires a loop of some sort:
[1, 2, 3].map((x) => x + 3) // [4, 5, 6]
Conversely, in array programming languages like APL/J/K, operations vectorize by
default. This means that operations like +
automatically scale to the molecular
level:
1 + 1 // works
[1, 2, 3] + 3 // also works
sclin implements a loose variant of vectorization. Whereas in APL/J/K, this would be an error:
1 2 3 + 4 5 / doesn't work
This is valid in sclin:
[1 2 3] [4 5] +
=> [5 7]
Almost all - if not all - of sclin's commands involving scalar datatypes
vectorize. This can include operations on FN
:
[1 2 3] [ 1.+ 2.* ] map
=> [[2 3 4] [2 4 6]]
In fact, since +
and *
both vectorize, the above snippet can also use Q
:
[1 2 3] [ 1.+ 2.* ] Q
=> [[2 3 4] [2 4 6]]
sclin can also vectorize over jagged structures:
[[1 2] 3 4 [[5 7] 8]] [1 2 3 4] +
=> [[2 3] 5 7 [[9 11] 12]]
Made with ❤️ by Ben Pang (@molarmanful).