this post was submitted on 15 Jul 2025
455 points (94.9% liked)

Programmer Humor

37227 readers
101 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Aedis@lemmy.world 32 points 2 days ago (1 children)

I'm partial to a recursive solution. Lol

def is_even(number):
    if number < 0 or (number%1) > 0:
        raise ValueError("This impl requires positive integers only") 
    if number < 2:
        return number
    return is_even(number - 2)
[–] tetris11@lemmy.ml 18 points 2 days ago* (last edited 2 days ago) (3 children)

I prefer good ole regex test of a binary num

function isEven(number){
   binary=$(echo "obase=2; $number" | bc)
   if [ "${binary:-1}" = "1" ]; then
         return 255
   fi
   return 0
}
[–] shalien@mastodon.projetretro.io 10 points 2 days ago (2 children)
[–] Chais@sh.itjust.works 4 points 2 days ago* (last edited 2 days ago)

If your codebase is closed source there's no risk of that happening, if it's open source there's nothing you can do about it.
Either way there's no use worrying.

[–] barubary@infosec.exchange 5 points 2 days ago

@tetris11 @Aedis More like:

isEven() {    case "$1" in        *[02468]) return 0;;        *) return 1;;    esac;}

(If all the line breaks are gone from this code snippet, blame Lemmy. It looks fine here.)

[–] balsoft@lemmy.ml 8 points 2 days ago* (last edited 2 days ago) (2 children)

Amateur! I can read and understand that almost right away. Now I present a better solution:

even() ((($1+1)&1))

~~(I mean, it's funny cause it's unreadable, but I suspect this is also one of the most efficient bash implementations possible)~~

(Actually the obvious one is a slight bit faster. But this impl for odd is the fastest one as far as I can tell odd() (($1&1)))

[–] Aedis@lemmy.world 2 points 2 days ago (1 children)

I'm waiting for a code golf style solution now.

[–] balsoft@lemmy.ml 2 points 1 day ago

I don't think there's much to codegolf. The "obvious" solution (even() (($1%2))) is both shorter and faster. Don't think it can be optimized much more.

[–] tetris11@lemmy.ml 3 points 2 days ago (1 children)

woah your bash is legit good. I thought numeric pretexts needed $(( blah )), but you're ommiting the $ like an absolute madman. How is this wizardy possible

[–] balsoft@lemmy.ml 3 points 2 days ago (1 children)

See: man bash, "Compound Commands" and "Shell Function Definitions"

[–] tetris11@lemmy.ml 4 points 2 days ago (1 children)

Oh I see it, but for some reason I was taught to always use $(( arith )) instead of (( arith )) and I guess I'm just wondering what the difference is

[–] balsoft@lemmy.ml 7 points 2 days ago* (last edited 2 days ago) (1 children)

The difference is that (( is a "compound command", similar to [[ (evaluate conditional expression), while $(( )) is "aritmetic expansion". They behave in almost exactly the same way but are used in different contexts - the former uses "exit codes" while the latter returns a string, so the former would be used where you would expect a command, while the latter would be used where you expect an expression. A function definition expects a compound command, so that's what we use. If we used $(( )) directly, it wouldn't parse:

$ even() $((($1+1)&1))
bash: syntax error near unexpected token `$((($1+1)&1))'

We would have to do something like

even() { return $(($1&1)); }

(notice how this is inverted from the (( case - (( actually inverts 0 -> exit code 1 and any other result to exit code 0, so that it matches bash semantics of exit code 0 being "true" and any other exit code being "false" when used in a conditional)

But this is a bit easier to understand and as such wouldn't cut it, as any seasoned bash expert will tell you. Can't be irreplaceable if anyone on your team can read your code, right?

This (( vs. $(( )) thing is similar to how there is ( compound command (run in a subshell), and $( ) (command substitution). You can actually use the former to define a function too (as it's a compound command):

real_exit() { exit 1; }
fake_exit() ( exit 1 )

Calling real_exit will exit from the shell, while calling fake_exit will do nothing as the exit 1 command is executed in a separate subshell. Notice how you can also do the same in a command substition (because it runs in a subshell too):

echo $(echo foo; exit 1)

Will run successfully and output foo.

(( being paired with $((, and ( with $( is mostly just a syntactic rhyme rather than anything consistent. E.g. { and ${ do very different things, and $[[ just doesn't exist.

It is another one of those unknown, very rarely useful features of bash.

[–] tetris11@lemmy.ml 2 points 1 day ago

amazing, thanks!