I'd like to propose a new join util.
The idea is simple: it calls foreach(str) before calling str.join. So, whenever you have something like
| foreach(str)
| ', '.join
you can replace it with
You can customize the string conversion by passing either a function or a format string as the second parameter, e.g.:
| join(', ', lambda x: '-{}-'.format(x)) # function
| join(', ', '-{}-') # fmt string
Implementation
def join(delim, formatter=str):
'''
join(' ')
join(' ', fmtFn)
join(' ', fmtString)
'''
return foreach(formatter) | delim.join
Tests
def test_join(self):
r = [1, 2, 3] > (pipe
| join(', ')
)
self.assertEquals(r, '1, 2, 3')
def test_join_with_formatter(self):
r = [1, 2, 3] > (pipe
| join(', ', lambda x: '-{}-'.format(x))
)
self.assertEquals(r, '-1-, -2-, -3-')
def test_join_with_fmtString(self):
r = [1, 2, 3] > (pipe
| join(', ', '-{}-')
)
self.assertEquals(r, '-1-, -2-, -3-')
I'd like to propose a new
joinutil.The idea is simple: it calls
foreach(str)before callingstr.join. So, whenever you have something likeyou can replace it with
You can customize the string conversion by passing either a function or a format string as the second parameter, e.g.:
Implementation
Tests