Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!news-transit.tcx.org.uk!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Joshua Cranmer Newsgroups: comp.lang.java.programmer Subject: Re: Refactoring discovery Date: Wed, 30 Mar 2011 17:31:53 -0400 Organization: A noiseless patient Spider Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 30 Mar 2011 21:31:54 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="LtjcJP1H6uHOtkcPMh0bUA"; logging-data="16570"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+tBMsDxBTlbN7IxA/Yra4wRJ8Zre/kWpI=" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.16pre) Gecko/20110305 Lightning/1.0b3pre Thunderbird/3.1.10pre In-Reply-To: Cancel-Lock: sha1:9r3GOendUZMwzw+jhi7zC69H9y0= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:2613 On 03/30/2011 04:10 PM, David Lamb wrote: > We already write "combined operations" in other contexts. > if (x.hasKey(y)) then x.addPair(y, > someVeryBigThingIDidn'tWantToSysnthesizeUnlessIHadTo) Well, another way to implement this is: public boolean hasKey(Y y) { LocPtr ptr = getWhereToInsert(y); this.cachedPtr = ptr; return ptr.valid(); } public void addPair(Y y, Object obj) { LocPtr ptr; if (cachedPtr && cachedPtr.y == y) ptr = cachedPtr; else ptr = getWhereToInsert(y); ptr.insert(obj); } Which pretty much gets you the "benefit" of combining them into one single operation without needing to provide all of the back-level predicate code operations (set-if-present, set-if-not-present, etc.) -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth