It can be dicey to invite comparisons between programming languages, as I did last week in an entry on data munging with PowerShell. But in this case, although I didn’t at first articulate very well what I found interesting about the example from Lee Holmes, by way of Scott Hanselman, the commentary helped me figure that out.
Among the questions that arose:
Can PowerShell express things as tersely as other dynamic languages? Nearly so. Does that matter? Lee Holmes writes:
Once you have a solution that works, a natural scripter’s passion is to tinker it down to one line. It’s no longer educational, intelligible, or extendable, but it’s fun.
There may be a bit more to this story. Dynamically-typed scripting languages are naturally terser than their statically-typed counterparts. An oft-cited benefit is that a complete chunk of logic can be seen and understood as a whole thing. Should that chunk be a function, or an entire program? You can legitimately make a tradeoff. Sometimes that chunk needs to be valuable to others in the future, in which case the maximally-terse one-liner isn’t helpful. But sometimes it only needs to be valuable to you right now, in which case the one-liner may actually improve your focus.
More interesting to me was the style of Lee’s PowerShell function. It expressed the operations of selection, grouping, and summarization in a style that felt different to me than what I’m used to seeing, and doing, in Python. I used the word “composable” to describe that difference. Others words might be “declarative,” “SQL-like,” or indeed “LINQ-like”. I wondered if this might an essential or a superficial difference between PowerShell and Python.
Wai Yip Tung’s example, which closely matches Lee Holmes’ example, suggests that it is not an essential difference.
Wai’s example also reminds us that much depends on what you know, or don’t know, about a language’s supporting libraries. And that in turn says something about how the libraries do or don’t manifest themselves to us. As Wai says, if Python’s obscure itemgetter function had instead been called select1, and presented as part of a module that includes groupby, Python programmers would be more likely to produce scripts in the style of the PowerShell example.
Finally, Wai speculates on how PowerShell’s pipelining style might be captured in Python, and whether or not that might be broadly useful. I don’t know the answer to that, but I think it’s an interesting question.
1 Note: The select(0,1) idiom shown in Wai’s example is new in Python 2.5, to which I have not yet upgraded. In Python 2.4 you can only select one thing.