Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Kotlin: new(ish) syntax emerges

Kotlin: new(ish) syntax emerges

Scheduled Pinned Locked Moved The Weird and The Wonderful
asp-netandroidlounge
16 Posts 11 Posters 0 Views 1 Watching
  • 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.
  • R raddevus

    In Kotlin you can do this:

    var items = (1..300) // becomes an IntRange (range of integers)

    Additionally you can call the shuffle() method on the range to randomly shuffle the ints. Then you can call last() on the returned shuffled ints to get the random last item. So if you want a list of 10 random values in the range of 1 - 1000, you can do it with just a couple of lines of Kotlin. Each time through the loop the range is shuffled and a new last() is chosen.

    for (i in 1..10) {
    print((1..1000).shuffled().last())
    print (" | ")
    }

    Output looks like the following:

    795 | 948 | 719 | 304 | 733 | 849 | 723 | 66 | 316 | 619 |

    Try it out in your browser at : Try Kotlin[^] These new types of syntax are ugly to me, but I can see that they could grow on a dev. *Note:By emerges, I just mean that newer languages have syntax which looks similar to this. Kotlin has had the Range type for a long time. Basically a fluent interface[^] but just interesting that it becomes more of a core part of syntax in newish languages.

    M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #2

    It's very Python or Ruby or whatever-esque. Personally, I really like that syntax, as it's so much easier to read. In fact, in C# I wrote an extension method so I can do: 5.ForEach(n=>DoSomethingWithN()); So technically, in C# with extension methods, I should be able to write: 10.ForEach(n=>1000.Shuffle().Last().ConsoleOut()); I like that syntax because it reads left-to-right and would be very much like the pipe operator in F#. Marc

    Latest Articles:
    Fun Exploring Div and Table UI Layout

    R 1 Reply Last reply
    0
    • R raddevus

      In Kotlin you can do this:

      var items = (1..300) // becomes an IntRange (range of integers)

      Additionally you can call the shuffle() method on the range to randomly shuffle the ints. Then you can call last() on the returned shuffled ints to get the random last item. So if you want a list of 10 random values in the range of 1 - 1000, you can do it with just a couple of lines of Kotlin. Each time through the loop the range is shuffled and a new last() is chosen.

      for (i in 1..10) {
      print((1..1000).shuffled().last())
      print (" | ")
      }

      Output looks like the following:

      795 | 948 | 719 | 304 | 733 | 849 | 723 | 66 | 316 | 619 |

      Try it out in your browser at : Try Kotlin[^] These new types of syntax are ugly to me, but I can see that they could grow on a dev. *Note:By emerges, I just mean that newer languages have syntax which looks similar to this. Kotlin has had the Range type for a long time. Basically a fluent interface[^] but just interesting that it becomes more of a core part of syntax in newish languages.

      R Offline
      R Offline
      Ryan Peden
      wrote on last edited by
      #3

      Hmm, neat. I like it. Looks pretty similar to how I'd use ranges in Ruby:

      (1..10).each do
      print (1..1000).to_a.shuffle.last
      print " | "
      end

      The only real difference is that I had to convert the range to an array, since arrays have a shuffle method and ranges don't. Of course, being Ruby, I could just patch a shuffle method onto the Range class. :)

      R 1 Reply Last reply
      0
      • M Marc Clifton

        It's very Python or Ruby or whatever-esque. Personally, I really like that syntax, as it's so much easier to read. In fact, in C# I wrote an extension method so I can do: 5.ForEach(n=>DoSomethingWithN()); So technically, in C# with extension methods, I should be able to write: 10.ForEach(n=>1000.Shuffle().Last().ConsoleOut()); I like that syntax because it reads left-to-right and would be very much like the pipe operator in F#. Marc

        Latest Articles:
        Fun Exploring Div and Table UI Layout

        R Offline
        R Offline
        raddevus
        wrote on last edited by
        #4

        Good point about Left to Right reading. I mostly like it too and I’m sure it will grow on me. I think the declaration/instantiation of the Range is a bit odd (1..10). No new operator or anything. but, it is quite streamlined.

        1 Reply Last reply
        0
        • R Ryan Peden

          Hmm, neat. I like it. Looks pretty similar to how I'd use ranges in Ruby:

          (1..10).each do
          print (1..1000).to_a.shuffle.last
          print " | "
          end

          The only real difference is that I had to convert the range to an array, since arrays have a shuffle method and ranges don't. Of course, being Ruby, I could just patch a shuffle method onto the Range class. :)

          R Offline
          R Offline
          raddevus
          wrote on last edited by
          #5

          Oh, wow, that’s very interesting that it is that close to the Ruby version. That’s one language I’ve stayed away from. :)

          1 Reply Last reply
          0
          • R raddevus

            In Kotlin you can do this:

            var items = (1..300) // becomes an IntRange (range of integers)

            Additionally you can call the shuffle() method on the range to randomly shuffle the ints. Then you can call last() on the returned shuffled ints to get the random last item. So if you want a list of 10 random values in the range of 1 - 1000, you can do it with just a couple of lines of Kotlin. Each time through the loop the range is shuffled and a new last() is chosen.

            for (i in 1..10) {
            print((1..1000).shuffled().last())
            print (" | ")
            }

            Output looks like the following:

            795 | 948 | 719 | 304 | 733 | 849 | 723 | 66 | 316 | 619 |

            Try it out in your browser at : Try Kotlin[^] These new types of syntax are ugly to me, but I can see that they could grow on a dev. *Note:By emerges, I just mean that newer languages have syntax which looks similar to this. Kotlin has had the Range type for a long time. Basically a fluent interface[^] but just interesting that it becomes more of a core part of syntax in newish languages.

            K Offline
            K Offline
            kalberts
            wrote on last edited by
            #6

            The subrange notation is about 50 years old, isn't it? In Pascal (1970), the parentheses are not needed, though. Subranges are, of course, allowed on any discrete type. So if you have a TYPE month: (jan, feb, mar, apr, may, june, july, aug, sept, oct, nov, dec); - note that these are NOT integers, but a distinct value domain - you can declare a VAR SummerMonth: may..aug; and assigning a value to SummerMonth outside that range causes an exception. Maybe Kotlin provides a similar full-blown enum concept, including subranges. (In Pascal, arrays can have any subrange index, e.g. Members: ARRAY [1970..2025] OF MemberList; - maybe that is possible in Kotlin as well, but it usually is not in C-derived languages.)

            S R 2 Replies Last reply
            0
            • K kalberts

              The subrange notation is about 50 years old, isn't it? In Pascal (1970), the parentheses are not needed, though. Subranges are, of course, allowed on any discrete type. So if you have a TYPE month: (jan, feb, mar, apr, may, june, july, aug, sept, oct, nov, dec); - note that these are NOT integers, but a distinct value domain - you can declare a VAR SummerMonth: may..aug; and assigning a value to SummerMonth outside that range causes an exception. Maybe Kotlin provides a similar full-blown enum concept, including subranges. (In Pascal, arrays can have any subrange index, e.g. Members: ARRAY [1970..2025] OF MemberList; - maybe that is possible in Kotlin as well, but it usually is not in C-derived languages.)

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #7

              Member 7989122 wrote:

              Subranges are, of course, allowed on any discrete type.

              That's something I got used to with Ada that I wish I had in C++ (or any of the other static typed languages I use) - even in languages like Kotlin or Rust, which have first class support for ranges, they're still a run-time entity, not a compile-time thing, so you can't constrain how a function works by a range in the same way... And you can't take sub-ranges of enums either :-( Oh, for a [dependently typed language](https://en.wikipedia.org/wiki/Dependent\_type#Comparison\_of\_languages\_with\_dependent\_types)?

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              1 Reply Last reply
              0
              • R raddevus

                In Kotlin you can do this:

                var items = (1..300) // becomes an IntRange (range of integers)

                Additionally you can call the shuffle() method on the range to randomly shuffle the ints. Then you can call last() on the returned shuffled ints to get the random last item. So if you want a list of 10 random values in the range of 1 - 1000, you can do it with just a couple of lines of Kotlin. Each time through the loop the range is shuffled and a new last() is chosen.

                for (i in 1..10) {
                print((1..1000).shuffled().last())
                print (" | ")
                }

                Output looks like the following:

                795 | 948 | 719 | 304 | 733 | 849 | 723 | 66 | 316 | 619 |

                Try it out in your browser at : Try Kotlin[^] These new types of syntax are ugly to me, but I can see that they could grow on a dev. *Note:By emerges, I just mean that newer languages have syntax which looks similar to this. Kotlin has had the Range type for a long time. Basically a fluent interface[^] but just interesting that it becomes more of a core part of syntax in newish languages.

                G Offline
                G Offline
                grumpy_nl
                wrote on last edited by
                #8

                Reminds of Turbo Pascal, so not that new.

                R 1 Reply Last reply
                0
                • R raddevus

                  In Kotlin you can do this:

                  var items = (1..300) // becomes an IntRange (range of integers)

                  Additionally you can call the shuffle() method on the range to randomly shuffle the ints. Then you can call last() on the returned shuffled ints to get the random last item. So if you want a list of 10 random values in the range of 1 - 1000, you can do it with just a couple of lines of Kotlin. Each time through the loop the range is shuffled and a new last() is chosen.

                  for (i in 1..10) {
                  print((1..1000).shuffled().last())
                  print (" | ")
                  }

                  Output looks like the following:

                  795 | 948 | 719 | 304 | 733 | 849 | 723 | 66 | 316 | 619 |

                  Try it out in your browser at : Try Kotlin[^] These new types of syntax are ugly to me, but I can see that they could grow on a dev. *Note:By emerges, I just mean that newer languages have syntax which looks similar to this. Kotlin has had the Range type for a long time. Basically a fluent interface[^] but just interesting that it becomes more of a core part of syntax in newish languages.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #9

                  raddevus wrote:

                  Each time through the loop the range is shuffled and a new last() is chosen.

                  Isn't that code going to be horribly inefficient? Surely the language must provide a better way to get a random number in a particular range?


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  R E R 3 Replies Last reply
                  0
                  • K kalberts

                    The subrange notation is about 50 years old, isn't it? In Pascal (1970), the parentheses are not needed, though. Subranges are, of course, allowed on any discrete type. So if you have a TYPE month: (jan, feb, mar, apr, may, june, july, aug, sept, oct, nov, dec); - note that these are NOT integers, but a distinct value domain - you can declare a VAR SummerMonth: may..aug; and assigning a value to SummerMonth outside that range causes an exception. Maybe Kotlin provides a similar full-blown enum concept, including subranges. (In Pascal, arrays can have any subrange index, e.g. Members: ARRAY [1970..2025] OF MemberList; - maybe that is possible in Kotlin as well, but it usually is not in C-derived languages.)

                    R Offline
                    R Offline
                    raddevus
                    wrote on last edited by
                    #10

                    Member 7989122 wrote:

                    The subrange notation is about 50 years old, isn't it?

                    Yeah, it probably is quite old.

                    Member 7989122 wrote:

                    but it usually is not in C-derived languages.

                    And almost every language I use is C-derived (C++, C#, Java, JavaScript) so that's why it just looks odd to me.

                    1 Reply Last reply
                    0
                    • G grumpy_nl

                      Reminds of Turbo Pascal, so not that new.

                      R Offline
                      R Offline
                      raddevus
                      wrote on last edited by
                      #11

                      Yeah, I probably have forgotten about Pascal. Last time I actually used it was probably 1996 or so. :)

                      1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        raddevus wrote:

                        Each time through the loop the range is shuffled and a new last() is chosen.

                        Isn't that code going to be horribly inefficient? Surely the language must provide a better way to get a random number in a particular range?


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        R Offline
                        R Offline
                        raddevus
                        wrote on last edited by
                        #12

                        Richard Deeming wrote:

                        Isn't that code going to be horribly inefficient? Surely the language must provide a better way to get a random number in a particular range?

                        Oh yes, just laziness on my part for a quick example. There may be other ways to accomplish the same thing that are more efficient.

                        1 Reply Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          raddevus wrote:

                          Each time through the loop the range is shuffled and a new last() is chosen.

                          Isn't that code going to be horribly inefficient? Surely the language must provide a better way to get a random number in a particular range?


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          E Offline
                          E Offline
                          englebart
                          wrote on last edited by
                          #13

                          This approach would not scale well if you want a random number between 1 and 2^31-1.

                          1 Reply Last reply
                          0
                          • R raddevus

                            In Kotlin you can do this:

                            var items = (1..300) // becomes an IntRange (range of integers)

                            Additionally you can call the shuffle() method on the range to randomly shuffle the ints. Then you can call last() on the returned shuffled ints to get the random last item. So if you want a list of 10 random values in the range of 1 - 1000, you can do it with just a couple of lines of Kotlin. Each time through the loop the range is shuffled and a new last() is chosen.

                            for (i in 1..10) {
                            print((1..1000).shuffled().last())
                            print (" | ")
                            }

                            Output looks like the following:

                            795 | 948 | 719 | 304 | 733 | 849 | 723 | 66 | 316 | 619 |

                            Try it out in your browser at : Try Kotlin[^] These new types of syntax are ugly to me, but I can see that they could grow on a dev. *Note:By emerges, I just mean that newer languages have syntax which looks similar to this. Kotlin has had the Range type for a long time. Basically a fluent interface[^] but just interesting that it becomes more of a core part of syntax in newish languages.

                            O Offline
                            O Offline
                            obermd
                            wrote on last edited by
                            #14

                            For basic loops I've wanted this type of syntax for a while, especially if I can replace the start and end items with variables.

                            1 Reply Last reply
                            0
                            • R raddevus

                              In Kotlin you can do this:

                              var items = (1..300) // becomes an IntRange (range of integers)

                              Additionally you can call the shuffle() method on the range to randomly shuffle the ints. Then you can call last() on the returned shuffled ints to get the random last item. So if you want a list of 10 random values in the range of 1 - 1000, you can do it with just a couple of lines of Kotlin. Each time through the loop the range is shuffled and a new last() is chosen.

                              for (i in 1..10) {
                              print((1..1000).shuffled().last())
                              print (" | ")
                              }

                              Output looks like the following:

                              795 | 948 | 719 | 304 | 733 | 849 | 723 | 66 | 316 | 619 |

                              Try it out in your browser at : Try Kotlin[^] These new types of syntax are ugly to me, but I can see that they could grow on a dev. *Note:By emerges, I just mean that newer languages have syntax which looks similar to this. Kotlin has had the Range type for a long time. Basically a fluent interface[^] but just interesting that it becomes more of a core part of syntax in newish languages.

                              K Offline
                              K Offline
                              Keith Barrow
                              wrote on last edited by
                              #15

                              Haskell ranges:

                              let items = [1..10]

                              KeithBarrow.net[^] - It might not be very good, but at least it is free!

                              1 Reply Last reply
                              0
                              • Richard DeemingR Richard Deeming

                                raddevus wrote:

                                Each time through the loop the range is shuffled and a new last() is chosen.

                                Isn't that code going to be horribly inefficient? Surely the language must provide a better way to get a random number in a particular range?


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                R Offline
                                R Offline
                                Rob Grainger
                                wrote on last edited by
                                #16

                                That was my immediate thought too. Glad to see someone else cares about wasting cycles.

                                "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups