r/ProgrammerHumor • • Aug 26 '20

Python goes brrrr

Post image
59.2k Upvotes

789 comments sorted by

View all comments

Show parent comments

115

u/ProbablyInnacurate Aug 26 '20

I love comprehensions.

50

u/axlee Aug 26 '20

I prefer lambdas + map/reduce/filter/etc, usually easier to understand with a quick look

27

u/marmoshet Aug 26 '20

Functional programming gang rise up

5

u/-Potatoes- Aug 26 '20

In my first year at university we did functional programming (not in python, unfortunately) and I think its left me forever scarred

2

u/IAm_A_Complete_Idiot Aug 26 '20

Shoulda used prolog.

Facts and Rules are the only way to program

2

u/marmoshet Aug 26 '20

Racket is a beautiful language and I recommend using it or Scala for CS 241. :)

→ More replies (1)

15

u/ProbablyInnacurate Aug 26 '20

Oh yeah, defs. I get stuck making them complex, unreadable and un-pythonic because it's fun.

7

u/Ghos3t Aug 26 '20

I've heard list comprehension is more optimized that map, reduce etc due to the way it is implemented in Python. Something to do work map, reduce being function calls

6

u/[deleted] Aug 26 '20

My favorite is that there are options so you can pick the syntax you prefer

2

u/RIcaz Aug 26 '20

Perl has entered the chat

0

u/[deleted] Aug 26 '20

[deleted]

6

u/13steinj Aug 26 '20

This depends on the person and the code.

[int(x) for x in foo]

Vs

map(int, foo)

For me the second reads easier ("map int onto everything in foo" rather than "call int on every element in foo").

For more complex tasks that use lambdas/obscure bound partial methods instead, I'd prefer a list comprehension.

1

u/ketexon Aug 26 '20

Apparently map is quite a lot slower than comprehension using lambdas. Maps are faster when using stuff like str though.

Source: test yourself pretty easily or StackOverflow

26

u/Mr_Redstoner Aug 26 '20

As a (mainly) Java programmer, a lot of my Streams would be comprehensions in Python.

16

u/mpa92643 Aug 26 '20

As a long time Java developer, I so very much appreciate Streams. It's so much more readable to say "I have a stream of Xs; convert them to Ys, take out the lowercase ones, add them to a Set, and return it" than "create a new Set. Now iterate through all the Xs. Declare a variable of type Y. Now set it to the conversion result of X. If Y is lowercase, add it to the set. Now return the set."

And that's a simple example. Once you start dealing with Lists of Lists, things go off the rails so quickly and the nesting becomes so ugly.

7

u/Dizzfizz Aug 26 '20

That’s interesting to read, as a beginner I find them very confusing and think it’s much simpler to do one thing after another, especially once it comes to looking for bugs.

9

u/AnotherUpsetFrench Aug 26 '20

You will get used to it, I promise, especially when you will start to gain more time and less headaches.

8

u/mpa92643 Aug 26 '20

Yeah, I can understand that. Streams add another layer of abstraction that requires understanding the core behaviors first. Streams are basically shortcuts for longer blocks of code, and they're easier to compose but can be harder to debug if you're not certain about what's happening under the hood. They provide incredible flexibility and conciseness, which is why they're so useful.

Just wait until you start getting into RX. Even once you get the hang of Java Streams, RX is going to make you so confused and frustrated that you're going to want to give up, but sticking with it is so worth it in the end. I'd recommend waiting a few years before even looking into RX though.

→ More replies (1)
→ More replies (9)

2

u/renrutal Aug 26 '20

The functional programming added to Java 8 really hurt the readability of the programs.

Scala and Kotlin did much better job integrating it in.

3

u/ArionW Aug 26 '20

It's Java developers who hurt readability of programs, they got new features but nobody taught them how to use them properly

7

u/Rasmaellin Aug 26 '20 edited Jan 01 '21

Learn Haskell! We have general monad comprehensions. Lists, IO, parser combinators—as long as it's a monad, we have comprehensions for it. And sometimes even comprehensions for non-monads with ApplicativeDo.

Heck, I wrote a brainfuck interpreter comprehension not too long ago.

0

u/[deleted] Aug 26 '20

[deleted]

→ More replies (2)

2

u/al_at_work Aug 26 '20

Dictionary comprehensions and generator expressions are so under-appreciated. Many people use list comprehensions because they're not aware the other two exist, but they (especially generator expressions) are so powerful.

1

u/JJ_The_Jet Aug 26 '20

[i for i in ‘abcdefghijklmnop.’]. So much better then typing [‘a’, ‘b’, ‘c’, ...]

2

u/mxzf Aug 26 '20

Alternatively, you can just do list('abcd') instead and cast the iterable string straight to a list (or, better yet most of the time), just iterate over the string in the first place without casting it to list.