Re: Seeking computer-programming job (Sunnyvale, CA)
On 2009-05-15, Seamus MacRae <smacrae319@live.ca.nospam> wrote:
eric-and-jane-smith wrote:
Seamus MacRae <smacrae319@live.ca.nospam> wrote in
news:guhr3h$ffm$1@news.motzarella.org:
If it merely transforms the list, and doesn't care whether they're
runnable, it should not matter. Suppose it sorts the list by priority.
But what if it does care? What if it takes a list of a million objects,
and deletes 17 of them from that list, because those 17 aren't runnable?
In Common Lisp, deleting 17 objects from a list of a million can be done
quickly and efficiently. But you seem to be implying you have to copy the
other 999983 objects to a list of runnables, because otherwise they would
be in a list of objects, even though they're all runnable.
Not in Java. In Java, you can use:
/**
* Removes non-<code>Runnable</code> objects from <code>list</code>
* and returns pruned list as a <code>List<Runnable></code>;
* caller is responsible for synchronizing the list if necessary.
*/
@SuppressWarnings("unchecked")
/*
* Justification: the only unchecked cast is in the return
* line, and all non-Runnables have been removed from the
* list at that time. The returned list is a list of
* Runnables unless a data race occurred. Correct behavior,
* as ever, depends on the caller synchronizing where
* appropriate.
*/
public List<Runnable> pruneList (List<?> list) {
for (Iterator<?> i = list.iterator(); i.hasNext();) {
// Old "for" used intentionally
Object o = i.next();
if (!(o instanceof Runnable)) i.remove();
}
return (List<Runnable>)list;
}
Untested.
The above vomit maens:
;; By default, all things are assumed non-runnable.
(defmethod is-runnable (obj) nil)
;; But things subclassed from runnable, well, are.
(defmethod is-runnable ((obj runnable-class)) t)
;; Function call to remove non-runnables from a some-list, returning
;; a new list with only runnables in it:
(remove-if-not 'is-runnable some-list)
REMOVE-IF-NOT is a functional construct which leaves the old list alone
(but the new list may share substructure with the old). If you
want to modify the original list, use DELETE-IF-NOT.
(setf some-list (delete-if-not 'is-runnable some-list))
Obviously correct by inspection.