Here's a crazy piece of Python code (from @driscollis) illustrating how to calculate prime numbers below 1000 in a "functional" style.

How would I write this?

🧵

Functional programming like this can be great for minimizing lines of code. But it is also great for making your brain spin. Here is how I would initially write this (if I were fancy, I would use the Sieve of Eratosthenes):
Can we collapse this into fewer lines of code? Certainly, (the functional style already showed that) we can. One thing to realize is that lines 3-6 can be replaced with an any call:
At this point, the code is structured such that we can use a list comprehension (or generator expression). Alternatively,
we could parameterize the code into a function and convert it to a generator. Let's see the list comprehension:
Let's make it into a function:
Recognizing that the function is accumulating, we can convert it into a generator by replacing the .append call with yield, or converting the list comprehension version into a generator expression:
What code do I prefer? I try to focus on making readable code. While I understand comprehensions, I don't favor complex comprehensions and I would probably write out the for loop. Just because you can write less lines of code doesn't mean that you always should.
Thanks for reading!

Feel free to share if you learned something and follow me for more Python insights. 🐍

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with matt harrison

matt harrison Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @__mharrison__

28 Oct
Let's explore the "any" and "all" built-in functions in Python.

A 🧵
First of all, I'll teach you how to fish in Python before giving you the fish. 🐟🎣

The built-in "help" function will give you documentation in Python. Make liberal use of it and reach out to it before ceding control to a search engine. Image
These are "aggregation" or "reducing" functions. They take a sequence and collapse it to a single value.

"any" returns if any value was truthy.
"all" returns if all values were truthy.

Most examples look like this: Image
Read 8 tweets
26 Oct
I'm frequently asked: Is it easy to get a tech job?

Probably not. It will require a degree (or a lot of hard work). People who say otherwise are probably selling you something.

1/
I've taught thousands of people Python over the years. Some pick it up quickly, others really struggle. That is why Universities have "weeder" courses. To sift out the strugglers.

2/
Does that mean you can't self-learn? No, but it will take a lot more work than just sitting back and watching YouTube.

3/
Read 5 tweets
18 May
This tweet obviously resonates with many.

I often get asked about debugging when teaching a course...

A 🧵

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

-Kernighan's Law

🤯
When I teach programming, I emphasize that to me, the most important part is that the code is "readable."

You write code (once or twice), but it is read all the time. Optimize for what happens a lot.

Not:

😦 Brevity
😦 Cleverness
😦 Using all of the features
Read 13 tweets
17 Mar
I'm teaching a Fundamentals of Python class this week.

We talked about tuples, and there was some confusion as to their existence.

Certainly, you could live in a world without them, but...

🧵
First of all ... pronunciation.

You can say "two-pull" or "tuh-pull" (both are valid).
A big confusion is when to use a list or tuple?

My basic rule of thumb:

If you have items of the same type and care about the order or re-arranging it, you want a list.
Read 9 tweets
1 Jan
One of the most effective things I did for my career is probably the most difficult.

🧵👇
It is not difficult because it requires strength or knowledge, it is difficult because it requires will power.

I'm talking about writing a book. 📚
Writing a book taught me a bunch of lessons about work, myself, and business. I would say it jumpstarted my career (even though I had been employed for over 13 years by that point). ⚒🧠
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(