Scala has the apply()
function.
I am new to Python and I am wondering how should I write the following one-liner:
(part_a, part_b) = (lambda x: re.search(r"(\w+)_(\d+)", x).groups())(input_string)
I would feel better with something like:
(part_a, part_b) = input_string.apply(lambda x: re.search(r"(\w+)_(\d+)", x).groups())
Am I wrong from a FF viewpoint? Is there such construction in Python?
Edit: I know about the poorly picked snippet.
When writing Haskell write Haskell. When writing Python just write Python:
part_a, part_b = re.search(r"(\w+)_(\d+)", input_string).groups()
If you're using a Python without apply
, you could always write it yourself...
def apply(fn, args=(), kwargs=None):
if kwargs is None:
kwargs = {}
return fn(*args, **kwargs)
just because you could, doesn't mean you should..
Python does not have such a construct; what would be the point? lambda x: ...
is a function, and therefore it should be used like a normal function, i.e., as (lambda x: ...)(input_string)
as in your first snippet.
However, in your case, I see no reason why you should even bother with a lambda; just do:
(part_a, part_b) = re.search(r"(\w+)_(\d+)", input_string).groups()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With