Purpose of repeat function












3















Using kotlin I can repeat an action in at least two ways:



val times = 5

// First option
for (i in 0 until times) {
print("Action $i")
}

// Second option
repeat(times) {
print("Action $it")
}


I'd like to know the purpose of repeat.




  • Should the traditional for loop be replaced with repeat function if possible?

  • Or are there special cases for this function?

  • Are there any advantages in repeat function?




EDIT



I've made some research about this question. As long as kotlin is open source project, I could download the sources and check git history.



I found that



1) repeat function is a replace for times function extension.



public inline fun Int.times(body : () -> Unit)


2) KT-7074. times function has become deprecated. But why?










share|improve this question




















  • 1





    The equivalent of repeat(times) is for (i in 0 until times) and not for (i in 0..times)

    – forpas
    Dec 29 '18 at 11:26











  • It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can use repeat(numPoints) { points += parsePosition(stream) } to parse them all. I could have used a for but the repeat was shorter and I thought it read better.

    – hudsonb
    Dec 30 '18 at 13:17











  • Funny thing: readCount times { rl.unlock() } - this is the usage of times extension in 2015. No dot notation.

    – Axel P
    Dec 30 '18 at 14:52
















3















Using kotlin I can repeat an action in at least two ways:



val times = 5

// First option
for (i in 0 until times) {
print("Action $i")
}

// Second option
repeat(times) {
print("Action $it")
}


I'd like to know the purpose of repeat.




  • Should the traditional for loop be replaced with repeat function if possible?

  • Or are there special cases for this function?

  • Are there any advantages in repeat function?




EDIT



I've made some research about this question. As long as kotlin is open source project, I could download the sources and check git history.



I found that



1) repeat function is a replace for times function extension.



public inline fun Int.times(body : () -> Unit)


2) KT-7074. times function has become deprecated. But why?










share|improve this question




















  • 1





    The equivalent of repeat(times) is for (i in 0 until times) and not for (i in 0..times)

    – forpas
    Dec 29 '18 at 11:26











  • It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can use repeat(numPoints) { points += parsePosition(stream) } to parse them all. I could have used a for but the repeat was shorter and I thought it read better.

    – hudsonb
    Dec 30 '18 at 13:17











  • Funny thing: readCount times { rl.unlock() } - this is the usage of times extension in 2015. No dot notation.

    – Axel P
    Dec 30 '18 at 14:52














3












3








3


0






Using kotlin I can repeat an action in at least two ways:



val times = 5

// First option
for (i in 0 until times) {
print("Action $i")
}

// Second option
repeat(times) {
print("Action $it")
}


I'd like to know the purpose of repeat.




  • Should the traditional for loop be replaced with repeat function if possible?

  • Or are there special cases for this function?

  • Are there any advantages in repeat function?




EDIT



I've made some research about this question. As long as kotlin is open source project, I could download the sources and check git history.



I found that



1) repeat function is a replace for times function extension.



public inline fun Int.times(body : () -> Unit)


2) KT-7074. times function has become deprecated. But why?










share|improve this question
















Using kotlin I can repeat an action in at least two ways:



val times = 5

// First option
for (i in 0 until times) {
print("Action $i")
}

// Second option
repeat(times) {
print("Action $it")
}


I'd like to know the purpose of repeat.




  • Should the traditional for loop be replaced with repeat function if possible?

  • Or are there special cases for this function?

  • Are there any advantages in repeat function?




EDIT



I've made some research about this question. As long as kotlin is open source project, I could download the sources and check git history.



I found that



1) repeat function is a replace for times function extension.



public inline fun Int.times(body : () -> Unit)


2) KT-7074. times function has become deprecated. But why?







kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 14:51







Axel P

















asked Dec 29 '18 at 11:10









Axel PAxel P

1,68121225




1,68121225








  • 1





    The equivalent of repeat(times) is for (i in 0 until times) and not for (i in 0..times)

    – forpas
    Dec 29 '18 at 11:26











  • It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can use repeat(numPoints) { points += parsePosition(stream) } to parse them all. I could have used a for but the repeat was shorter and I thought it read better.

    – hudsonb
    Dec 30 '18 at 13:17











  • Funny thing: readCount times { rl.unlock() } - this is the usage of times extension in 2015. No dot notation.

    – Axel P
    Dec 30 '18 at 14:52














  • 1





    The equivalent of repeat(times) is for (i in 0 until times) and not for (i in 0..times)

    – forpas
    Dec 29 '18 at 11:26











  • It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can use repeat(numPoints) { points += parsePosition(stream) } to parse them all. I could have used a for but the repeat was shorter and I thought it read better.

    – hudsonb
    Dec 30 '18 at 13:17











  • Funny thing: readCount times { rl.unlock() } - this is the usage of times extension in 2015. No dot notation.

    – Axel P
    Dec 30 '18 at 14:52








