about summary refs log tree commit diff stats
path: root/js/baba-yaga/scratch/baba/test_comprehensive_features.baba
blob: 7a205b155fd2306a94a9a3d5fd4ec91e5bb25bcb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Comprehensive test combining all new functional programming features

io.out "=== Comprehensive Feature Test ===";

// Example: Processing student data with advanced functional programming

// Sample student data
students : [
  {name: "Alice", scores: [85, 92, 78, 90]},
  {name: "Bob", scores: [75, 88, 82, 79]},
  {name: "Charlie", scores: [95, 96, 94, 97]},
  {name: "Diana", scores: [65, 70, 68, 72]}
];

// Calculate average score
calculateAverage : scores ->
  with (
    total : reduce (acc x -> acc + x) 0 scores;
    count : length scores;
  ) ->
    total / count;

// Process each student using flatMap and array operations
processStudent : student ->
  with (
    scores : student.scores;
    average : calculateAverage scores;
    cumulative : cumsum scores;
    
    // Use pattern guards to assign grades
    grade : when average is
      a if (a >= 90) then "A"
      a if (a >= 80 and a < 90) then "B"  
      a if (a >= 70 and a < 80) then "C"
      a if (a >= 60 and a < 70) then "D"
      a if (a < 60) then "F"
      _ then "Invalid";
    
    // Use broadcasting to normalize scores
    maxScore : reduce (acc x -> math.max acc x) 0 scores;
    normalized : broadcast (score max -> score / max * 100) maxScore scores;
  ) ->
    {
      name: student.name,
      average: average,
      grade: grade,
      cumulative: cumulative,
      normalized: normalized
    };

// Process all students
processedStudents : map processStudent students;

io.out "Processed Students:";
io.out processedStudents;

// Advanced analysis using array programming
io.out "=== Advanced Analysis ===";

// Extract all scores using flatMap
allScores : flatMap (s -> s.scores) students;
io.out "All scores:";
io.out allScores;

// Find top performers using where and at
highScoreIndices : where (score -> score >= 90) allScores;
highScores : at highScoreIndices allScores;
io.out "High scores (>=90):";
io.out highScores;

// Use zipWith to compare consecutive students
studentNames : map (s -> s.name) processedStudents;
studentAverages : map (s -> s.average) processedStudents;

// Reshape data into matrix for analysis
scoresMatrix : reshape [4, 4] allScores;
io.out "Scores as 4x4 matrix:";
io.print scoresMatrix;

// Combine multiple operations in a pipeline
topStudents : filter (s -> s.average >= 85) processedStudents;
topStudentAnalysis : sort.by topStudents (s -> s.average);

io.out "Top students (avg >= 85), sorted by average:";
io.out topStudentAnalysis;

io.out "=== All comprehensive tests completed ===";