Question: I have SSRS report that has around 80+ columns. I have requirement where in dynamically hide\show columns in report based on user selection. I could able to do it by setting expression for "Visiblity" property and having report parameter thro' which columns to display can be choosen.

My problem is 2 points

1. fox example if columns 2 and 4 to be hidden, then there is an empty column between 1 and 3 and 5 columns. How to avoid this
2. When i export to PDF / Excel these spaces prevail.

Let me know your suggestions / help.

Answer: 
Here is the shortest version I could come up with.

```
_.reduce = function(list, iteratee, memo){
  var memoUndefined = arguments.length < 3;
  _.each(list, function(elem, index, list){
    if(memoUndefined) {
      memoUndefined = false;
      memo = elem;
    } else memo = iteratee(memo, elem, index, list);
  });
  return memo;
};

```
First, you need a way to determine whether `reduce` received an initial `memo` value when you are inside the function you pass to `_.each`. You could do this a number of ways. One way is to simply set a flag based on the length of `arguments`. You need to do this outside the `_.each` call because the function you pass to `_.each` will have its own `arguments` object, masking the `arguments` object for `reduce`. 

Using your code as a starting point:

```
var reduce = function(list, iteratee, memo) {
    var considerFirst = arguments.length > 2;
    var memo = memo || list[0] || list[Object.keys(list)[0]];
    _.each(list, function(element, index, list){
        if (index > 0 || considerFirst) {
            memo = iteratee(memo, element, index, list);
        }
    });
    return memo;
};

```

This still isn't quite right, though. We also need to update how you are defaulting `memo`. Currently, if `memo` receives a falsy value (e.g. `0`), we still set it to the fist element in the list, but we don't set the flag indicating to ignore the first element. This means `reduce` will process the first element twice.

To get this right, you need to change how you are defaulting `memo`, setting it only if no argument is passed in. You could do something like this:

```
var reduce = function(list, iteratee, memo) {
    var considerFirst = true;
    if (arguments.length < 3) {
        memo = list[0];
        considerFirst = false;
    } 
    _.each(list, function(element, index, list){
        if (index > 0 || considerFirst) {
            memo = iteratee(memo, element, index, list);
        }
    });
    return memo;
};

```

This way, you only set `memo` if no argument was passed.

Note that you don't need to initialize `memo` with `var`. Having `memo` as a parameter does all the initialization you need.

Also note that I removed support for using `reduce` on a plain object. When you pass an object to `_.each`, the value of the `index` parameter is not a numerical index but the key for that entry, which may or may not be an integer. This does not play well with our `index > 0` check to see if we are looking at the first entry. There are ways around this, but it doesn't seem central to your question. Check out the actual [underscore implementation](http://underscorejs.org/docs/underscore.html) if you want to see how to make it work.

Update: the implementation SpiderPig suggests doesn't rely on `index` and so would work with objects, not just arrays.

Lastly, it's worth pointing out that [underscore's implementation](http://underscorejs.org/docs/underscore.html) of `_.reduce` uses a `for` loop and not `_.each`.