Question about sum(x -> ..., 1:n) notation

Hi again Dan. Question about this formula you wrote. I don’t understand the notation.

sum(k -> loops(n,k), 1:n) == factorial(n)

What does sum(k->loops(n,k), 1:n) mean? Is something ranging from k to loops(n,k)? Also, what is this 1:n for?

Thanks again.

sum iterates over a collection, applies a function to each element, and sums the results from the function. k->loops(n,k) is an anonymous function of k that returns loops(n,k). 1:n is a range, and is the collection that sum iterates. The result here is loops(n,1) + loops(n,2) + ... + loops(n,n)

5 Likes

Thanks!