concept predicate in category functional programming

appears as: predicate, predicate, predicates
Functional Programming in C++

This is an excerpt from Manning's book Functional Programming in C++.

The first higher-level construct you could use here is collection filtering. Generally speaking, filtering is a simple algorithm that checks whether an item in the original collection satisfies a condition; if it does, the item is put into the result. The filtering algorithm can’t know in advance what predicates users will use to filter their collections. Filtering could be done on a specific attribute (the example is filtering on a specific gender value), on multiple attributes at the same time (such as getting all females with black hair), or on a more complex condition (getting all females who recently bought a new car). So, this construct needs to provide a way for the user to specify the predicate. In this case, the construct needs to let you provide a predicate that takes a person and returns whether the person is female. Because filtering allows you to pass a predicate function to it, filtering is, by definition, a higher-order function.

Figure 2.10 Partitioning a group of people based on a predicate that checks whether a person is female. As the result, all females are moved to the start of the collection.

c02_10.png
Listing 2.12 Naive recursive implementation
template <typename FilterFunction>
std::vector<std::string> names_for(
        const std::vector<person_t>& people,
        FilterFunction filter)
{
    if (people.empty()) {
        return {};    #1  

    } else {
        const auto head = people.front();
        const auto processed_tail = names_for(    #2  
                tail(people),
                filter);

        if (filter(head)) {    #3  
            return prepend(name(head), processed_tail);    #3  
        } else {    #3  
            return processed_tail;    #3  
        }
    }
}

#1   If the collection is empty, returns an empty result
#2   Calls the function recursively to process the collection’s tail
#3   If the first element satisfies the predicate, includes it in the result. Otherwise, skips it.
Functional Programming in Java: How functional techniques improve your Java programs

This is an excerpt from Manning's book Functional Programming in Java: How functional techniques improve your Java programs.

I chose a while loop because it translates more easily into a generic method that can be applied to any type, given a function from this type to itself and a second function (called a predicate) from this type to a Boolean.

public static <T> Result<T> of(T value) {
  return value != null
      ? success(value)
      : Result.failure("Null value");
}

public static <T> Result<T> of(T value, String message) {
  return value != null
      ? success(value)
      : failure(message);
}

public static <T> Result<T> of(Function<T, Boolean> predicate, T value) {
  try {
    return predicate.apply(value)
        ? success(value)
        : empty();
  } catch (Exception e) {
    String errMessage =
        String.format("Exception while evaluating predicate: %s", value);
    return Result.failure(new IllegalStateException(errMessage, e));
  }
}

public static <T> Result<T> of(Function<T, Boolean> predicate,
                               T value, String message) {
  try {
    return predicate.apply(value)
        ? Result.success(value)
        : Result.failure(String.format(message, value));
  } catch (Exception e) {
    String errMessage =
            String.format("Exception while evaluating predicate: %s",
                                         String.format(message, value));
    return Result.failure(new IllegalStateException(errMessage, e));
  }
}

7.5.1. Applying predicates

Applying a predicate to a Result is something that you’ll often have to do. This is something that can easily be abstracted, so that you can write it only once.