You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import _dispatchable from "./internal/_dispatchable.js";
  3. import _xdropRepeatsWith from "./internal/_xdropRepeatsWith.js";
  4. import last from "./last.js";
  5. /**
  6. * Returns a new list without any consecutively repeating elements. Equality is
  7. * determined by applying the supplied predicate to each pair of consecutive elements. The
  8. * first element in a series of equal elements will be preserved.
  9. *
  10. * Acts as a transducer if a transformer is given in list position.
  11. *
  12. * @func
  13. * @memberOf R
  14. * @since v0.14.0
  15. * @category List
  16. * @sig ((a, a) -> Boolean) -> [a] -> [a]
  17. * @param {Function} pred A predicate used to test whether two items are equal.
  18. * @param {Array} list The array to consider.
  19. * @return {Array} `list` without repeating elements.
  20. * @see R.transduce
  21. * @example
  22. *
  23. * const l = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3];
  24. * R.dropRepeatsWith(R.eqBy(Math.abs), l); //=> [1, 3, 4, -5, 3]
  25. */
  26. var dropRepeatsWith =
  27. /*#__PURE__*/
  28. _curry2(
  29. /*#__PURE__*/
  30. _dispatchable([], _xdropRepeatsWith, function dropRepeatsWith(pred, list) {
  31. var result = [];
  32. var idx = 1;
  33. var len = list.length;
  34. if (len !== 0) {
  35. result[0] = list[0];
  36. while (idx < len) {
  37. if (!pred(last(result), list[idx])) {
  38. result[result.length] = list[idx];
  39. }
  40. idx += 1;
  41. }
  42. }
  43. return result;
  44. }));
  45. export default dropRepeatsWith;