Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ R.propEq(1, 'v1', {'v1': 1}) # True
- [ ] reduceWhile
- [x] 0.1.2 reject
- [ ] remove
- [ ] repeat
- [x] repeat
- [ ] replace
- [x] 0.1.2 reverse
- [ ] scan
Expand Down Expand Up @@ -474,7 +474,7 @@ R.subtract(date(1,2,3), date(1,2,3)) # float('nan)
- [x] 0.1.2 tap
- [ ] test
- [ ] thunkify
- [ ] times
- [x] times
- [ ] toLower
- [ ] toPairs
- [ ] toPairsIn
Expand Down
2 changes: 2 additions & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
from .reduced import reduced
from .reduceRight import reduceRight
from .reject import reject
from .repeat import repeat
from .reverse import reverse
from .slice import slice
from .sort import sort
Expand All @@ -86,6 +87,7 @@
from .take import take
from .takeWhile import takeWhile
from .tap import tap
from .times import times
from .toString import toString
from .union import union
from .unionWith import unionWith
Expand Down
5 changes: 5 additions & 0 deletions ramda/repeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .always import always
from .private._curry2 import _curry2
from .times import times

repeat = _curry2(lambda value, n: times(always(value), n))
20 changes: 20 additions & 0 deletions ramda/times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from math import isnan

from .private._curry2 import _curry2
from .private._helper import toNumber


def inner_times(fn, n):
length = toNumber(n)
idx = 0

if length < 0 or isnan(length):
raise ValueError('n must be a non-negative number')
arr = []
while idx < length:
arr.append(fn(idx))
idx += 1
return arr


times = _curry2(inner_times)
20 changes: 20 additions & 0 deletions test/test_repeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import unittest

import ramda as R

"""
https://github.com/ramda/ramda/blob/master/test/repeat.js
"""


class TestRepeat(unittest.TestCase):
def test_returns_a_lazy_list_of_identical_values(self):
self.assertEqual([0, 0, 0, 0, 0], R.repeat(0, 5))

def test_can_accept_any_value_including_None(self):
self.assertEqual([None, None, None], R.repeat(None, 3))


if __name__ == '__main__':
unittest.main()
24 changes: 24 additions & 0 deletions test/test_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import unittest

import ramda as R

"""
https://github.com/ramda/ramda/blob/master/test/times.js
"""


class TestTimes(unittest.TestCase):
def test_takes_a_map_func(self):
self.assertEqual([0, 1, 2, 3, 4], R.times(R.identity, 5))
self.assertEqual([0, 2, 4, 6, 8], R.times(R.multiply(2), 5))

def test_throws_if_second_argument_is_not_a_valid_array_length(self):
with self.assertRaises(ValueError):
R.times(3)('cheers!')
with self.assertRaises(ValueError):
R.times(R.identity, -1)


if __name__ == '__main__':
unittest.main()