Haskell foldl’ poor performance with (++)

There is much confusion about this issue. The usual reason given is that “repeatedly appending at end of list requires repeated traversals of list and is thus O(n^2)“. But it would only be so simple under strict evaluation. Under lazy evaluation everything is supposed to be delayed, so it begs the question whether there actually … Read more

hibernate: LazyInitializationException: could not initialize proxy

The problem is that you are trying to access a collection in an object that is detached. You need to re-attach the object before accessing the collection to the current session. You can do that through session.update(object); Using lazy=false is not a good solution because you are throwing away the Lazy Initialization feature of hibernate. … Read more

Recursive function causing a stack overflow

You’re being hit by filter‘s laziness. Change (filter …) to (doall (filter …)) in your recur form and the problem should go away. A more in-depth explanation: The call to filter returns a lazy seq, which materialises actual elements of the filtered seq as required. As written, your code stacks filter upon filter upon filter…, … Read more

Pass arguments to dplyr functions

You need to use the standard evaluation versions of the dplyr functions (just append ‘_’ to the function names, ie. group_by_ & summarise_) and pass strings to your function, which you then need to turn into symbols. To parameterise the argument of summarise_, you will need to use interp(), which is defined in the lazyeval … Read more

Explain a lazy evaluation quirk

This is no longer true as of R 3.2.0! The corresponding line in the change log reads: Higher order functions such as the apply functions and Reduce() now force arguments to the functions they apply in order to eliminate undesirable interactions between lazy evaluation and variable capture in closures. And indeed: add <- function(x) { … Read more

Does Haskell have tail-recursive optimization?

Haskell uses lazy-evaluation to implement recursion, so treats anything as a promise to provide a value when needed (this is called a thunk). Thunks get reduced only as much as necessary to proceed, no more. This resembles the way you simplify an expression mathematically, so it’s helpful to think of it that way. The fact … 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”) #> #> Attaching package: ‘dplyr’ #> The following objects are masked from ‘package:stats’: #> #> filter, lag #> The following objects are masked from ‘package:base’: #> #> intersect, setdiff, setequal, union db <- src_sqlite(tempfile(), create = TRUE) iris2 <- … Read more