Category Archives: Uncategorized

Electoral Votes and Poll Closing Times

7:00 PM         Georgia         15
7:00 PM        Indiana        11
7:00 PM        Kentucky 8
7:00 PM        South Carolina 8
7:00 PM         Vermont        3
7:00 PM        Virginia 13
7:30 PM        North Carolina 15
7:30 PM        Ohio        20
7:30 PM        West Virginia 5
8:00 PM        Alabama        9
8:00 PM        Connecticut 7
8:00 PM        Delaware 3
8:00 PM        Florida        27
8:00 PM        Illinois 21
8:00 PM        Maine        4
8:00 PM        Maryland 10
8:00 PM        Massachusetts 12
8:00 PM        Mississippi 6
8:00 PM        Missouri 11
8:00 PM        New Hampshire 4
8:00 PM        New Jersey 15
8:00 PM        Oklahoma 7
8:00 PM        Pennsylvania 21
8:00 PM        Tennessee 11
8:30 PM        Arkansas 6
9:00 PM        Arizona        10
9:00 PM        Colorado 9
9:00 PM        Kansas        6
9:00 PM        Louisiana 9
9:00 PM        Michigan 17
9:00 PM        Minnesota 10
9:00 PM        Nebraska 5
9:00 PM        New Mexico 5
9:00 PM        New York 31
9:00 PM        North Dakota 3
9:00 PM        Rhode Island 4
9:00 PM        South Dakota 3
9:00 PM        Texas        34
9:00 PM        Wisconsin 10
9:00 PM        Wyoming        3
10:00 PM Iowa        7
10:00 PM Montana        3
10:00 PM Nevada        5
10:00 PM Utah        5
11:00 PM California 55
11:00 PM Hawaii        4
11:00 PM Idaho        4
11:00 PM Oregon        7
11:00 PM Washington 11
1:00 AM        Alaska        3


Take a picture of yourself right now.
don’t change your clothes, don’t fix your hair…just take a picture.
post that picture with NO editing.
post these instructions with your picture.

Trying to operatate an iPhone backwards makes me frown.

Camping this last weekend at West Hills

This is the shelter we stayed in. The high pitched roof helps keep the heat in, and the rain out. And the snow, although that mostly melted.

This was the point of the whole trip. A garbage can cooked turkey to share with friends and family. The can acts as an oven, with the charcoal providing way more than enough heat to cook the bird, which is impaled on a stake, holding it vertically in the middle of the can. The 15 pound bird cooked in just a few hours.

My boy Jonathan (on the left), me on the right, and our friend Joel in between. Yes, that’s coffee in the mug. And, yes, it’s a stainless steel vacuum mug. With a spill proof top. But not the one I leave at the office.

My other boy, Michael, against the far wall, near the fire. They’re warming up after we sent them outside to do some of the cooking. Which was mostly making sure there were enough coals around the trash can.
We also made mashed potatoes, sweet potatoes, gravy, peas, corn bread, stuffing, and cherry and apple pies, cooked in dutch ovens. The dutch ovens were in the fireplace opposite the one in this picture, in the far corner of the shelter. Between the two fireplaces, the inside temperature got up into the 80s at one point. We had to open the doors to let out the heat.
And the smoke.

Everyone had a good time, and we came back with the same number of boys we left with.

Office 1 (me 0)

“my office looks like a bookstore exploded in it, and then an electronics store was dropped on it to smother the flames.” – J.Scalzi

The books are all triple stacked, and there are several more shelves surrounding the room….

This showed up in my local bookstore

So I bought it. And there was at least one more face out copy, besides the one you see here.

And this is where I bought it:

Penn Books on the LIRR Level of Penn Station. Small store, but many shelves of SF, particularly if you know to look under the display table across from the main set of shelves. The staff are outstanding. I got the first recommendation for Vinge’s Rainbows (sic) End there. They also have an excellent classics and literature section. As in, when I mentioned that ‘March Up Country’ was (sort of) based on ‘The Anabasis’, they knew where it should be shelved, wondered why it wasn’t there, and offered to order it. Which might just be good salesmanship, except, after picking up my copy, I was looking in the same section for a copy of the Eddas, which they had, and spotted another copy of the Anabasis.

Buy books there, if you can.

Using the Standard C++ Library in a Functional style.

The standard C++ library offers a number of algorithms that have nearly exact analogues in functional languages, and are used in almost the same way. In particular std::accumulate and std::transform are powerful, and very general, algorithms. In functional programming literature, accumulate is usually referred to as fold, or foldl, and transform is known as map, or zipWith, depending on whether transform is the unary or binary function form.

To quickly review, accumulate takes an iterator range, and either sums the range, or applies a functor to each value together with the result of the last application of the functor. This can be thought of as putting the operation between each pair of elements in the range. When the operation is plus or times, the result is the sum or product of all of the elements in the list. This is useful, but not really exciting to most programmers. However, when you consider carefully what can be used as the functor argument, you realize that fold is a an extremely powerful algorithm.

