zlacker

[parent] [thread] 14 comments
1. exitb+(OP)[view] [source] 2024-08-27 10:28:50
This is the function that confused the person you respond to, ported to Python:

    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.
replies(8): >>mirolj+h4 >>JaumeG+ne >>freila+zk >>itisha+XM >>hyperh+CR >>jdminh+vU >>Zak+1V >>miller+IC3
2. mirolj+h4[view] [source] 2024-08-27 11:17:54
>>exitb+(OP)
> 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.

replies(1): >>auggie+l5
◧◩
3. auggie+l5[view] [source] [discussion] 2024-08-27 11:26:43
>>mirolj+h4
And yet we've all done it.
replies(1): >>mirolj+ld
◧◩◪
4. mirolj+ld[view] [source] [discussion] 2024-08-27 12:36:04
>>auggie+l5
No. We didn't. At least not we all.

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.

replies(2): >>auggie+o51 >>tekkni+eC2
5. JaumeG+ne[view] [source] 2024-08-27 12:43:36
>>exitb+(OP)
That's because you are more familiar with whatever style of code you are used to.

Don't confuse familiarity with readability.

6. freila+zk[view] [source] 2024-08-27 13:28:56
>>exitb+(OP)
> I know which one I'd prefer to grok at 2AM with alerts going off.

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.

replies(1): >>zikzak+Lp
◧◩
7. zikzak+Lp[view] [source] [discussion] 2024-08-27 14:01:39
>>freila+zk
I was in a "101" undergrad compsci class the first year the program used Java (1997, I think?) and so this asst prof was showing a simple example of some Java syntax.

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.

8. itisha+XM[view] [source] 2024-08-27 16:07:34
>>exitb+(OP)
Honestly both read about the same to me, and I'm largely unfamiliar with Clojure. The main difference appears to be the 3 `str` callouts, which appear extraneous as the following version works just the same:

    (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.
replies(1): >>kazina+Q61
9. hyperh+CR[view] [source] 2024-08-27 16:27:29
>>exitb+(OP)
Having written a wordle clone recently, this produces the wrong result, by the way. For example guess SASSY with the answer STICK.
10. jdminh+vU[view] [source] 2024-08-27 16:38:31
>>exitb+(OP)
I do definitely get more 2AM alerts going off when I work with Python, so it's got that going for it.
11. Zak+1V[view] [source] 2024-08-27 16:40:42
>>exitb+(OP)
And here's what cond could look like in Python syntax:

    def color(word, letter, idx):
        cond:
            word[idx] == letter: return GREEN
            letter in word: return YELLOW
            True: return GREY
◧◩◪◨
12. auggie+o51[view] [source] [discussion] 2024-08-27 17:23:37
>>mirolj+ld
I'll invoke a no true Scotsman argument here.
◧◩
13. kazina+Q61[view] [source] [discussion] 2024-08-27 17:30:58
>>itisha+XM
I'm guessing that str allow it to work when the inputs are symbols? So that they are compared as strings rather than by identity. There could be more than one symbol named "foo"; if you want those to compare the same, you can't use regular symbol equality.

Or possibly the code even uses non-symbols for some of the arguments. Suppose that letter is sometimes the integer 1.

◧◩◪◨
14. tekkni+eC2[view] [source] [discussion] 2024-08-28 04:58:48
>>mirolj+ld
note how they said grok and not work? this is what oncall looks like, reading code at 2 AM
15. miller+IC3[view] [source] 2024-08-28 14:32:51
>>exitb+(OP)
Kotlin time (since we're in the JVM context for Clojure)

    fun color(word: String, letter: Char, idx: Int) =
      when (letter) {
        word[idx] -> GREEN
        in word -> YELLOW
        else -> GRAY
      }
[go to top]