|
2 | 2 | ## Conversion of JavaScript's Array methods to AutoHotkey
|
3 | 3 |
|
4 | 4 |
|
5 |
| -AHK supports functors but no built in functions for functional style operations. Funky. JavaScript's Array object has some nice methods; so I stole them. |
| 5 | +AHK supports [objects](https://autohotkey.com/docs/objects/Object.htm), [arrays](https://autohotkey.com/docs/Objects.htm#Table_of_Contents), and [function objects](https://autohotkey.com/docs/objects/Functor.htm). But it lacks built in methods for functional operation on arrays, like `Array.map()`. JavaScript's Array object has some handy methods; so I ported them over. |
6 | 6 |
|
7 |
| -Since AHK is also prototypical in nature, these could have been added to a base object. But that didn't happen for simplicities sake, they are all prefixed with `array_`. |
| 7 | +Since AHK is also prototypical in nature, these could have been added to a base object. For simplicity's sake these are global functions. All methods are prefixed with `array_`. |
| 8 | + |
| 9 | +### Usage |
| 10 | + |
| 11 | +Just include the `array_.ahk` file and pass functors as arguments when needed. For instance, to double all values in an array: |
| 12 | + |
| 13 | + array := [1,2,3,4] |
| 14 | + result := array_map(array, func("double")) |
| 15 | + double(item) { |
| 16 | + return item * 2 |
| 17 | + } |
| 18 | + msgbox % array_toString(result) ;outputs "[2,4,6,8]" |
| 19 | + |
| 20 | +or getting fancy with a partial: |
| 21 | + |
| 22 | + array := [12,3,44,9] |
| 23 | + result := array_map(array, func("multiply").bind(2)) |
| 24 | + multiply(a, b) { |
| 25 | + return a * b |
| 26 | + } |
| 27 | + msgbox % array_toString(result) ;outputs "[24,6,88,18]" |
| 28 | + |
8 | 29 |
|
9 | 30 | ### Stolen Methods
|
10 | 31 |
|
@@ -34,7 +55,7 @@ Since AHK is also prototypical in nature, these could have been added to a base
|
34 | 55 | Considered implementing a `array_sort` quicksort; but currently no immediate need.
|
35 | 56 |
|
36 | 57 | ### Tests
|
37 |
| -Nothing is truely complete without testing, so each array_<method> has a test case file in the `tests/` directory. |
| 58 | +Nothing is truely complete without testing, so each `array_<method>` has a test case file in the `tests/` directory. |
38 | 59 |
|
39 | 60 | Built a few primitive [test classes](test_suite/tester.ahk) to collect test cases. Management of this test class is located in [testrunner.ahk](testrunner.ahk). Results are output to an edit control in a new window.
|
40 | 61 |
|
|
0 commit comments