A quick example:

std::list& cons(std::list& xs,int x)



return xs;


int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

vector v(init, init + 10);

std::list empty;

std::list v2 = std::accumulate(v.begin(), v.end(), empty, cons);

> foldr (:) [] [1..10]

(Note, the lines of code starting with > are Haskell equivalents to the C++ code, and SHOULD work at a ghci Prelude> prompt. Since that’s where I’m cutting and pasting from. Although it’s sort of like literate haskell, the whole article probably won’t compile since early definitions may conflict with later ones.)

Cons is a list constructor function. It pushes its argument onto the end of the linked list, and then returns a reference to the list that the element was appended to. The fact that this function takes and returns a reference to a list is a compromise I’ve made between the C++ library and functional purity. Really, I should be taking and returning lists by value. Except that the overhead is unconscionable, turning an O(n) operation into an O(n^2) operation. Since there is no way of accessing the intermediate state, in this case, I’m safe, but you need to be careful in this analysis. Using cons() in your own code will modify the value, and you lose the referential transparency we’re trying to maintain.

So the application of cons() using accumulate against an empty list copies the list into a new list. If we had supplied a non-empty list, we would have appended the lists together. Interesting, for an algorithm that’s usually considered a purely numerical one.

Functional languages usually work with singly linked lists. A singly linked list has an append at the head only, and therefore the tail remains unchanged, and any references to the tail can not tell that there is a new list that uses it. In fact, a list might be the tail of several other lists, and there is no way of telling the difference. It would be possible to create a version of list that is singly linked, where cons returned by value, and in that case, two lists could share a tail, but have distinct heads. So long as the list held only immutable values (and properly, all values are immutable), you would be completely safe.

Transform, or map, as it’s know in FP circles, is another fundamental algorithm. Transform takes an iterator range and applies a functor to each element in the range and copies the result into a destination output iterator, or, in its second form, takes two ranges and a binary operator, and outputs the result of pair -wise evaluation of the elements in each range, through the output iterator.

int func(int x)


return x*x + x + 1;


// …

std::transform(v.begin(), v.end(), v2.begin(), func);

> let func x = x*x + x + 1

> map func [1 .. 10]

Functions and function objects as first-class entities

As you can see, the core STL algorithms are fairly weak by themselves. That is, they don’t really do anything by themselves. What they do is capture conventional patterns of function applications to collections. The real power is in the functions themselves. However, C++ functions are fairly limited. Let’s say you wanted to write a function that added a constant K to its argument. One way is to write int adder(int x, int K); Unfortunately, it’s rather difficult to apply this function with the same constant to every element in a collection.

{Actually, in a few moments we’ll see how we can do exactly that almost trivially, but let’s hold on to that thought.}

Function objects, aka functors in the OO community (the name means something _entirely_ different in functional programming) , give us a way of extending what functions can do by letting us associate some additional information with the function call. We do this by creating an object with an operator()() method. That lets the object be used where a function is called for, and also to supply the additional information as part of the object the operator is part of.

class adder


int K;


adder(int k) : K(k)


int operator()(int i) const

{return K+i;}


Then we can do something like:

std::transform(v.begin(), v.end(), v3.begin(), adder(5));

> let adder x = (x +)

> let adder5 = adder 5

> map adder5 [1 .. 10]

But, as you can see, that’s a lot of work for a small function that we’re probably only going to use in one place.

Still, it’s nice to see that we now have an entity that can be used just like a function, but is also a first class entity in the C++ language.

Lambda abstraction and Partial Application

So, how can the adder example be made more convenient. Something that you might actually use?
How about

std::transform(v.begin(), v.end(), v4.begin(), _1 + 5);

> map (x -> x + 5) [1 .. 10]

> — or, even cooler

> map ( + 5) [1 .. 10]

This is using the Boost Lambda library to constructed a function object in place. One that takes a single parameter, and returns that plus 5. That _1 is what as known as a placeholder. Its type is boost::lambda::placeholder1_type. (And, yes, the leading underscore followed by a number is a perfectly legal variable name.) The functor acts in this context as though it has an operator() with a signature like int operator()(int arg1), and when transform applies it, the output is the same as our earlier example using adder. Except we didn’t have create a class just for this single use.

The reason I say that it acts as though it has that signature in this context, is that the actual signature is much more complicated, involving quite a bit of template metaprogramming in order to deduce the correct signature given the actual types of the arguments. If, v, for example, was a vector of doubles, the lambda expression would need to calculate that the return type is also a double, since the type of (double + int) is double. And for non-builtin types, it can be much more complicated, particularly if the lambda expression has more than one free variable.

For example:

k = (_1 * _2)(i,j);

> let k = x y -> x * y

