' Array Filters — filter/map/reduce operations on arrays
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    DIM data(1 TO 10) AS LONG
    DIM result(1 TO 10) AS LONG
    DIM names(1 TO 5) AS STRING
    DIM scores(1 TO 5) AS LONG
    LOCAL n AS LONG
    LOCAL resultLen AS LONG
    LOCAL i AS LONG
    LOCAL sum AS LONG
    LOCAL product AS LONG
    LOCAL minVal AS LONG
    LOCAL maxVal AS LONG
    LOCAL mean AS DOUBLE
    LOCAL variance AS DOUBLE
    LOCAL sumsq AS DOUBLE
    LOCAL csum AS LONG
    LOCAL line AS STRING

    PRINT "=========================="
    PRINT " Array Filters & Reducers"
    PRINT "=========================="
    PRINT

    n = 10
    data(1) = 3
    data(2) = 8
    data(3) = 1
    data(4) = 6
    data(5) = 12
    data(6) = 4
    data(7) = 9
    data(8) = 2
    data(9) = 7
    data(10) = 5

    PRINT "=== FILTER-MAP-REDUCE CHAIN ==="
    PRINT "Original:"
    line = ""
    FOR i = 1 TO n
        line = line & STR$(data(i)) & " "
    NEXT i
    PRINT line

    resultLen = 0
    FOR i = 1 TO n
        IF data(i) MOD 2 = 0 THEN
            resultLen = resultLen + 1
            result(resultLen) = data(i)
        END IF
    NEXT i
    PRINT "Filtered (even only):"
    line = ""
    FOR i = 1 TO resultLen
        line = line & STR$(result(i)) & " "
    NEXT i
    PRINT line

    FOR i = 1 TO resultLen
        data(i) = result(i)
    NEXT i
    n = resultLen

    FOR i = 1 TO n
        result(i) = data(i) * data(i)
    NEXT i
    PRINT "Mapped (squared):"
    line = ""
    FOR i = 1 TO n
        line = line & STR$(result(i)) & " "
    NEXT i
    PRINT line

    sum = 0
    FOR i = 1 TO n
        sum = sum + result(i)
    NEXT i
    PRINT "Reduced (sum): " & STR$(sum)

    minVal = result(1)
    maxVal = result(1)
    sum = 0
    FOR i = 1 TO n
        IF result(i) < minVal THEN minVal = result(i)
        IF result(i) > maxVal THEN maxVal = result(i)
        sum = sum + result(i)
    NEXT i
    mean = sum / n
    sumsq = 0.0
    FOR i = 1 TO n
        sumsq = sumsq + (result(i) - mean) * (result(i) - mean)
    NEXT i
    variance = sumsq / n
    PRINT "Stats: min=" & STR$(minVal) & " max=" & STR$(maxVal) & " mean=" & FORMAT$(mean, "##.##") & " var=" & FORMAT$(variance, "##.##")
    PRINT
    PRINT

    names(1) = "Alice"
    names(2) = "Bob"
    names(3) = "Carol"
    names(4) = "David"
    names(5) = "Eve"
    scores(1) = 92
    scores(2) = 78
    scores(3) = 85
    scores(4) = 63
    scores(5) = 97

    PRINT "=== PARALLEL ARRAYS (name/score) ==="
    FOR i = 1 TO 5
        line = names(i)
        PRINT line & ": " & STR$(scores(i))
    NEXT i
    PRINT
    PRINT

    FOR i = 1 TO 10
        data(i) = i * 3
    NEXT i
    n = 10
    PRINT "=== CUMULATIVE SUM ==="
    PRINT "Data:     "
    line = ""
    FOR i = 1 TO 10
        line = line & FORMAT$(data(i), "####")
    NEXT i
    PRINT line
    PRINT "Cumulative:"
    line = ""
    csum = 0
    FOR i = 1 TO 10
        csum = csum + data(i)
        line = line & FORMAT$(csum, "####")
    NEXT i
    PRINT line
    PRINT

    PRINT "Array operations complete."
    FUNCTION = 0
END FUNCTION
