Re: [Wrapl-discussion] list:delete, list:remove
Brought to you by:
rajamukherji
|
From: Raja M. <raj...@gm...> - 2010-09-03 06:10:36
|
Hi Roman,
> Do you think it is practical to return the removed value from
> list:delete and
> list:remove methods? I mean, instead of returning the remaining list.
> Perhaps, you have already wrote some working code that uses these
> methods that
> way. What are your reasons?
>
> My thoughts:
> --------------------------------------------------------------------------------
> :copy, :empty, :insert, :reverse, :shift, :sort - all return the
> modified list.
> Why :delete and :remove should return the value?
I think when I wrote :delete for lists, I wanted it to return the
corresponding removed value for efficiency, the value can be used
without a separate index operation first. I also had this in mind for
:delete on tables, but ended up returning the table there instead. I
think that :delete should be consistent across lists/tables, but I'm
still of the opinion that the removed value should be returned in both
cases.
> :pop, :pull, :push, :put - are stack-style methods - they shouldn't be
> a pattern.
>
> In case of list:remove the value to be removed is already known to be
> returned.
As for :remove, if the removed value is returned from a generator, it
won't be known before it is removed. Since :remove fails if no matching
value is found, I think it's more appropriate to return the value rather
the list/table. For example,
WITH removed_item <- cache:remove(items_by_date()) DO (
...
);
where items_by_date() generates values that may be in cache.
What do you think of this reasoning? Of course, I intend of making the
behaviour consistent between tables and lists.
> As an example:
> --------------------------------------------------------------------------------
> If :delete returns a list, this programming challenge:
> http://www.reddit.com/r/programming/comments/2xdkd/google_interview_questions/c2xe85
> could be solved in one line:
>
> TO :puzzle(list @ List.T) ALL :"*":foldl(ALL list:copy:delete(list:keys));
> -- or
> TO :puzzle(list @ List.T) ALL PROD list:copy:delete(list:keys):values;
>
> [1, 2, 3, 4, 5]:puzzle;
Hmmm, solving a Google programming challenge in one line sounds fun, but
it's not enough to convince me. Now you can have the challenge of
solving it in one line if :delete returns the value instead :)
> By the way, there is a typo in :shift description:
> --------------------------------------------------------------------------------
> :shift(list @ T, n @ Std.Integer.SmallT, r @ Std.Integer.SmallT) : T
> shifts the nth element by n positions within list
> ->
> shifts the nth element by r positions within list
Thanks for this (and the previous typos/errors). I've encountered a
small problem while upgrading the Hans Boehm garbage collector to the
latest CVS version, but as soon as that's resolved, I'll release a new
version with all these fixes.
Raja
|