Open Michael2109 opened 6 years ago
Any opinions on the format for this? We could combine both options. Although the Scala one is quite nice by having a separate keyword of yield
.
val x = for (i <- 1 to 5) -> i
As I am not a Scalist I am not sure how powerful the syntactic sugar is, if it is closer to list comprehensions or just some simple initialization (simple sequences) , but if the latter is the case, we can take a simplistic approach of Python - there is a built in function for this. range(X)
creates list [0,1,2,3 .. (X-1)]
Then range(Y,X)
creates list [Y, (Y+1) .. (X-1)]
There is also a step version range(Y,X,A)
that produces list `[Y, (Y+A), (Y+2A) .. up to highest possible number smaller than X]
I like that. There are a few different things Scala does with this but I'm not sure how it translates to Python.
Equivalent of running .map(_*2)
after generating the list.
for (i <- 1 to 5) yield i * 2
Also adding extra conditions.
for (e <- a if e > 2) yield e
Also they have examples with using objects.
def getQueryAsSeq(query: String): Seq[MiniTweet] = {
val queryResults = getTwitterInstance.search(new Query(query))
val tweets = queryResults.getTweets // java.util.List[Status]
for (status <- tweets) yield ListTweet(status.getUser.toString, status.getText, status.getCreatedAt.toString)
}
It seems like it is closer to list comprehensions (if that is the case, I don see much difference between this and #457) Pyhonic syntax for these is very similar to haskell and the advantage of it is that it is more math-like and seems more compact and declarative compared to using some sort of for explicit loops (but really, I just went through like the first tutorial on this Scala functionality I found)
The difference is this will be a built in language feature. #457 (As we've recently realised) are all just method calls that are parsed using a different syntax. This will be something we have to add in that could allow for some great ways of generating lists. I think this will be very useful. I feel the Scala version is very neat. I'll look around and see if there are alternatives that we could use.
I'm looking into this more and I'm unsure of how beneficial this will be to us. Using map
for lists can do anything these can do. Although this is somewhat similar if not the same to Haskell list generation.
https://wiki.haskell.org/List_comprehension
I wonder if this is something we should do now or leave until the future if we feel a need for it.
In Scala you can do this.
In F# you can do this.
This could be something useful to add.