Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module 5: Streams 2 is stringly typed #36

Open
xgillard opened this issue Dec 2, 2020 · 0 comments
Open

Module 5: Streams 2 is stringly typed #36

xgillard opened this issue Dec 2, 2020 · 0 comments

Comments

@xgillard
Copy link
Collaborator

xgillard commented Dec 2, 2020

This issue relates to #35 (actually this should be a subtask of the previous).

Streams 2 is "stringly typed" this is not how we should teach students to code.
The easiest and cleanest way to solve all questions si to define the following method:

private final static Predicate<Student> pred(final Map<String, Predicate<?>> conditions) {
    return (s) -> {
      for (Map.Entry<String, Predicate<?>> e : conditions.entrySet()) {
        switch (e.getKey()) {
          case "firstName":
            if (!((Predicate<String>) e.getValue()).test(s.getFirstName()))
              return false;
            else break;
          case "lastName":
            if (!((Predicate<String>) e.getValue()).test(s.getLastName()))
              return false;
            else break;
          case "section":
            if (!((Predicate<Integer>) e.getValue()).test(s.getSection()))
              return false;
            else break;
          case "courses_results":
            if (!((Predicate<Map<String, Double>>) e.getValue()).test(s.getCourses_results()))
              return false;
            else break;
          default:
            throw new RuntimeException("Unexpected key");
        }
      }

      return true;
    };
  }

One should rely on the type system to help student get a grasp on what should be done. Also, it would be great if we could show students how to combine predicates (and, or, not, ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant