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.

56 lines
1.6 KiB

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import _dispatchable from "./internal/_dispatchable.js";
  3. import _xtake from "./internal/_xtake.js";
  4. import slice from "./slice.js";
  5. /**
  6. * Returns the first `n` elements of the given list, string, or
  7. * transducer/transformer (or object with a `take` method).
  8. *
  9. * Dispatches to the `take` method of the second argument, if present.
  10. *
  11. * @func
  12. * @memberOf R
  13. * @since v0.1.0
  14. * @category List
  15. * @sig Number -> [a] -> [a]
  16. * @sig Number -> String -> String
  17. * @param {Number} n
  18. * @param {*} list
  19. * @return {*}
  20. * @see R.drop
  21. * @example
  22. *
  23. * R.take(1, ['foo', 'bar', 'baz']); //=> ['foo']
  24. * R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
  25. * R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  26. * R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz']
  27. * R.take(3, 'ramda'); //=> 'ram'
  28. *
  29. * const personnel = [
  30. * 'Dave Brubeck',
  31. * 'Paul Desmond',
  32. * 'Eugene Wright',
  33. * 'Joe Morello',
  34. * 'Gerry Mulligan',
  35. * 'Bob Bates',
  36. * 'Joe Dodge',
  37. * 'Ron Crotty'
  38. * ];
  39. *
  40. * const takeFive = R.take(5);
  41. * takeFive(personnel);
  42. * //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan']
  43. * @symb R.take(-1, [a, b]) = [a, b]
  44. * @symb R.take(0, [a, b]) = []
  45. * @symb R.take(1, [a, b]) = [a]
  46. * @symb R.take(2, [a, b]) = [a, b]
  47. */
  48. var take =
  49. /*#__PURE__*/
  50. _curry2(
  51. /*#__PURE__*/
  52. _dispatchable(['take'], _xtake, function take(n, xs) {
  53. return slice(0, n < 0 ? Infinity : n, xs);
  54. }));
  55. export default take;