1




1





The equivalent of repeat(times) is for (i in 0 until times) and not for (i in 0..times)

– forpas
Dec 29 '18 at 11:26





The equivalent of repeat(times) is for (i in 0 until times) and not for (i in 0..times)

– forpas
Dec 29 '18 at 11:26













It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can use repeat(numPoints) { points += parsePosition(stream) } to parse them all. I could have used a for but the repeat was shorter and I thought it read better.

– hudsonb
Dec 30 '18 at 13:17





It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can use repeat(numPoints) { points += parsePosition(stream) } to parse them all. I could have used a for but the repeat was shorter and I thought it read better.

– hudsonb
Dec 30 '18 at 13:17













Funny thing: readCount times { rl.unlock() } - this is the usage of times extension in 2015. No dot notation.

– Axel P
Dec 30 '18 at 14:52





Funny thing: readCount times { rl.unlock() } - this is the usage of times extension in 2015. No dot notation.

– Axel P
Dec 30 '18 at 14:52












3 Answers
3






active

oldest

votes


















0














Next lines are all just my opinion:




  • there are no special cases when you should or shouldn't use repeat
    function.

  • it has more concise syntax.

  • In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.


It's all up to you to decide when and how to use it.






share|improve this answer

































    0














    From Standard.kt:



    /**
    * Executes the given function [action] specified number of [times].
    *
    * A zero-based index of current iteration is passed as a parameter to [action].
    *
    * @sample samples.misc.ControlFlow.repeat
    */
    @kotlin.internal.InlineOnly
    public inline fun repeat(times: Int, action: (Int) -> Unit) {
    contract { callsInPlace(action) }

    for (index in 0 until times) {
    action(index)
    }
    }


    As you can see repeat(times) is actually for (index in 0 until times).

    There is also a zero-based loop counter and it is: it.




    Should the traditional for loop be replaced with repeat function if
    possible?




    I can't find any reason for that




    Or are there special cases for this function?




    None I can think of.




    Are there any advantages in repeat function?




    None I can think of, or maybe(?) just 1:

    for educational purposes, I suppose it's easier to teach

    that repeat(n) { } performs n iterations of the block of statements inside the curly brackets.






    share|improve this answer

































      0














      It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange and forEach



      (0..4).forEach {
      println(it)
      }



      0 1 2 3 4




      They all serve the same purpose, so the choice is yours.



      You don't need to worry about performance either, since repeat and forEach are inline functions, which means the lambda code is copied over to the call site at compile time.






      share|improve this answer

























        Your Answer






        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: "1"
        };
        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',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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%2fstackoverflow.com%2fquestions%2f53968995%2fpurpose-of-repeat-function%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Next lines are all just my opinion:




        • there are no special cases when you should or shouldn't use repeat
          function.

        • it has more concise syntax.

        • In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.


        It's all up to you to decide when and how to use it.






        share|improve this answer






























          0














          Next lines are all just my opinion:




          • there are no special cases when you should or shouldn't use repeat
            function.

          • it has more concise syntax.

          • In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.


          It's all up to you to decide when and how to use it.






          share|improve this answer




























            0












            0








            0







            Next lines are all just my opinion:




            • there are no special cases when you should or shouldn't use repeat
              function.

            • it has more concise syntax.

            • In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.


            It's all up to you to decide when and how to use it.






            share|improve this answer















            Next lines are all just my opinion:




            • there are no special cases when you should or shouldn't use repeat
              function.

            • it has more concise syntax.

            • In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.


            It's all up to you to decide when and how to use it.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 29 '18 at 11:48

























            answered Dec 29 '18 at 11:28









            SergeySergey

            2,82821631




            2,82821631

























                0














                From Standard.kt:



                /**
                * Executes the given function [action] specified number of [times].
                *
                * A zero-based index of current iteration is passed as a parameter to [action].
                *
                * @sample samples.misc.ControlFlow.repeat
                */
                @kotlin.internal.InlineOnly
                public inline fun repeat(times: Int, action: (Int) -> Unit) {
                contract { callsInPlace(action) }

                for (index in 0 until times) {
                action(index)
                }
                }


                As you can see repeat(times) is actually for (index in 0 until times).

                There is also a zero-based loop counter and it is: it.




                Should the traditional for loop be replaced with repeat function if
                possible?




                I can't find any reason for that




                Or are there special cases for this function?




                None I can think of.




                Are there any advantages in repeat function?




                None I can think of, or maybe(?) just 1:

                for educational purposes, I suppose it's easier to teach

                that repeat(n) { } performs n iterations of the block of statements inside the curly brackets.






                share|improve this answer






























                  0














                  From Standard.kt:



                  /**
                  * Executes the given function [action] specified number of [times].
                  *
                  * A zero-based index of current iteration is passed as a parameter to [action].
                  *
                  * @sample samples.misc.ControlFlow.repeat
                  */
                  @kotlin.internal.InlineOnly
                  public inline fun repeat(times: Int, action: (Int) -> Unit) {
                  contract { callsInPlace(action) }

                  for (index in 0 until times) {
                  action(index)
                  }
                  }


                  As you can see repeat(times) is actually for (index in 0 until times).

                  There is also a zero-based loop counter and it is: it.




                  Should the traditional for loop be replaced with repeat function if
                  possible?




                  I can't find any reason for that




                  Or are there special cases for this function?




                  None I can think of.




                  Are there any advantages in repeat function?




                  None I can think of, or maybe(?) just 1:

                  for educational purposes, I suppose it's easier to teach

                  that repeat(n) { } performs n iterations of the block of statements inside the curly brackets.






                  share|improve this answer




























                    0












                    0








                    0







                    From Standard.kt:



                    /**
                    * Executes the given function [action] specified number of [times].
                    *
                    * A zero-based index of current iteration is passed as a parameter to [action].
                    *
                    * @sample samples.misc.ControlFlow.repeat
                    */
                    @kotlin.internal.InlineOnly
                    public inline fun repeat(times: Int, action: (Int) -> Unit) {
                    contract { callsInPlace(action) }

                    for (index in 0 until times) {
                    action(index)
                    }
                    }


                    As you can see repeat(times) is actually for (index in 0 until times).

                    There is also a zero-based loop counter and it is: it.




                    Should the traditional for loop be replaced with repeat function if
                    possible?




                    I can't find any reason for that




                    Or are there special cases for this function?




                    None I can think of.




                    Are there any advantages in repeat function?




                    None I can think of, or maybe(?) just 1:

                    for educational purposes, I suppose it's easier to teach

                    that repeat(n) { } performs n iterations of the block of statements inside the curly brackets.






                    share|improve this answer















                    From Standard.kt:



                    /**
                    * Executes the given function [action] specified number of [times].
                    *
                    * A zero-based index of current iteration is passed as a parameter to [action].
                    *
                    * @sample samples.misc.ControlFlow.repeat
                    */
                    @kotlin.internal.InlineOnly
                    public inline fun repeat(times: Int, action: (Int) -> Unit) {
                    contract { callsInPlace(action) }

                    for (index in 0 until times) {
                    action(index)
                    }
                    }


                    As you can see repeat(times) is actually for (index in 0 until times).

                    There is also a zero-based loop counter and it is: it.




                    Should the traditional for loop be replaced with repeat function if
                    possible?




                    I can't find any reason for that




                    Or are there special cases for this function?




                    None I can think of.




                    Are there any advantages in repeat function?




                    None I can think of, or maybe(?) just 1:

                    for educational purposes, I suppose it's easier to teach

                    that repeat(n) { } performs n iterations of the block of statements inside the curly brackets.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 29 '18 at 12:24

























                    answered Dec 29 '18 at 11:38









                    forpasforpas

                    10.6k2423




                    10.6k2423























                        0














                        It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange and forEach



                        (0..4).forEach {
                        println(it)
                        }



                        0 1 2 3 4




                        They all serve the same purpose, so the choice is yours.



                        You don't need to worry about performance either, since repeat and forEach are inline functions, which means the lambda code is copied over to the call site at compile time.






                        share|improve this answer






























                          0














                          It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange and forEach



                          (0..4).forEach {
                          println(it)
                          }



                          0 1 2 3 4




                          They all serve the same purpose, so the choice is yours.



                          You don't need to worry about performance either, since repeat and forEach are inline functions, which means the lambda code is copied over to the call site at compile time.






                          share|improve this answer




























                            0












                            0








                            0







                            It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange and forEach



                            (0..4).forEach {
                            println(it)
                            }



                            0 1 2 3 4




                            They all serve the same purpose, so the choice is yours.



                            You don't need to worry about performance either, since repeat and forEach are inline functions, which means the lambda code is copied over to the call site at compile time.






                            share|improve this answer















                            It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange and forEach



                            (0..4).forEach {
                            println(it)
                            }



                            0 1 2 3 4




                            They all serve the same purpose, so the choice is yours.



                            You don't need to worry about performance either, since repeat and forEach are inline functions, which means the lambda code is copied over to the call site at compile time.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 30 '18 at 11:36

























                            answered Dec 29 '18 at 23:54









                            Willi MentzelWilli Mentzel

                            9,024114469




                            9,024114469






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968995%2fpurpose-of-repeat-function%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas