PurelyFunctional.tv Newsletter 380: what can you iterate through?

Issue 380 - June 01, 2020 ยท Archives ยท Subscribe

Clojure Tip ๐Ÿ’ก

what can you iterate through?

I was helping a friend solve a coding challenge the other day and I quickly found a linear algorithm when he was sure it had to be quadratic. The key was that there were two collections. If you iterated through one of them, you got a quadratic solution. Through the other and it was linear.

I showed him my solution and he called it "clever" because it (in his words) "inverted the problem". That reminded me of the APL implementation of Conway's Game of Life, which was reimplemented in Clojure by Christophe Grande. Both solutions seemingly "invert the problem". Instead of iterating through all cells and and counting how many neighbors are alive, it generates the neighbors of all live cells and counts how many times the neighbor appears. It's super clever and it hinges on the reciprocal nature of the definition of neighbor in the game.

I assure you that my solution to my friend's problem was not clever and I don't have any magical way of inverting problems to find simple solutions. I just did one thing, which was to list all of the things I could iterate through, then try each one in turn. It was clear that iterating through one list would be way simpler.

What taught me to do that? I'm not sure, but I think it was Clojure! I'm not sure I can do it reliably. But the technique seemed easy enough to merit talking about. I'd love to have a list of easy perspective changes for approaching problems.

So that's my tip for today: if you know you need to iterate, but there are multiple things to iterate through, try them all. Don't be limited by the direction implied by the wording of the problem.

By the way, the problem is this week's challenge, which you can find below.

PurelyFunctional.tv Update ๐Ÿ’ฅ

Just before the quarantine, I enacted plans to discontinue new memberships to PurelyFunctional.tv. However, the timing was terrible! I planned to make individual course sales much more interesting, but I have not gotten to them yet.

I've reopened memberships to anyone who wants them. I'll discontinue them again later, with the same policy of allowing existing memberships to continue.

If you would like a membership, check out the Register page. Memberships give you access to 100 hours of video courses on Clojure, ClojureScript, and tooling.

Book update ๐Ÿ“–

Chapter 8 was just released as part of the Manning Early Access Program. Chapter 8 is all about Stratified Design, where we learn to organize our code into layers. This chapter took me a long time. It was hard for me to boil down this design skill.

You can buy the book and use the coupon code TSSIMPLICITY for 50% off.

Well, I did wind up splitting Chapter 11. I'm currently working on Chapters 10 through 13, which are all about higher-order functions.

I'm wondering how much you would enjoy getting more detailed updates about the progress of the book. Let me know.

Quarantine update ๐Ÿ˜ท

I know a lot of people are going through tougher times than I am. If you, for any reason, can't afford my courses, and you think the courses will help you, please hit reply and I will set you up. It's a small gesture I can make, but it might help.

I don't want to shame you or anybody that we should be using this time to work on our skills. The number one priority is your health and safety. I know I haven't been able to work very much, let alone learn some new skill. But if learning Clojure is important to you, and you can't afford it, just hit reply and I'll set you up. Keeping busy can keep us sane.

Stay healthy. Wash your hands. Stay at home. Wear a mask. Take care of loved ones.

Clojure Challenge ๐Ÿค”

Last week's challenge

The challenge in Issue 379 was to segment space-free strings into words. You can see the submissions here.

You can leave comments on these submissions in the gist itself. Please leave comments! There are lots of great discussions in there. You can also hit the Subscribe button to keep abreast of the comments. We're all here to learn.

And I must say that I am so happy with the discussions happening in the gist comments. People are getting fast feedback from each other and trying out multiple implementations. Check it out.

This week's challenge

Medication contraindications

You probably know this, but here's some background so we're all on the same page. Some medicines shouldn't be taken in combination. When two medications shouldn't be taken together, it's called a contraindication.

(def patient-medications [{:name "Blythenal"
                           :rxNorm "blyth"}
                          {:name "Masbutol"
                           :rxNorm "masbut"}
                          {:name "Welbutril"
                           :rxNorm "welb"}])

(def contraindication-pairs [["nr913ng" "blyth"]
                             ["masbut"  "87f2h139049f"]
                             ["nr913ng" "j1j81f0"]
                             ["blyth" "welb"]
                             ["masbut"  "welb"]])

Your task is to take the list of medications a patient has and a list of contraindication pairs, and determine what pairs of medications (if any) they are prescribed that don't mix well.

;;  complete this function
(defn contraindications [meds pairs]

It should return nil if there are no contraindications and a collection of the contraindications (pairs) if there are any.

Bonus: Make it a linear algorithm (linear in the number of the patient medications and the number of the contradiction pairs).

You can also find these same instructions here. I might update them to correct errors and clarify the de scriptions. That's also where submissions will be posted. And there's a great discussion!

As usual, please reply to this email and let me know what you tried. I'll collect them up and share them in the next issue. If you don't want me to share your submission, let me know.

Rock on!
Eric Normand