Iterate in reverse order over a list in Java

Sometimes we want to iterate in reverse order over a list in Java – without (re)sorting the list. Code example:

    for (T item : backwards(list)) {
        // do something
    }

Here is a backwards() method that does this for you (maybe put this code into a util class and use a static import):

    public static <T> Iterable<T> backwards(final List<T> list) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                return backwardsIterator(list);
            }
        };
    }

    private static <T> Iterator<T> backwardsIterator(List<T> list) {
        final ListIterator<T> iter = list.listIterator(list.size());
        return new Iterator<T>() {
            @Override
            public boolean hasNext() {
                return iter.hasPrevious();
            }

            @Override
            public T next() {
                return iter.previous();
            }

            @Override
            public void remove() {
                iter.remove();
            }
        };
    }

Record and share shell activity via shelr.tv

Terminal

Shelr.tv allows Unix/Linux command line users to record something interesting from their terminal and share it to followers.

It is a bit like YouTube for plain text shellcasts. A great feature is that you can copy and paste everything you see.

A nice intro with interesting comments from one of the core developers can be found on linuxaria.com.

A Debian package has been proposed through the Debian “package mentor” system.

World calendar

Facts due to movements of earth and moon:

  • 1 solar year = about 365.25 days
  • 1 lunar month = about 29.5 days

The leap year rules from the Gregorian calendar are good because they keep the calendar year well aligned with the solar year.

But the weird lengths of the months and shifting week day of January 1st are drawbacks in many respects.

It would be so much nicer to have a perennial calendar so that every year begins on the same week day and the week day of any given day in a month is predictable.

For these and many other reasons, I like the proposed World Calendar.

Export HTML table as CSV file using JQuery

Put this code into a script section into a common HTML head include file:

$(document).ready(function() {

  $('table').each(function() {
    var $table = $(this);

    var $button = $("<button type='button'>");
    $button.text("Export to spreadsheet");
    $button.insertAfter($table);

    $button.click(function() {
      var csv = $table.table2CSV({delivery:'value'});
      window.location.href = 'data:text/csv;charset=UTF-8,'
                            + encodeURIComponent(csv);
    });
  });
})

Note:

Consider vending misers to save energy at work …

A power meter is now installed in the upstairs lunch room in Halifax at the company that I currently work for. It measures the power consumption of both vending machines at the same time (using a power bar).

vending-machines-metered

The meter shows both the current consumption in Watts (W) and the accumulated energy use in kilo Watt hours (kWh). I installed it yesterday at 2:45pm and will let it run for 1 week (to get good data).

green-team-metering-in-progress

After that we will do some cost/benefit analysis regarding the potential installation of vending misers.

If you are in our Halifax office you can go and see for yourself how the consumption kind of alternates between roughly 150W (between refrigeration) and roughly 800W (during active refrigeration).

If you want to do something similar with other electric appliances, e.g. at your homes, you can buy those power meters devices at Canadian Tire or similar stores. This is the one that I am currently using.