How do lexical closures work? [duplicate]

Python is actually behaving as defined. Three separate functions are created, but they each have the closure of the environment they’re defined in – in this case, the global environment (or the outer function’s environment if the loop is placed inside another function). This is exactly the problem, though – in this environment, i is … Read more

Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)

I needed this as well so I just took the source code from b93 and put it in a “util” class. I had to modify it slightly to work with the current API. For reference here’s the working code (take it at your own risk…): public static<A, B, C> Stream<C> zip(Stream<? extends A> a, Stream<? … Read more

Extract a dplyr tbl column as a vector

With dplyr >= 0.7.0, you can use pull() to get a vector from a tbl. library(dplyr, warn.conflicts = FALSE) db <- src_sqlite(tempfile(), create = TRUE) iris2 <- copy_to(db, iris) vec <- pull(iris2, Species) head(vec) #> [1] “setosa” “setosa” “setosa” “setosa” “setosa” “setosa”

What does a lazy val do?

The difference between them is, that a val is executed when it is defined whereas a lazy val is executed when it is accessed the first time. scala> val x = { println(“x”); 15 } x x: Int = 15 scala> lazy val y = { println(“y”); 13 } y: Int = <lazy> scala> x … Read more