Bonneville
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
Maven (famous)@lemmy.zip to Programmer Humor@programming.dev · 16 days ago

Modern Programming

lemmy.zip

message-square
16
link
fedilink
1

Modern Programming

lemmy.zip

Maven (famous)@lemmy.zip to Programmer Humor@programming.dev · 16 days ago
message-square
16
link
fedilink
alert-triangle
You must log in or # to comment.
  • Digital Mark@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    2
    ·
    16 days ago

    print( ["even", "odd"][num % 2] )

    If you need to avoid evaluating the wrong branch:

    print( [lambda: "even", lambda: "odd"][num % 2]() )

    • FireIced@lemmy.super.ynh.fr
      link
      fedilink
      arrow-up
      1
      ·
      16 days ago

      Cursed

      • Gamma@programming.dev
        link
        fedilink
        English
        arrow-up
        2
        ·
        15 days ago

        Not as cursed as

        print("eovdedn"[n%2::2]) 
        
  • entropicdrift@lemmy.sdf.org
    link
    fedilink
    arrow-up
    1
    ·
    16 days ago
    print("odd" if num % 2 else "even")
    

    That’s the native python version, for those curious

    • DreadPirateShawn@lemmy.blahaj.zone
      link
      fedilink
      arrow-up
      0
      ·
      16 days ago

      The ternary syntax is really my only real gripe with python design – putting the conditional BETWEEN the true and false values feels so very messy to me.

      • balsoft@lemmy.ml
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        16 days ago

        Eh, reads pretty naturally to me. That said, (like I lisp)

    • Kaboom@reddthat.com
      link
      fedilink
      arrow-up
      0
      arrow-down
      1
      ·
      16 days ago

      Why is the return first?

  • Ephera@lemmy.ml
    link
    fedilink
    English
    arrow-up
    1
    ·
    16 days ago

    For optimal performance, you should rewrite it in Rust:

    inline_python::python! {
        print(js2py.eval_js("(number) => number % 2 ? 'odd' : 'even'")(number))
    };
    
    • ATPA9@feddit.org
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      16 days ago

      And now you can use wasm to run it in a browser!

  • SpaceNoodle@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    16 days ago

    Please. That’s C’s ternary operator. JS is just a pile of garbage cosplaying as a programming language

    • Victor@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      16 days ago

      Why do you say it’s a pile of garbage?

      • SpaceNoodle@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        16 days ago

        Because of all the garbage

        • Fillicia@sh.itjust.works
          link
          fedilink
          arrow-up
          1
          ·
          15 days ago

          Clearly the garbage collector is too effective

          • filcuk@lemmy.zip
            link
            fedilink
            arrow-up
            1
            ·
            15 days ago

            No they’re not supposed to be piling it up

  • kn0wmad1c@programming.dev
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    16 days ago

    num % 2 isn’t a boolean result in any of these languages, so I feel like it would always output “odd”

    Edit: 0 is false, everything else is true.

    • Kaboom@reddthat.com
      link
      fedilink
      arrow-up
      0
      arrow-down
      1
      ·
      16 days ago

      0 is false in C, Python, and JS. It should work

Programmer Humor@programming.dev

programmer_humor@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmer_humor@programming.dev

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 2K users / day
  • 2.86K users / week
  • 2.92K users / month
  • 2.94K users / 6 months
  • 1 local subscriber
  • 24.5K subscribers
  • 86 Posts
  • 471 Comments
  • Modlog
  • mods:
  • adr1an@programming.dev
  • Feyter@programming.dev
  • BurningTurtle@programming.dev
  • Pierre-Yves Lapersonne@programming.dev
  • BE: 0.19.12
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org