←back to thread

498 points azhenley | 4 comments | | HN request time: 0.612s | source
1. QuadrupleA ◴[] No.45773004[source]
Love Carmack, but hard disagree on this and a lot of similar functional programming dogma. I find this type of thing very helpful:

    classList = ['highlighted', 'primary']
    if discount:
        classList.append('on-sale')
    classList = ' '.join(classList)
And not having to think about e.g. `const` vs `let` frees up needless cognitive load, which is why I think python (rightly) chose to not make it an option.
replies(3): >>45774119 #>>45776812 #>>45776933 #
2. ColeShepherd ◴[] No.45774119[source]
Some potential alternatives to consider:

1.

    classList = ['highlighted', 'primary']
        .concatif(discount, 'on-sale')
        .join(' ')
2.

    classList = ' '.join(['highlighted', 'primary'] + (['on-sale'] if discount else []))
3.

    mut classList = ['highlighted', 'primary']
    if discount:
        classList.append('on-sale')
    classList = ' '.join(classList)

    freeze classList

4.

    def get_class_list(discount):
        mut classList = ['highlighted', 'primary']
        if discount:
            classList.append('on-sale')
        classList = ' '.join(classList)
        return classList

    classList = get_class_list(discount)
3. salutis ◴[] No.45776812[source]
Fennel (Lisp):

    (table.concat [:highlighted :primary (if discount :on-sale)] " ")
4. ◴[] No.45776933[source]