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

Modern Programming

lemmy.zip

message-square
29
link
fedilink
6

Modern Programming

lemmy.zip

Maven (famous)@lemmy.zip to Programmer Humor@programming.dev · 24 days ago
message-square
29
link
fedilink
alert-triangle
You must log in or # to comment.
  • Digital Mark@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    3
    ·
    23 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
      ·
      23 days ago

      Cursed

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

        Not as cursed as

        print("eovdedn"[n%2::2]) 
        
  • Ephera@lemmy.ml
    link
    fedilink
    English
    arrow-up
    1
    ·
    23 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
      23 days ago

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

  • entropicdrift@lemmy.sdf.org
    link
    fedilink
    arrow-up
    1
    ·
    24 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
      1
      ·
      23 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.

      • idunnololz@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        23 days ago

        At least you guys have ternary syntax cries in kotlin.

        • aubeynarf@lemmynsfw.com
          link
          fedilink
          arrow-up
          1
          ·
          7 days ago

          in Scala, everything is an expression, including “if”, maybe kotlin is the same?

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

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

        • Shareni@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          23 days ago

          Lisps makes more sense to me though

          (if condition a b)

          VS

          a if condition else b

        • rovingnothing29@lemmy.world
          link
          fedilink
          arrow-up
          0
          arrow-down
          1
          ·
          23 days ago

          Oh, (you) (really) (like) (Lisp)? (That’s) (great!)

      • Rob Bos@lemmy.ca
        link
        fedilink
        English
        arrow-up
        0
        ·
        23 days ago

        It’s kinda natural to me having used Perl a lot.

        • SpaceNoodle@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          23 days ago

          That’s not quite the argument you might think it is

          • Rob Bos@lemmy.ca
            link
            fedilink
            English
            arrow-up
            0
            ·
            23 days ago

            Argument?

            • SpaceNoodle@lemmy.world
              link
              fedilink
              arrow-up
              0
              ·
              23 days ago

              You know, the stuff in @_

        • l3mming@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          23 days ago

          You clearly haven’t used Perl a lot. Perl’s ternary looks like:

          $even = $num % 2 ? “nay” : “yay”;

          Incidentally, it is also the same as PHP’s, but mainly because PHP stole it.

          • psud@aussie.zone
            link
            fedilink
            English
            arrow-up
            0
            ·
            23 days ago

            You do get the if in the middle of stuff though in the form print(debug message) if $debug

            • palordrolap@fedia.io
              link
              fedilink
              arrow-up
              0
              ·
              23 days ago

              Wait until you learn that postfix conditionals are syntactic sugar and the compiler* turns that line into the equivalent of $debug and print(debug message), putting the conditional in first place, a lot like the ternary operator.

              * Perl compiles to bytecode before running.

              The ternary operator itself isn’t implemented in terms of and (and or) but it could be.

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

      Why is the return first?

  • SpaceNoodle@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    24 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
      ·
      23 days ago

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

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

        Because of all the garbage

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

          Clearly the garbage collector is too effective

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

            No they’re not supposed to be piling it up

            • piccolo@sh.itjust.works
              link
              fedilink
              arrow-up
              1
              ·
              23 days ago

              Is a garbage collector not a garbage disposal. Smh.

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

          One example that’s giving you problems? Maybe even on a daily basis if you use it for work? What’s garbage about it?

  • kn0wmad1c@programming.dev
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    23 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
      ·
      23 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: [email protected]

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.

  • 1.08K users / day
  • 3.18K users / week
  • 5.57K users / month
  • 5.64K users / 6 months
  • 1 local subscriber
  • 24.8K subscribers
  • 234 Posts
  • 1.75K Comments
  • Modlog
  • mods:
  • adr1an@programming.dev
  • Feyter@programming.dev
  • BurningTurtle@programming.dev
  • Pierre-Yves Lapersonne@programming.dev
  • BE: 0.19.12
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org