Tips for golfing in Dart









up vote
4
down vote

favorite












Dart is an object oriented programming language borrowing from both Java and Javascript. What general tips do you have for golfing in Dart? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Dart (e.g. "remove comments" is not an answer). Please post one tip per answer.



If a tip is similar to Java/JS, please link to the answer in the original language's thread as well if you can.



Taken mostly from Joey's Tips for Powershell










share|improve this question

























    up vote
    4
    down vote

    favorite












    Dart is an object oriented programming language borrowing from both Java and Javascript. What general tips do you have for golfing in Dart? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Dart (e.g. "remove comments" is not an answer). Please post one tip per answer.



    If a tip is similar to Java/JS, please link to the answer in the original language's thread as well if you can.



    Taken mostly from Joey's Tips for Powershell










    share|improve this question























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      Dart is an object oriented programming language borrowing from both Java and Javascript. What general tips do you have for golfing in Dart? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Dart (e.g. "remove comments" is not an answer). Please post one tip per answer.



      If a tip is similar to Java/JS, please link to the answer in the original language's thread as well if you can.



      Taken mostly from Joey's Tips for Powershell










      share|improve this question













      Dart is an object oriented programming language borrowing from both Java and Javascript. What general tips do you have for golfing in Dart? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Dart (e.g. "remove comments" is not an answer). Please post one tip per answer.



      If a tip is similar to Java/JS, please link to the answer in the original language's thread as well if you can.



      Taken mostly from Joey's Tips for Powershell







      code-golf tips dart






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 20 hours ago









      Elcan

      27115




      27115




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote













          Variable declaration



          Regular variable declaration takes 4 bytes (var) + 2 bytes/variable (i,).
          You can declare variables as optional named parameters for your function and shave a few bytes.



          f()var i,j,k; //Takes 15 bytes
          g(i,j,k) //Takes 12 bytes


          Note: This doesn't work for non-constant values, for example:



          f(i=List) //Works
          g(i=) //Doesn't work

          f(i=0,j=0) //Works
          g(i=0,j=i) //Doesn't work





          share|improve this answer



























            up vote
            2
            down vote













            Implicit parameter passing



            In some cases where Dart expects a function to be declared, you can code the function elsewhere, and then just pass its name. Dart will take care of passing the value automatically. Let me illustrate :



            [0,1,2,3,4,5].forEach((i) => print(i)); //Using a lambda
            [0,1,2,3,4,5].forEach(print); //Using implicit parameter passing since print() expects a similar parameter


            You can also use your own functions



            f(List i)
            i.forEach(print); //Prints each number on a new line


            [[0,1], [1,2]].forEach(f); //Prints 0 n 1 n 1 n 2n


            If you know a lambda might be used in multiple places, for example a map(), it can be useful to make it its own function and pass it that way instead of declaring it multiple times.






            share|improve this answer




















            • Is this not just a consequence of first-class functions? (I don't know Dart)
              – Quelklef
              15 hours ago











            • Looks like it's the case. I didn't know this was more widely spread dartlang.org/guides/language/…
              – Elcan
              15 hours ago


















            up vote
            1
            down vote













            String conversion and concatenation



            Dart doesn't allow for concatenation for types other than String and doesn't implicitly convert to String like C# does for example.



            It however includes a very useful way to concatenate variables without needing to use any addition operator or explicitly casting to String (using the .toString() method).



            var i=0,j=1,k=2;
            var s0='$j'; //'1'
            var s1='$i$j$k'; //'012'
            var s2='i=$i'; //'i=0'


            You can also perform operations directly in the $ section and save a few temporary variable declarations.



            var i=0,j=1,k=2;
            var s='$i+j+k'; //'3'





            share|improve this answer






















              Your Answer





              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "200"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175574%2ftips-for-golfing-in-dart%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              Variable declaration



              Regular variable declaration takes 4 bytes (var) + 2 bytes/variable (i,).
              You can declare variables as optional named parameters for your function and shave a few bytes.



              f()var i,j,k; //Takes 15 bytes
              g(i,j,k) //Takes 12 bytes


              Note: This doesn't work for non-constant values, for example:



              f(i=List) //Works
              g(i=) //Doesn't work

              f(i=0,j=0) //Works
              g(i=0,j=i) //Doesn't work





              share|improve this answer
























                up vote
                2
                down vote













                Variable declaration



                Regular variable declaration takes 4 bytes (var) + 2 bytes/variable (i,).
                You can declare variables as optional named parameters for your function and shave a few bytes.



                f()var i,j,k; //Takes 15 bytes
                g(i,j,k) //Takes 12 bytes


                Note: This doesn't work for non-constant values, for example:



                f(i=List) //Works
                g(i=) //Doesn't work

                f(i=0,j=0) //Works
                g(i=0,j=i) //Doesn't work





                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Variable declaration



                  Regular variable declaration takes 4 bytes (var) + 2 bytes/variable (i,).
                  You can declare variables as optional named parameters for your function and shave a few bytes.



                  f()var i,j,k; //Takes 15 bytes
                  g(i,j,k) //Takes 12 bytes


                  Note: This doesn't work for non-constant values, for example:



                  f(i=List) //Works
                  g(i=) //Doesn't work

                  f(i=0,j=0) //Works
                  g(i=0,j=i) //Doesn't work





                  share|improve this answer












                  Variable declaration



                  Regular variable declaration takes 4 bytes (var) + 2 bytes/variable (i,).
                  You can declare variables as optional named parameters for your function and shave a few bytes.



                  f()var i,j,k; //Takes 15 bytes
                  g(i,j,k) //Takes 12 bytes


                  Note: This doesn't work for non-constant values, for example:



                  f(i=List) //Works
                  g(i=) //Doesn't work

                  f(i=0,j=0) //Works
                  g(i=0,j=i) //Doesn't work






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 20 hours ago









                  Elcan

                  27115




                  27115




















                      up vote
                      2
                      down vote













                      Implicit parameter passing



                      In some cases where Dart expects a function to be declared, you can code the function elsewhere, and then just pass its name. Dart will take care of passing the value automatically. Let me illustrate :



                      [0,1,2,3,4,5].forEach((i) => print(i)); //Using a lambda
                      [0,1,2,3,4,5].forEach(print); //Using implicit parameter passing since print() expects a similar parameter


                      You can also use your own functions



                      f(List i)
                      i.forEach(print); //Prints each number on a new line


                      [[0,1], [1,2]].forEach(f); //Prints 0 n 1 n 1 n 2n


                      If you know a lambda might be used in multiple places, for example a map(), it can be useful to make it its own function and pass it that way instead of declaring it multiple times.






                      share|improve this answer




















                      • Is this not just a consequence of first-class functions? (I don't know Dart)
                        – Quelklef
                        15 hours ago











                      • Looks like it's the case. I didn't know this was more widely spread dartlang.org/guides/language/…
                        – Elcan
                        15 hours ago















                      up vote
                      2
                      down vote













                      Implicit parameter passing



                      In some cases where Dart expects a function to be declared, you can code the function elsewhere, and then just pass its name. Dart will take care of passing the value automatically. Let me illustrate :



                      [0,1,2,3,4,5].forEach((i) => print(i)); //Using a lambda
                      [0,1,2,3,4,5].forEach(print); //Using implicit parameter passing since print() expects a similar parameter


                      You can also use your own functions



                      f(List i)
                      i.forEach(print); //Prints each number on a new line


                      [[0,1], [1,2]].forEach(f); //Prints 0 n 1 n 1 n 2n


                      If you know a lambda might be used in multiple places, for example a map(), it can be useful to make it its own function and pass it that way instead of declaring it multiple times.






                      share|improve this answer




















                      • Is this not just a consequence of first-class functions? (I don't know Dart)
                        – Quelklef
                        15 hours ago











                      • Looks like it's the case. I didn't know this was more widely spread dartlang.org/guides/language/…
                        – Elcan
                        15 hours ago













                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      Implicit parameter passing



                      In some cases where Dart expects a function to be declared, you can code the function elsewhere, and then just pass its name. Dart will take care of passing the value automatically. Let me illustrate :



                      [0,1,2,3,4,5].forEach((i) => print(i)); //Using a lambda
                      [0,1,2,3,4,5].forEach(print); //Using implicit parameter passing since print() expects a similar parameter


                      You can also use your own functions



                      f(List i)
                      i.forEach(print); //Prints each number on a new line


                      [[0,1], [1,2]].forEach(f); //Prints 0 n 1 n 1 n 2n


                      If you know a lambda might be used in multiple places, for example a map(), it can be useful to make it its own function and pass it that way instead of declaring it multiple times.






                      share|improve this answer












                      Implicit parameter passing



                      In some cases where Dart expects a function to be declared, you can code the function elsewhere, and then just pass its name. Dart will take care of passing the value automatically. Let me illustrate :



                      [0,1,2,3,4,5].forEach((i) => print(i)); //Using a lambda
                      [0,1,2,3,4,5].forEach(print); //Using implicit parameter passing since print() expects a similar parameter


                      You can also use your own functions



                      f(List i)
                      i.forEach(print); //Prints each number on a new line


                      [[0,1], [1,2]].forEach(f); //Prints 0 n 1 n 1 n 2n


                      If you know a lambda might be used in multiple places, for example a map(), it can be useful to make it its own function and pass it that way instead of declaring it multiple times.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 19 hours ago









                      Elcan

                      27115




                      27115











                      • Is this not just a consequence of first-class functions? (I don't know Dart)
                        – Quelklef
                        15 hours ago











                      • Looks like it's the case. I didn't know this was more widely spread dartlang.org/guides/language/…
                        – Elcan
                        15 hours ago

















                      • Is this not just a consequence of first-class functions? (I don't know Dart)
                        – Quelklef
                        15 hours ago











                      • Looks like it's the case. I didn't know this was more widely spread dartlang.org/guides/language/…
                        – Elcan
                        15 hours ago
















                      Is this not just a consequence of first-class functions? (I don't know Dart)
                      – Quelklef
                      15 hours ago





                      Is this not just a consequence of first-class functions? (I don't know Dart)
                      – Quelklef
                      15 hours ago













                      Looks like it's the case. I didn't know this was more widely spread dartlang.org/guides/language/…
                      – Elcan
                      15 hours ago





                      Looks like it's the case. I didn't know this was more widely spread dartlang.org/guides/language/…
                      – Elcan
                      15 hours ago











                      up vote
                      1
                      down vote













                      String conversion and concatenation



                      Dart doesn't allow for concatenation for types other than String and doesn't implicitly convert to String like C# does for example.



                      It however includes a very useful way to concatenate variables without needing to use any addition operator or explicitly casting to String (using the .toString() method).



                      var i=0,j=1,k=2;
                      var s0='$j'; //'1'
                      var s1='$i$j$k'; //'012'
                      var s2='i=$i'; //'i=0'


                      You can also perform operations directly in the $ section and save a few temporary variable declarations.



                      var i=0,j=1,k=2;
                      var s='$i+j+k'; //'3'





                      share|improve this answer


























                        up vote
                        1
                        down vote













                        String conversion and concatenation



                        Dart doesn't allow for concatenation for types other than String and doesn't implicitly convert to String like C# does for example.



                        It however includes a very useful way to concatenate variables without needing to use any addition operator or explicitly casting to String (using the .toString() method).



                        var i=0,j=1,k=2;
                        var s0='$j'; //'1'
                        var s1='$i$j$k'; //'012'
                        var s2='i=$i'; //'i=0'


                        You can also perform operations directly in the $ section and save a few temporary variable declarations.



                        var i=0,j=1,k=2;
                        var s='$i+j+k'; //'3'





                        share|improve this answer
























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          String conversion and concatenation



                          Dart doesn't allow for concatenation for types other than String and doesn't implicitly convert to String like C# does for example.



                          It however includes a very useful way to concatenate variables without needing to use any addition operator or explicitly casting to String (using the .toString() method).



                          var i=0,j=1,k=2;
                          var s0='$j'; //'1'
                          var s1='$i$j$k'; //'012'
                          var s2='i=$i'; //'i=0'


                          You can also perform operations directly in the $ section and save a few temporary variable declarations.



                          var i=0,j=1,k=2;
                          var s='$i+j+k'; //'3'





                          share|improve this answer














                          String conversion and concatenation



                          Dart doesn't allow for concatenation for types other than String and doesn't implicitly convert to String like C# does for example.



                          It however includes a very useful way to concatenate variables without needing to use any addition operator or explicitly casting to String (using the .toString() method).



                          var i=0,j=1,k=2;
                          var s0='$j'; //'1'
                          var s1='$i$j$k'; //'012'
                          var s2='i=$i'; //'i=0'


                          You can also perform operations directly in the $ section and save a few temporary variable declarations.



                          var i=0,j=1,k=2;
                          var s='$i+j+k'; //'3'






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 19 hours ago

























                          answered 20 hours ago









                          Elcan

                          27115




                          27115



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175574%2ftips-for-golfing-in-dart%23new-answer', 'question_page');

                              );

                              Post as a guest














































































                              Popular posts from this blog

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo