table.lua 715 B

123456789101112131415161718192021222324
  1. local Table = {}
  2. --- Find the first entry for which the predicate returns true.
  3. -- @param t The table
  4. -- @param predicate The function called for each entry of t
  5. -- @return The entry for which the predicate returned True or nil
  6. function Table.find_first(t, predicate)
  7. for _, entry in pairs(t) do
  8. if predicate(entry) then
  9. return entry
  10. end
  11. end
  12. return nil
  13. end
  14. --- Check if the predicate returns True for at least one entry of the table.
  15. -- @param t The table
  16. -- @param predicate The function called for each entry of t
  17. -- @return True if predicate returned True at least once, false otherwise
  18. function Table.contains(t, predicate)
  19. return Table.find_first(t, predicate) ~= nil
  20. end
  21. return Table