functional.txt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ***Map
  2. Map applies a function to every element in an array, and stores the result in
  3. another array.
  4. This common code:
  5. [pawn]
  6. new
  7. input[100] = {0, 1, ...},
  8. output[100];
  9. for (new i = 0; i != 100; ++i)
  10. {
  11. output[i] = input[i] * 42;
  12. }
  13. [/pawn]
  14. Can be written as:
  15. [pawn]
  16. new
  17. input[100] = {0, 1, ...},
  18. output[100];
  19. inline const Times42(x) @return x * 42;
  20. Map(using inline Times42, input, output);
  21. [/pawn]
  22. Note: "return" can be using inside inline functions, but will sometimes give
  23. warnings. "@return" can always be used instead.
  24. This can be shrunk even further with the use of the new "lambda" functions:
  25. [pawn]
  26. new
  27. input[100] = {0, 1, ...},
  28. output[100];
  29. Map({_0 * 42}, input, output);
  30. [/pawn]
  31. ***Lambda Functions
  32. Lambda functions are very simple inline functions that can appear inside a
  33. function call, instead of being declared separately. These are a little awkward
  34. to use. For one thing, this works:
  35. [pawn]
  36. new
  37. input[100] = {0, 1, ...},
  38. output[100],
  39. ret = Map(using inline Whatever, input, output);
  40. [/pawn]
  41. This doesn't:
  42. [pawn]
  43. new
  44. input[100] = {0, 1, ...},
  45. output[100],
  46. ret = Map({_0 - 8}, input, output);
  47. [/pawn]
  48. You have to write:
  49. [pawn]
  50. new
  51. input[100] = {0, 1, ...},
  52. output[100],
  53. ret;
  54. Map({_0 - 8}, input, output) => ret;
  55. [/pawn]
  56. I know this is awkward, but I borrowed syntax already established by Slice in
  57. his [url=http://forum.sa-mp.com/showthread.php?t=343172][u]multidimensional sort include[/u][/url].
  58. The parameters have names "_0", "_1", "_2" etc. "Map" takes a function that
  59. requires one parameter, so you can only use "_0" in its lambda. "ZipWith3" on
  60. the other hand takes an inline function with 3 parameters, so you can use "_1"
  61. and "_2" as well.
  62. An example of a "Sum" function would be:
  63. [pawn]
  64. Sum(arr[], len = sizeof (arr))
  65. {
  66. FoldR({_0 + _1}, arr, 0, len) => return;
  67. }
  68. [/pawn]
  69. Using "return" instead of a variable is acceptable, not much else is though.
  70. Functions (look them up somewhere):
  71. Map - See above.
  72. Map_ - Doesn't save the results, useful for just printing all the values.
  73. IdxMap - Like map, but with an extra "index" parameter first in the inline.
  74. IdxMap_ - To IdxMap what Map_ is to Map.
  75. ZipWith - Combine two arrays using a function.
  76. ZipWith_ - Don't store the results (Map_ with 2 function parameters).
  77. ZipWith3 - ZipWith could be called "ZipWith2".
  78. ZipWith3_ - 3 parameter Map_.
  79. FoldL - Left fold.
  80. FoldL1 - Left fold with no default value.
  81. FoldR - Right fold.
  82. Foldr1 - Right fold with no default value.
  83. ScanL - Left fold, saving interim results.
  84. ScanR - Right fold, saving interim results.
  85. And - Returns true if every array item is true.
  86. Or - Returns true if any array item is true.
  87. All - Returns true if every array item passes a single parameter test.
  88. Any - Returns true if any array item passes a single parameter test.
  89. Reverse - Reverse the items in an array.
  90. Elem - Returns true if "n" is in an array.
  91. NotElem - ...