{
    "source_code": [
        "' ==========================================================",
        "' ==  A General-Purpose Function Library for jdBasic",
        "' ==  Save this file as \"funclib.jdb\"",
        "' ==========================================================",
        "",
        "' Sorts a 1D array in ascending order.",
        "' It uses the APL-style GRADE function, which returns the indices",
        "' that *would* sort the array. We then use those indices to",
        "' reorder the original array.",
        "FUNC SORT(A)",
        "  RETURN A[GRADE(A)]",
        "ENDFUNC",
        "",
        "' Calculates the Mean (average) of a 1D array.",
        "FUNC MEAN(A)",
        "  ' The [0] is used to get the scalar length from the shape vector",
        "  RETURN SUM(A) / LEN(A)[0]",
        "ENDFUNC",
        "",
        "' Calculates the Median of a 1D array.",
        "FUNC MEDIAN(A)",
        "  SORTED_A = SORT(A)",
        "  N = LEN(SORTED_A)[0]",
        "  MID = INT(N / 2)",
        "  IF N MOD 2 = 0 THEN",
        "    ' Even number of elements, average the middle two",
        "    RETURN (SORTED_A[MID - 1] + SORTED_A[MID]) / 2",
        "  ELSE",
        "    ' Odd number of elements, return the middle one",
        "    RETURN SORTED_A[MID]",
        "  ENDIF",
        "ENDFUNC",
        "",
        "' Calculates the Population Standard Deviation of a 1D array.",
        "FUNC STDDEV(A)",
        "  AVG = MEAN(A)",
        "  VAR = (A - AVG) ^ 2",
        "  RETURN SQR(SUM(VAR) / LEN(A)[0])",
        "ENDFUNC",
        "",
        "' Pads a string on the left with a given character to a total length.",
        "FUNC PAD_LEFT$(original$, total_length, pad_char$)",
        "  padding_needed = total_length - LEN(original$)",
        "  IF padding_needed <= 0 THEN RETURN original$",
        "  RETURN (pad_char$ * padding_needed) + original$",
        "ENDFUNC",
        "",
        "' A simple word-wrap function.",
        "FUNC WORD_WRAP$(text$, max_width)",
        "    words = SPLIT(text$, \" \")",
        "    result$ = \"\"",
        "    current_line$ = \"\"",
        "    FOR i = 0 TO LEN(words)[0] - 1",
        "        word$ = words[i]",
        "        IF LEN(current_line$ + \" \" + word$) > max_width THEN",
        "            result$ = result$ + TRIM$(current_line$) + CHR$(10) ' CHR$(10) is newline",
        "            current_line$ = word$ + \" \"",
        "        ELSE",
        "            current_line$ = current_line$ + word$ + \" \"",
        "        ENDIF",
        "    NEXT i",
        "    result$ = result$ + TRIM$(current_line$)",
        "    RETURN result$",
        "ENDFUNC"
    ],
    "variables": {}
}