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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# python_ramda


This is a repo try to copy <https://github.com/ramda/ramda> in python.

[![linting: pylint](https://img.shields.io/badge/linting-pylint-yellowgreen)](https://github.com/PyCQA/pylint)
Expand Down Expand Up @@ -195,7 +194,7 @@ R.equals(float('nan'), float('nan')) # True
- [ ] findLastIndex
- [x] 0.1.2 flatten
- [x] 0.1.2 flip
- [ ] forEach
- [x] forEach
- [ ] forEachObjIndexed
- [ ] fromPairs
- [x] 0.1.2 groupBy
Expand Down
1 change: 1 addition & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .find import find
from .flatten import flatten
from .flip import flip
from .forEach import forEach
from .groupBy import groupBy
from .gt import gt
from .gte import gte
Expand Down
14 changes: 14 additions & 0 deletions ramda/forEach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .private._checkForMethod import _checkForMethod
from .private._curry2 import _curry2


def inner_forEach(fn, arr):
length = len(arr)
idx = 0
while idx < length:
fn(arr[idx])
idx += 1
return arr


forEach = _curry2(_checkForMethod('forEach', inner_forEach))
52 changes: 52 additions & 0 deletions test/test_forEach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

import unittest

import ramda as R

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

arr = [{'x': 1, 'y': 2}, {'x': 100, 'y': 200}, {'x': 300, 'y': 400}, {'x': 234, 'y': 345}]


class TestForEach(unittest.TestCase):
def test_performes_the_passed_in_function_on_each_element_of_the_list(self):
sideEffect = {}

def fn(elem):
nonlocal sideEffect
sideEffect[elem['x']] = elem['y']
R.forEach(fn, arr)
self.assertEqual(sideEffect, {1: 2, 100: 200, 300: 400, 234: 345})

def test_returns_the_original_list(self):
s = ''

def fn(obj):
nonlocal s
s += str(obj['x'])
self.assertEqual(arr, R.forEach(fn, arr))
self.assertEqual(s, '1100300234')

def test_handles_empty_list(self):
self.assertEqual([], R.forEach(lambda x: x * x, []))

def test_dispatches_to_forEach_method(self):
dispatched = False

def fn():
return

class DummyList:
def forEach(this, callback):
nonlocal dispatched
dispatched = True
self.assertEqual(fn, callback)

R.forEach(fn, DummyList())
self.assertEqual(True, dispatched)


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