Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Parallel Assignments and Fuze

    Beginners
    3
    5
    251
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • AlienDjinn
      AlienDjinn last edited by AlienDjinn

      Posting this here for other beginners in case it helps.

      I was working on a crypto program and trying to port an example from Python to handle GCD (Greatest Common Divisor) when I hit snag with my coding skills. Python has a thing called 'parallel assignment' and it's magical powers look like this:

      def gcd(a,b)
      // return the GCD for a and b using Euclid's algorithm.  
           while a != 0:
                a, b = b % a, a
           return b
      

      Ok, that looks easy but conceptually stumped me. I got an alternate version to work, but it used recursion and was also clear as mud (turn on debug to see that in action below).

      function gcd(a, b)
            if  b == 0  then 
                //print("DEBUG: L1 :  a  =  ", a, "  b  =  ", b, "\n") 
                a = a
            else 
                //print("DEBUG: L1 :  a  =  ",  a, "  b  =  ", b, "\n") 
                a = gcd(b, a % b)
            endif
      return a
      

      I wanted to try porting the original Python version again and finally got this to work. Thanks to a little help from https://en.wikipedia.org/wiki/Assignment_(computer_science)

      function gcd(a, b)
            int  t 
            while b != 0  loop
                  //print("DEBUG: L1 :  a  =  ", a, "  b  =  ", b, "\n") 
                  t = a
                  a = b % a
                  b = t
           repeat
      return a
      

      Question: is this the preferred way to approach the concept of parallel assignment in Fuze? I'm asking because I feel like others will see examples and possibly land down this rabbit hole as well.

      Apologies if this is a dumb question. Thanks for the advice.

      -AlienDjinn

      1 Reply Last reply Reply Quote 2
      • vinicity
        vinicity F last edited by vinicity

        I’m no expert, but I think your solution looks reasonable. Just one question: in the python example you loop while a != 0, but your code loops while b != 0. Any reason for that? It seems like it could potentially affect the answer depending on a and b’s initial value.

        Edit: OK, now I noticed that you return a instead of b, which confuses me further... But If the function returns the correct values, then I guess it works?

        AlienDjinn 1 Reply Last reply Reply Quote 1
        • AlienDjinn
          AlienDjinn @vinicity last edited by AlienDjinn

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • Martin
            Martin Fuze Team last edited by Martin

            I had to just lookup what parallel assignment even was!!

            So, Fuze doesn't support it directly but if I'm understanding the article correctly then wouldn't this work?

            function gcd(a,b)
                // return the GCD for a and b using Euclid's algorithm.  
                while a != 0 loop
                    var t = a
                    a = b % a
                    b = t
                 repeat
            return b
            

            I haven't tested it - and, note, it's almost identical to your verstion

            AlienDjinn 1 Reply Last reply Reply Quote 1
            • AlienDjinn
              AlienDjinn @Martin last edited by

              @Martin @vinicity thanks for catching my transposition error from the original version. I tested that version and you're correct it works as you describe.

              Well, just wanted to start this thread for others that happen along that pattern as well. Thanks!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post