Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Passing a Predicate to a Routine

44 views
Skip to first unread message

manueurofax

unread,
Apr 25, 2025, 5:27:28 AMApr 25
to Google Apps Script Community
Hy,
I'm wondering if it's possible to pass a Predicate to a Routine and activate it from the Routine...
If you can have an example...
Thank you...

Keith Andersen

unread,
Apr 25, 2025, 7:54:52 AMApr 25
to google-apps-sc...@googlegroups.com

per Claude Ai....

Here's a practical example that demonstrates this concept:

```javascript
// This is the "routine" function that accepts a predicate as a parameter
function filterStudents(students, predicate) {
  const results = [];
 
  for (let student of students) {
    // Here we "activate" or call the predicate function that was passed in
    if (predicate(student)) {
      results.push(student);
    }
  }
 
  return results;
}

// Example usage with different predicates
function main() {
  const studentList = [
    {name: "Alice", grade: 90, subject: "Math"},
    {name: "Bob", grade: 75, subject: "Science"},
    {name: "Charlie", grade: 85, subject: "Math"},
    {name: "Diana", grade: 95, subject: "History"}
  ];
 
  // Predicate 1: Students with grades above 80
  const highPerformers = filterStudents(studentList, function(student) {
    return student.grade > 80;
  });
  Logger.log("High performers: " + JSON.stringify(highPerformers));
 
  // Predicate 2: Math students
  const mathStudents = filterStudents(studentList, function(student) {
    return student.subject === "Math";
  });
  Logger.log("Math students: " + JSON.stringify(mathStudents));
}
```

In this example:
1. The `filterStudents` function is our "routine" that accepts a predicate function as its second parameter
2. The predicate functions are the anonymous functions that test a condition (grade > 80 or subject === "Math")
3. The routine "activates" the predicate by calling it with `predicate(student)` on each student
4. Based on whether the predicate returns true or false, the routine decides what to do

This pattern is powerful because it allows you to create a generalized function (the routine) that can behave differently based on the function (predicate) that's passed to it. You can reuse the routine with different predicates instead of writing separate functions for each type of filtering.



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/0ff90d8a-c6d9-44b5-a89a-3ed28735f34cn%40googlegroups.com.

DimuDesigns

unread,
Apr 25, 2025, 5:56:39 PMApr 25
to Google Apps Script Community
If by predicate you mean a function then yes you can.

Functions are first-class objects in Javascript...and therefore Google Apps Script (GAS is a Javascript runtime).

You can pass them around as arguments to another function.

manueurofax

unread,
Apr 26, 2025, 8:58:13 AMApr 26
to Google Apps Script Community

thank you all...
Reply all
Reply to author
Forward
0 new messages