By coincidence today I just had to implement my last post flatten operation of Array Ruby class. What is is funny about it is that when I read Fowler using it as an example of a typical operation of a human interface operation I thought to myself: "Who on earth would need such an operation?" If you don´t know what it does here is a example (Ruby code):
[1, [2, 3], 4, [5, [6, 7], 8], 9].flatten = [1, 2, 3, 4, 5, 6, 7, 8, 9]
And in the end I´ve ended up implementing it myself only a couple of days later of thinking that :)
I am doing some combinations of elements in my current project and my algorithm resulted this kind of collections, that may have elements that are other inner collections. All I needed was this kind of flatten operation, of course I could review my algorithm but this solution required much less effort. I guess the use of this operation is more common than I thought.
Here is the code:
public static Collection flatten(Collection elements)
{
Collection flattened = new LinkedList();
for (Object o : elements) {
if (o instanceof Collection)
flattened.addAll(flatten((Collection) o));
else
flattened.add(o);
}
return flattened;
}
A human interface operation is one that has a very specific usage but even though it is added to your interface against the philosophy of adding only operations of a general need.
By the way, Cedric has recently posted his opinions on human interfaces too.
[1, [2, 3], 4, [5, [6, 7], 8], 9].flatten = [1, 2, 3, 4, 5, 6, 7, 8, 9]
And in the end I´ve ended up implementing it myself only a couple of days later of thinking that :)
I am doing some combinations of elements in my current project and my algorithm resulted this kind of collections, that may have elements that are other inner collections. All I needed was this kind of flatten operation, of course I could review my algorithm but this solution required much less effort. I guess the use of this operation is more common than I thought.
Here is the code:
public static Collection flatten(Collection elements)
{
Collection flattened = new LinkedList();
for (Object o : elements) {
if (o instanceof Collection)
flattened.addAll(flatten((Collection) o));
else
flattened.add(o);
}
return flattened;
}
A human interface operation is one that has a very specific usage but even though it is added to your interface against the philosophy of adding only operations of a general need.
By the way, Cedric has recently posted his opinions on human interfaces too.

Comments