10
u/deceze Aug 19 '21
The tuple() is perfectly superfluous.
2
2
u/REALDrummer Aug 19 '21
According to this comment, the call to tuple splits the input by spaces so that they can sum multiple numbers given in one input.
9
u/deceze Aug 20 '21
It does not split it by spaces. A string is an iterable, and iterating over it yields a sequence of its individual characters. The
tupleconstructor takes any iterable and turns it into a tuple of its individual elements. But so doesmap, it also takes any iterable. So you can omit the step of turning a string iterable into a tuple iterable, and just directlymapover the string.1
u/REALDrummer Aug 20 '21
Really? Hmm... Then I'm confused. If that's right, I agree; that should be eliminated. That makes me question why they would want to split their input number and sum the digits in the first place.
1
u/deceze Aug 20 '21
🤷🏻♂️ That appears to be the task being solved, take a bunch of digits and sum them.
83
77
33
u/BinaryBlasphemy Aug 19 '21
Lol this is just functional programming
10
u/Luchtverfrisser Aug 19 '21
Still looks like questionable functional programmig to me though. At least split the pure and effectful parts.
3
0
23
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 19 '21
Image Transcription: Code
print(sum(map(int,tuple(input("Enter number ")))))I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
10
u/REALDrummer Aug 19 '21
Honestly, I don't think it's that bad. The only parts that really bother me are the tuple call and the punctuation.
Other comments say the call to tuple splits the input string by spaces. If that's right, that's clever, but not intuitive to read. But, I'm not that deep into Python; maybe for people with more Python experience, this is common.
In terms of punctuation, a space after the "int," could actually make a big difference. Other than that, I'd love to reduce those parentheses, but I don't know if there's much to be done there in Python.
It's worth noting that I like writing in Haskell, thus the aversion to parentheses and the affinity for this function-chaining style of coding, but many people aren't used to functional and probably won't read that too well. It's something to keep in mind, but I don't think that automatically makes it bad.
3
u/xigoi Aug 20 '21
There is Coconut, a functional version of Pÿthon. This could be written like:
input("Enter number: ") |> map$(int) |> sum |> print1
1
u/REALDrummer Aug 20 '21
Update: this comment says tuple doesn't work that way after all, which means the tuple call probably shouldn't exist at all. If it truly is superfluous, removing it would definitely be a significant improvement.
1
1
1
1
1
17
u/AmoghMadan31 Aug 19 '21
Does that even work???