I have a list of struct here [~e[0 0 * * 1-5 ], ~e[/10 * * * * *], ~e[0 */3 * * * *]] which is represented through sigil. I need to take one expression at a time and compare it against this function
Crontab.DateChecker.matches_date?(~e[0 0 * * 1-5 *], ~N[2021-08-12 00:00:00])
if it matches it will return true otherwise false. But somehow I need to pass this one by one to check with every expression
I am not sure if I understand correctly, but if so, you are looking for
Enum.map/2, or possibly
Enum.all?/2, depending on what you want (map each entry to a boolean, or check if any/all of them matches)
entries = [~e[0 0 * * 1-5 ], ~e[/10 * * * * *], ~e[0 */3 * * * *]]
Enum.map(entries, fn entry ->
Crontab.DateChecker.matches_date?(entry, ~N[2021-08-12 00:00:00])
I just need to pass those expressions one by one to match them against the DateTime. Is this help?
Do you need to get a boolean answer (matches or not) for each of them, or check if any of them matches, or if all of them match?
Yes, I need to get a boolean answer. In this case I need to check if any one of them matches.
Enum.map solution above will return a list of booleans, one for each entry, but probably it’s more appropriate to use
Enum.any?, which would give you a single true or false value depending on whether any of the entries matches or not.
But I think there will be a case where more than one can also match. What should I return in that case? Any suggestion for that case?
iex(87)> Enum.map(b, fn b → Crontab.DateChecker.matches_date?(b, ~N[2021-08-12 00:00:00]) end)
[true, true, true]
Like for this case
I think Enum.any? will be better there
That depends on what you application needs.
Do you need to know if even just one of them matches? Then use
Or do you need to know if all of them match? Then use
Or do you need to map each of the entries to a true or false answer? Then either
Enum.map as shown above or
Enum.reduce to create a map of entry to boolean.
What kind of value should your code return?
Isn’t the solution in the title?
We just need to return true or false. I think we can’t use Enum.all? because it needed all true values. So if one expression doesn’t match it will return false. I think Enum.any works perfectly here for now.
Filter return type would be different right
Enum.any? is exactly what you need. Where’s the confusion?
Yup. It has been solved. Thanks