Skip to content
Zach Reedy edited this page Apr 28, 2020 · 3 revisions

GMLodash

v1.0.0

This library is a port of the popular JavaScript library Lodash to GML 2.3. The intent is to be a utility library that enables a functional programming workflow. Not dissimilar to C#'s LINQ, or Python's List Comprehension.

Idiomatic GML 2.3

Several key changes have been made as of GML2.3 that fundamentally change how GML should be written. GMLodash was written with the following changes in mind.

  • Data Structures (ds_*) are not deprecated, however, their usage is discouraged in favor of arrays and structs, as these are natively Garbage Collected.
  • Structs should be used in place of maps.
  • Arrays should be used in place of lists.

Data Structures in GMLodash

For all intents and purposes, GMLodash sees a ds_map and struct, and ds_list and array as the same thing. Types as passed to any function are preserved on the return type, unless explicitly stated otherwise. This is to say that if you pass a ds_* you will be returned an appropriate ds_* type. Passing a struct/array will return an appropriate struct/array.

_.filter([1,2,3,4], function (x) { return x <= 2; }) => [1,2]   // array -> array

var list = ds_list_create();
ds_list_add(1,2,3,4);
_.filter(list, function (x) { return x <= 2; }) => list(1,2)    // list -> list

Data Structure Type Tagging

Please note that in order to support this, GMLodash overrides the GML Runtime's implementations of ds_*_create and ds_exists to append a fractional portion to the index returned by these functions to identify the Data Structure's type. Care should be taken to preserve these indices, otherwise GMLodash will treat these as reals.

// list = 0.0078125
_.filter(list, function (x) { return x <= 2; }) => [1,2]        // real -> real
_.filter(floor(list), function (x) { return x <= 2; }) => 0     // real -> real

Destroying Data Structures returned by GMLodash

Please note that all collections typed as ds_* will mark any element that is also a collection as the appropriate data structure type. The effect of this is that you will only need to ds_*_destroy the returned collection. All children will also be destroyed.

Definitions

  • collection refers to either an array, struct, ds_list, ds_map or string.
  • object refers to either a struct or a ds_map.
  • value refers to any data type supported by GML.

When a function is documented to take a collection and returns either a collection or object, then the returned type will be in parity with the provided collection. That is array to array, ds_map to ds_map. Further information on this can be found in the Data Structure Type Tagging section.

Clone this wiki locally