Kotlin: new(ish) syntax emerges
-
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 calllast()
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 newlast()
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.
-
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 calllast()
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 newlast()
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.
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#. MarcLatest Articles:
Fun Exploring Div and Table UI Layout -
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 calllast()
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 newlast()
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.
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 " | "
endThe 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 ashuffle
method onto theRange
class. :) -
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#. MarcLatest Articles:
Fun Exploring Div and Table UI Layout -
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 " | "
endThe 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 ashuffle
method onto theRange
class. :) -
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 calllast()
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 newlast()
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.
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.)
-
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.)
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
-
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 calllast()
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 newlast()
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.
-
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 calllast()
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 newlast()
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.
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
-
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.)
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.
-
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
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.
-
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
-
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 calllast()
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 newlast()
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.
-
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 calllast()
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 newlast()
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.
Haskell ranges:
let items = [1..10]
KeithBarrow.net[^] - It might not be very good, but at least it is free!
-
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
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.