help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: func to remove dupes


From: Dave Korn
Subject: RE: func to remove dupes
Date: Thu, 28 Jun 2007 15:46:43 +0100

On 28 June 2007 14:40, Stephan Beal wrote:

> On Thursday 28 June 2007, John Graham-Cumming wrote:
>> You could always use the function 'uniq' which is in my GMSL.  Here's the
>> definition: 
>> 
>> uniq = $(if $1,$(call uniq,$(call chop,$1)) $(if $(filter $(call
>> last,$1),$(call chop,$1)),,$(call last,$1)))
> 
> The list's Christoph Schulz sent a nice implementation which uses fewer
> $(call)s:
> 
> remove-dupes = $(if $1,$(strip $(word 1,$1) $(call $0,$(filter-out
> $(word 1,$1),$1))))
> 
> Pretty elegant, IMO :).
> 
> (In any case, both yours and Christoph's implementations are far more
> elegant than mine!)

  I'm not 100% sure about that myself... is it really more efficient to
recurse your way up the stack just in order to save yourself the use of a
temporary variable, rather than using a temporary and iterating?  Or do we
just assume that the difference between iteration and recursion will be
swamped in the noise of what is essentially a quadratic algorithm anyway?

  Funny time for this thread to come up, I had to do exactly this just the
other day myself....


# Filter out all duplicate words from TEXT
# Usage: $(call filter-dups,TEXT)
define filter-dups
$(strip $(eval __lib_tmp:=)\
    $(foreach word,$(1),$(if $(filter-out $(__lib_tmp),$(word)),$(word),)\
                        $(eval __lib_tmp:=$(__lib_tmp) $(word))\
    )\
)
endef

  Hm.  I could make that more efficient by only appending the new $word to the
list if it didn't already match, which I think I could do by moving the second
eval into the THEN-PART of the $(if ...).


    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....





reply via email to

[Prev in Thread] Current Thread [Next in Thread]