' 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 outLine 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:"
    outLine = ""
    FOR i = 1 TO n
        outLine = outLine & STR$(data(i)) & " "
    NEXT i
    PRINT outLine

    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):"
    outLine = ""
    FOR i = 1 TO resultLen
        outLine = outLine & STR$(result(i)) & " "
    NEXT i
    PRINT outLine

    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):"
    outLine = ""
    FOR i = 1 TO n
        outLine = outLine & STR$(result(i)) & " "
    NEXT i
    PRINT outLine

    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=" & STR$(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 (sName/score) ==="
    FOR i = 1 TO 5
        outLine = names(i)
        PRINT outLine & ": " & 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:     "
    outLine = ""
    FOR i = 1 TO 10
        outLine = outLine & STR$(data(i))
    NEXT i
    PRINT outLine
    PRINT "Cumulative:"
    outLine = ""
    csum = 0
    FOR i = 1 TO 10
        csum = csum + data(i)
        outLine = outLine & FORMAT$(csum)
    NEXT i
    PRINT outLine
    PRINT

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