PurelyFunctional.tv Newsletter 392: command your repl
Sign up for weekly Clojure tips, software design, and a Clojure coding challenge.
Clojure Tip 💡
command your repl
Last week, I mentioned that there are three editor commands you need to know to do REPL-driven development (RDD). These three commands are all you should really need. Of course, more commands will give you more nuance. But I wanted to select the bare minimum that gives you all of the benefits of RDD.
Those commands are all evaluation commands. They differ only in what they compile. Here they are:
- Evaluate one expression
- Evaluate a top-level form
- Evaluate the whole file
You, as the programmer, have to decide which one to do at any given time. It turns out that the five most popular Clojure editors all have these three commands. I've run this by many experienced Clojure programmers and they agree that these are a minimal set.
How do you decide what command is right? My rule is pretty simple: compile the thing you are working on.
If you're modifying a function, evaluate the function (a top-level form). If you're moving stuff around the namespace, evaluate the whole file. And if you're testing out what a single expression does, evaluate that expression.
Those three commands will give you the granularity you need to use 99% of the time. I've got a lesson on how to evaluate code, which includes the 3 commands for the top-5 editors (check the Notes), which I've made free for the time being.
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.
Also, if you just want to subscribe for a paid membership, I have opened them back up for the moment. Register here.
Stay healthy. Wash your hands. Stay at home. Wear a mask. Take care of loved ones.
Clojure Challenge 🤔
Last week's challenge
I'm currently out of town so I haven't published the submissions for Issue 391. I will do it when I get back.
Please do participate in the discussion on the gist where the submissions are hosted. It's active and it's a great way to get comments on your code.
This week's challenge
If I give you a sorted vector of integers, you can search through it
quickly using binary search to know if it contains a given number
n right in the middle? If yes, you're done. If not, then you
either have to search the left half or the right half. Since the numbers
are sorted, you can check if
n is greater than or less than the middle
number. You can then recurse down into the appropriate half. Your task
is to write this function.
(binary-search 3 [1 2 3]) ;=> true (binary-search 4 [1 2 5]) ;=> false (binary-search 10 [1 2 4 5 9 10 11 12]) ;=> true
You can assume you're passed a sorted vector.
Thanks to this site for the challenge idea where it is considered Hard level in Python.
You can also find these same instructions here. I might update them to correct errors and clarify the descriptions. 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.