Matt Harrison Profile picture
Oct 27, 2021 β€’ 8 tweets β€’ 3 min read β€’ Read on X
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__

Apr 19, 2024
XGBoost is a popular machine learning algorithm that provides excellent performance out of the box for various types of machine learning problems, including regression and classification. Image
However, one common issue with XGBoost is overfitting, leading to poor generalization and inaccurate predictions on new data.

Overfitting occurs when the model learns the training data too well, including the noise and randomness, and cannot generalize to new data.
You can use various techniques to determine whether an XGBoost model is overfitting, including cross-validation, regularization, and monitoring the training and validation errors.
Read 6 tweets
Mar 15, 2024
Big O notation (pronounced Big-Oh) is a way to discuss how long an algorithm takes to run. It is also called "runtime complexity".

Understanding this is key for technologists.

Let's look at it.

1/
If we want to calculate the mean of a sequence, a naive implementation will calculate the sum and divide it by the length:

2/ Image
Often we express Big O in terms of the size of the data and N is commonly used to represent the numbers of items.

We have to loop over every item in seq. There are N items, so the runtime complexity of the mean function is O(N).

3/
Read 10 tweets
Mar 6, 2024
A thread about pandas... (The library not the animal)

🐼🧡 Image
I like to describe pandas:

β‹… Like Excel

β‹… Like SQL

Things that you would do with Excel or SQL, you can program in pandas Image
It is a Python library, so it is good to understand these parts of the Python language:

* Syntax (index vs invocation vs attribute access, slicing)

* Functions

* Lambdas

* List comprehensions Image
Read 7 tweets
Feb 8, 2024
I often teach about Decorators in Python.

Many know how to use them, but few can write them.

These are tricky because nested functions make our brains hurt.

Here are some hints for grokking them.

1/ Image
In short, decorators allow you to inject orthogonal behavior before or after a function is executed.

But my favorite decorator definition is related to the construction and will help you easily create them: A callable that takes a callable and returns a callable.

2/
What do I mean by "orthogonal"?

A function should do one thing. If you want to add caching or logging, it really isn't related to the function (and could be applied to multiple functions). It is "orthogonal" behavior.

3/
Read 13 tweets
Oct 13, 2023
I often teach about Decorators in Python.

Many know how to use them, but few can write them.

These are tricky because nested functions make our brains hurt.

Here are some hints for grokking them.

1/ Image
In short, decorators allow you to inject orthogonal behavior before or after a function is executed.

But my favorite decorator definition is related to the construction and will help you easily create them: A callable that takes a callable and returns a callable.

2/
What do I mean by "orthogonal"?

A function should do one thing. If you want to add caching or logging, it really isn't related to the function (and could be applied to multiple functions). It is "orthogonal" behavior.

3/
Read 13 tweets
Sep 21, 2023
I often teach about Decorators in Python.

Many know how to use them, but few can write them.

These are tricky because nested functions make our brains hurt.

Here are some hints for grokking them.

1/ Image
In short, decorators allow you to inject orthogonal behavior before or after a function is executed.

But my favorite decorator definition is related to the construction and will help you easily create them: A callable that takes a callable and returns a callable.

2/
What do I mean by "orthogonal"?

A function should do one thing. If you want to add caching or logging, it really isn't related to the function (and could be applied to multiple functions). It is "orthogonal" behavior.

3/
Read 13 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

Don't want to be a Premium member but still want to support us?

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

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(