I'd like to propose a new util: foreach_i.
Motivation
You know how JS's Array.map also passes the element index as a 2nd parameter to the function?
> ['a', 'b', 'c'].map((x, i) => `Element ${i} is ${x}`)
[ 'Element 0 is a', 'Element 1 is b', 'Element 2 is c' ]
That's exactly what I'm trying to do here. The only difference is that the index would be passed as the 1st param:
def test_foreach_i():
r = ['a', 'b', 'c'] > (pipe
| foreach_i(lambda i, x: f'Element {i} is {x}')
| list
)
assert r == [ 'Element 0 is a'
, 'Element 1 is b'
, 'Element 2 is c'
]
(Naïve) Implementation
from pipetools.utils import foreach, as_args
from typing import Callable, TypeVar
A = TypeVar('A')
B = TypeVar('B')
def foreach_i(f: Callable[[int, A], B]):
return enumerate | foreach(as_args(f))
The same could be done for foreach_do.
I'd like to propose a new util:
foreach_i.Motivation
You know how JS's
Array.mapalso passes the element index as a 2nd parameter to the function?That's exactly what I'm trying to do here. The only difference is that the index would be passed as the 1st param:
(Naïve) Implementation
The same could be done for
foreach_do.