vector_extra.sml 958 B

123456789101112131415161718192021222324252627282930313233
  1. structure VectorExtra = struct
  2. fun sum vec =
  3. Vector.foldl (fn (elem, acc) => elem + acc)
  4. 0
  5. vec;
  6. fun filter proc vec =
  7. let
  8. val len = Vector.length vec;
  9. fun iter (ind, acc) =
  10. if ind < len
  11. then
  12. let
  13. val current = Vector.sub(vec, ind);
  14. in
  15. if proc current
  16. then iter (ind + 1, current :: acc)
  17. else iter (ind + 1, acc)
  18. end
  19. else
  20. rev acc
  21. in
  22. iter (0, [])
  23. end;
  24. fun all_in_list vec lst =
  25. Vector.foldl (fn (elem, acc) =>
  26. acc andalso (List.exists (fn lst_elem => lst_elem = elem)
  27. lst))
  28. true
  29. vec;
  30. end