Log In  


Utility Functions for PICO-8

Cart #utils_fn_with_tests-0 | 2024-08-22 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Utility Functions for PICO-8 is a collection of essential functions, inspired by JavaScript, that simplifies game development by offering common operations on tables and strings. This library is particularly useful when porting JavaScript code to PICO-8, making the transition smoother and more efficient.

Functions Overview:

  • Iteration Functions:

    • foreach(t, fn): Iterates over each element in a table and applies a function.
    • map(t, fn): Transforms each element in a table and returns a new table.
    • filter(t, fn): Filters elements based on a condition and returns a new table.
    • reduce(t, fn, initial): Reduces a table to a single value using a function.
  • Search and Manipulation Functions:

    • find(t, fn): Finds the first element that satisfies a condition.
    • findindex(t, fn): Finds the index of the first element that satisfies a condition.
    • includes(t, value): Checks if a value exists in a table.
    • concat(t1, t2): Concatenates two tables.
  • Advanced Functions:
    • flat(t, depth): Flattens a nested table up to a specified depth.
    • flatmap(t, fn): Maps each element using a function and flattens the result.
    • sort(t, fn): Sorts elements in a table, optionally using a custom comparison function.
    • deepcopy(orig): Creates a deep copy of a table, preserving nested structures.

Test Functions:

This script includes built-in tests to verify the functionality of each utility function. Run the tests automatically with the run_all_tests() function.

(fake-8 support)
https://github.com/jtothebell/fake-08



I appreciate this contribution, but it's worth noting foreach() and map() are already builtins in PICO-8
https://www.lexaloffle.com/dl/docs/pico-8_manual.html#FOREACH
https://www.lexaloffle.com/dl/docs/pico-8_manual.html#MAP


kozm0naut

thanks for the comment, but I'm doing it mainly for Fake 8, which has difficulty even with vanillia functions coming from pico 8 (especially if there are incompatible updates).

so it's mostly for information purposes that these functions implementaion exists and work in the enviorment fake 8



[Please log in to post a comment]