cobalt icon indicating copy to clipboard operation
cobalt copied to clipboard

Inline for loops for generating lists

Open Michael2109 opened this issue 6 years ago • 6 comments

In Scala you can do this.

val x = for (i <- 1 to 5) yield i    // x = List (1, 2, 3, 4, 5)

In F# you can do this.

let seq1 = seq { for i in 1 .. 10 -> i }

This could be something useful to add.

Michael2109 avatar May 01 '18 19:05 Michael2109

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

Michael2109 avatar May 08 '18 12:05 Michael2109

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]

FilipJanitor avatar May 08 '18 13:05 FilipJanitor

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)
}

Michael2109 avatar May 08 '18 13:05 Michael2109

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)

FilipJanitor avatar May 08 '18 13:05 FilipJanitor

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.

Michael2109 avatar May 14 '18 18:05 Michael2109

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.

Michael2109 avatar May 15 '18 14:05 Michael2109