Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Replacing loops with safer constructs #3

Open
katrinleinweber opened this issue Aug 15, 2018 · 9 comments
Open

Replacing loops with safer constructs #3

katrinleinweber opened this issue Aug 15, 2018 · 9 comments
Assignees
Labels
event Topic idea/suggestion for an event. Needs assignee & votes to be scheduled as _post/*.md R

Comments

@katrinleinweber
Copy link
Contributor

katrinleinweber commented Aug 15, 2018

Constructing your own for-loops is only rarely actually necessary. We'll learn about alternatives here. Bring your own code and let's de-loop it! Good introduction in German: Heise Developer: Weg mit den Schleifen!

What should/will the learning objective(s) be?

What do participants need to know / install / bring?

What next? Upvote this topic. It will be scheduled when a) 3 or more people upvoted and b) it has an assignee. If you want to teach this topic, please say so. To schedule the event, create a new file in the _posts folder, copy this template's filename and content and fill in the new event metadata.
@katrinleinweber katrinleinweber added the event Topic idea/suggestion for an event. Needs assignee & votes to be scheduled as _post/*.md label Aug 15, 2018
@rbirkelbach rbirkelbach added the R label Aug 16, 2018
@jkreklow
Copy link
Collaborator

I have a couple of examples for de-looping in Python

@david-morris
Copy link
Collaborator

I could certainly help with this, and I'd be comfortable teaching functional Python after the teaching primer. I've TA'd that class before...

But why? Functional programming is honestly interesting mostly for people who are interested in code for code's sake. Wouldn't our target audience be better off being told to use the for item in collection pattern and list comprehensions, which they would be more likely to use?

I think if we want to reduce the use of unpredictable structures, we should look at "a monad is a monoid in the category of endofunctors, what's the problem" as a not do.

@jkreklow
Copy link
Collaborator

I agree that, in most cases, you'll use loops and list comprehensions. But as soon as it comes to time-consuming calculations on big datasets, I'm very happy to know how to use functional Python to save my day (and lots of time). So it probably depends on the audience we have...

@jkreklow
Copy link
Collaborator

Another very powerful tool for major efficiency improvements without much programming effort is just in time compilation with numba. This can be used to speed up pure numpy code with lots of loops instead of de-looping ;-)

@david-morris
Copy link
Collaborator

Something that meshes with efficiency and correctness concerns is if we teach people to manipulate data with the methods of the data structures they're using. That's especially good when we're teaching people how to write code in languages with a global interpreter lock / green threads only, with data structures provided by libraries written in C++.

@katrinleinweber
Copy link
Contributor Author

katrinleinweber commented Aug 22, 2018

But why? Functional programming is honestly interesting mostly for people who are interested in code for code's sake.

I take the opposite stand here: the functional paradigm avoids for loops as an error source and may prove more intuitive/natural. Maybe it's a brain-type-thingy ;-)

@katrinleinweber katrinleinweber removed the R label Aug 22, 2018
@david-morris
Copy link
Collaborator

I think that for loops are more universal between the languages scientists would use, and if we can get them to not use while loops, that would be an effective way of preventing the errors mentioned on that blog recommending functional programming.

After all, the choice is between learning how to use for loops, or learning how to use map, reduce, zip, and select (they have to write functions anyway). To someone who sees programming as a tax they have to pay to do science, I'd expect the first sounds more appealing, so they're going to do it, whether they have us around to tell us how to do it or not.

@david-morris
Copy link
Collaborator

Rereading my previous comments makes me think we could sell functional programming as "how to work with large datasets."

Come for the map-reduce and array broadcasts, stay for clean functional code.

@katrinleinweber
Copy link
Contributor Author

Hi @david-morris! I'm not an expert in list comprehension in python. If they are comparable to R's purrr::map(), we can include them. Or make a separate event out of it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
event Topic idea/suggestion for an event. Needs assignee & votes to be scheduled as _post/*.md R
Projects
None yet
Development

No branches or pull requests

4 participants