The many MANY faces of a Kotlin for loop

Sdílet
Vložit
  • čas přidán 29. 04. 2023
  • #Kotlin, #forloop, #programming, #development, #coding, #tutorial, #tipsandtricks, #bestpractices.
    In this video, we explore the different ways to use a for loop in Kotlin. From iterating over ranges and collections, to custom iterators and conditions, there are many ways to write a for loop in Kotlin. We'll also show you some best practices and tips to make your code more concise and expressive.
    Whether you're new to Kotlin or an experienced developer, this video is a great way to level up your skills and write more efficient and readable code.
  • Věda a technologie

Komentáře • 15

  • @odayprogrammer
    @odayprogrammer  Před rokem +2

    the for loop in 4:19 should actually be `for (i in 1..list.size)`

  • @abbasadrali
    @abbasadrali Před rokem

    Just to add a little bit..
    the for loop at 1:15 will through "out of bound" exception because the range include the size which will be 1 index more than the "list" size
    the same apply to the loop at 4:30
    plus the main difference between the "until" and "range(two dots)" is that range include the last elements while the "until" donot include it.

    • @odayprogrammer
      @odayprogrammer  Před rokem

      yes I did miss that part and also for the loop with index -1 (lookahead) you can use zipWithNext(), someone in Kotlin Slack told me that

  • @jam4l
    @jam4l Před rokem

    This is very informative, thanks.

  • @MadridIsta7
    @MadridIsta7 Před rokem

    Thanks man. I’m an iOS developer and lately started to do Android as well. This is very helpful and straight to the point!

  • @ulicqueldromal
    @ulicqueldromal Před rokem

    How did that example with current work? That should throw an indexoutofBounds exception. It does for me.

    • @odayprogrammer
      @odayprogrammer  Před rokem

      There’s a correction to it where I should start from 1. I’ve replaced the pinned comment with that correction

  • @drastik67
    @drastik67 Před rokem

    For loops, live long, be happy. :P

  • @ulicqueldromal
    @ulicqueldromal Před rokem

    downTo is not the opposite of until, it includes both ends. It's the opposite of '..'
    You have to be careful with these ranges or you get an indexoutofBounds exception.

  • @gameking2447
    @gameking2447 Před 6 měsíci

    kotlin is really a mixture of java , python

  • @kqvanity
    @kqvanity Před rokem

    Your computer is pretty decent. It takes like ~5 seconds on my PC to run simple kt files.

    • @odayprogrammer
      @odayprogrammer  Před rokem

      It’s cause M1, company laptop

    • @odayprogrammer
      @odayprogrammer  Před rokem

      Also format it, how long has it been since you formatted it

    • @kqvanity
      @kqvanity Před rokem

      > M1
      Can you please do a benchmarking comparison between the two (Intel/AMD and and the M1 chip) for midrange Kotlin/Android projects
      > How long has it been since you formatted it?
      Does formatting play a role in build time?
      I usually don't because for casual trial and error, It's merely a singular file where i get to dabble with the language features.
      However, I developed a bash script that would run my code on Kotlin's playground remote server where it takes nearly ~3 seconds.