That lambda expression can be applied to any pair of arguments that has an operator*() defined for it, and then as long as k has an assignment operator that can accept the result of that operator*(), everything will just work. However, that flexibility comes with a cost. The actual type of (_1 *_2) is insanely complicated. With the version of boost that I have, with gcc 4.0.2, the type is:

const boost::lambda::lambda_functor, boost::tuples::tuple >, boost::lambda::lambda_functor >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >

Not a variable or member you would like to declare. Fortunately, it works together with boost::function. You can assign that functor to a boost::function for later use. e.g.

boost::function a = (_1 * _2);

boost::function b = (_1 * _2);

This is also one of the powerful aspects of ML and Haskell, and most modern functional languages. They are strictly typed, but the types can be inferred by the compiler. So the programmer is, for the most part, freed from having to annotate the types of everything. But the compiler will still give you an error if you say something that is really impossible. You get most of the benefits of duck-typing, without having to worry that there is some case that you haven’t covered in tests that will result in ‘Does Not Understand’.

The boost::lambda library even supports currying, although with a syntax that is a little hard to work with:

boost::function c = bind(protect(_1 * _2), i, _1);

The protect call protects the evaluation of the lambda expression from the bind operation. The bind operation attaches an argument to an expression to be used when the expression is evaluated later. In this case, one of the arguments is a lambda placeholder, so that the result is equivalent to the lambda expression (i * _1).

The documentation for boost::lambda is quite good, and is worth extensive study. So I won’t repeat it. Instead, let’s focus on ways of using it that you might not have thought of otherwise.

Pipes and Filters Architecture

Pipes and Filters is one of the fundamental architectural patterns identified by Mary Shaw. On a Unix command line, this is expressed with | (pipe), < (redirect stdin), > (redirect stdout), and tools like tee and xargs. In a functional programing system, piping the output of one function to the input of another is via function composition, that is with functions f and g, you compute f(g(x)). This is also sometimes written as f . g. read as f compose g.

Doing this can also be very useful in complex template programs, where the intermediate type is not known, or difficult to express, but when passed directly to another templated function, the compiler computes the type for us.

The goal here is to take a sequence of functions, compose the entire sequence, and then apply that composed function to a sequence of values. Where we end up is












The interesting part is the final stanza. What are foldl1 and composer? Lets work our way outward. The composer is a functor that takes two boost::functions and returns a boost::function that composes them. It assumes that each of the functions has the same signature, which in this case is reasonable, since they’ve come out of a single sequence. It binds together the two functions and a lambda placeholder, then converts that to a new boost::function.

class composer





operator()(boost::function f,

boost::function g)


using boost::lambda::_1;

return bind(f, bind(g, _1));



Constructing a version of composer that takes functions with different signatures and generates the correct return type is a little more complicated. Take a look at the example code. The trick is to use a nested struct that does the type computation based on the template arguments of the member function. A conventional template metaprogramming approach, but quite strange the first time you come across it.

The next part is the function foldl1. The unusual name is borrowed from Haskell. Once you’ve learned C++, Java, and Ruby, Haskell is the next language you should learn. Foldl1 is an abbreviation for fold left with one argument. It’s very similar to std::accumulate, with one key difference. Instead of starting with a base value, the first value in the sequence is used. This means that it can not be applied to an empty list, but it does avoid having to come up with an identity value, such as 0 for addition, or 1 for multiplication. We could use the function identity(x){return x;}, but that’s an extra function call we don’t need, and an extra argument to constructing the composed function. And, not all sets have an identity.


typename BinOp>

typename Iter::value_type

foldl1(Iter first,

Iter last,

BinOp oper)


typename Iter::value_type n

= *first++;

return std::accumulate(first,





So, assuming that fns is, say, a std::vector >, then it’s just a matter of pushing the elements of the sequence v through the composed function, and streaming them out through the ostream_iterator.

What is currying? an aha! moment

Since it really is an aha! moment, the best I can do is tell you what led me to it, and hope that helps.

There is a difference between partial application and currying that most experts ignore, because they already understand, but they lie in wait ready to tell you that you have it all wrong.

And you won’t understand because the concepts are >thatI’ll start with Haskell syntax for the type of a function that takes two integers and returns one.

Integer -> Integer -> Integer

in C that would be

int (*T)(int , int )

Now. what about a function that takes an int and returns a function
that takes an int and returns an int?

(using cdecl syntax)
declare T as function (int) returning pointer to function (int) returning int

int (*T(int ))(int )

or, in Haskell
Integer -> Integer -> Integer


Shouldn’t those have different types in Haskell? They look really much more different in C !

I can’t tell the difference in Haskell between a function that takes two ints and returns an int, and a function that takes one int and returns a function that takes one int and returns an int.




a -> b -> c

a function that takes an a and returns a function that maps b to c, or a function that takes an a and a b and returns a c. They are entirely equivalent.