def color(word, letter, idx):
if word[idx] == letter:
return GREEN
elif letter in word:
return YELLOW
else:
return GREY
I know which one I'd prefer to grok at 2AM with alerts going off.At that time I'd just opt for sleep. Or sex. Or drink. Reading code doesn't belong to things one should do at 2AM.
That's just a myth spread by a few workaholic programmers. Luckily, there are enough 9-5 programmers to clean up the mess created by those 2AM committers.
Don't confuse familiarity with readability.
I hate meaningless statements like this. This means nothing, other maybe that you know Python. 20 years ago people might have said that about Python - I even know many people today who would say that about Python.
I had been programming in C for a while, learning from K&R, to build ray tracing input files and that sort of thing so I was kind of disappointed but whatever, I was a mature student who had rediscovered computers a couple of years before (had a C64 in the 80s) and was just happy to be there.
Anyway, this guy in the back yells out "I could do that in 2 lines of Q-BASIC" or something to that effect (Q-BASIC was definitely part of his pithy one-liner). Little did I know he was representing so many of the people I would encounter over the next decades.
(defn color [word letter idx]
(cond
(= (nth word idx) letter) :green
(str/includes? word letter) :yellow
:else :gray))
Interesting that even with the `str` callouts removed, the function still appears to work on other datatypes such as: (def s (seq ("test1"))
A lazy sequence, but one Clojure still allows to be indexed over in O(1) time. That's probably what the `str` conversion was trying to speed up.Python, meanwhile, fails on lazy input as it isn't indexable.
word = (c for c in "test1")
I guess I'll be checking out Clojure this weekend. def color(word, letter, idx):
cond:
word[idx] == letter: return GREEN
letter in word: return YELLOW
True: return GREYOr possibly the code even uses non-symbols for some of the arguments. Suppose that letter is sometimes the integer 1.
fun color(word: String, letter: Char, idx: Int) =
when (letter) {
word[idx] -> GREEN
in word -> YELLOW
else -> GRAY
}