From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

#!/usr/bin/ruby
#
#
func solve(n, pred) {
gather {
n.permutations { |*candidate|
if (pred.all { |p| p(candidate) }) {
take(candidate)
}
}
}
}
var predicates = [
->(s) { last(s) != "Baker" },
->(s) { first(s) != "Cooper" },
->(s) { (first(s) != "Fletcher") && (last(s) != "Fletcher") },
->(s) { index(s, "Miller") > index(s, "Cooper") },
->(s) { abs(index(s, "Smith") - index(s, "Fletcher")) != 1 },
->(s) { abs(index(s, "Cooper") - index(s, "Fletcher")) != 1 },
]
var Names = ["Baker", "Cooper", "Fletcher", "Miller", "Smith"]
var solutions = solve(Names, predicates)
solutions.each { .join(', ').say }
assert_eq(solutions, [["Smith", "Cooper", "Baker", "Fletcher", "Miller"]])