clojure-dev

Ben Sless 2023-02-04T11:44:12.985649Z

Is there anything I need to do when I comment to replies on Ask to make sure the original commenter sees them?

Alex Miller (Clojure team) 2023-02-05T18:30:14.532569Z

if you reply to an answer or comment, those in the thread should get an email (unless they turned that off)

dmiller 2023-02-04T14:53:40.974819Z

APersistentVector$Seq implements IReduce; APersistent$RSeq does not. Is there a reason for this? (While I'm nit-picking, ...`RSeq extends ASeq implements IndexedSeq, Counted` , the Counted being redundant.) (I doubt either of these has affected anyone's enjoyment of life, other than myself. I went ahead an implemented IReduce in ClojureCLR for RSeq and have been living with my own FUD ever since.)

😆 1
dmiller 2023-02-06T13:50:47.016309Z

While on the topic of reduce, in ArrayChunk we have

public Object reduce(IFn f, Object start) {
		Object ret = f.invoke(start, array[off]);
		if(RT.isReduced(ret))
			return ret;
		for(int x = off + 1; x < end; x++)
			{
			ret = f.invoke(ret, array[x]);
			if(RT.isReduced(ret))
				return ret;
			}
		return ret;
}
This is the only place I can find in the source where RT.isReduced is called and if true, the Reduced object is not deref()'d. (Ignoring TransformerIterator , where passing the Reduced instance out seems reasonble.) Is there something special about the places were reduce might be called on an ArrayChunk?

Alex Miller (Clojure team) 2023-02-06T14:13:19.495409Z

In short, yes - this is a different reduce method (part of IChunk), than every other reduce (IReduce, CollReduce)

dmiller 2023-02-06T14:40:58.977069Z

That's what I was missing. Even with that hint, it took me a bit to piece it together. (The CollReduce bone connects to the InternalReduce bone connects to the IChunkedSeq bone. I think. ) Thx.

Alex Miller (Clojure team) 2023-02-06T14:42:13.748889Z

yes

Alex Miller (Clojure team) 2023-02-06T14:42:45.913239Z

I have made the mistake of implementing both interfaces in the same object which caused some very subtle errors that took a long time to figure out

Alex Miller (Clojure team) 2023-02-06T14:42:54.458519Z

(wrt reduced)

dmiller 2023-02-06T14:53:43.480759Z

So, before I commit myself to an error, is the following correct for reduce in IReduceInit and IReduce (but not IChunkedSeq.reduce): One essential rule when writing a reduce method: after each invocation of the reduction function, check the result to see if it is an instance of Reduced; if so, stop immediately and return the deref value.

Alex Miller (Clojure team) 2023-02-06T14:59:32.073099Z

yes

dmiller 2023-02-06T15:04:08.783469Z

Thanks. Planning on that note for my next blog post. Not much information out there about clojure.lang.Reduced The Transducers article seems to be about it. I may have missed others.

Alex Miller (Clojure team) 2023-02-06T15:05:50.461219Z

there is all the trickiness in expanding transducers that may do a sub-reduce that need to re-wrap reduced, but that doesn't change the rule above

dmiller 2023-02-06T15:08:37.801539Z

Fortunately, all that complication lies out in the Clojure source code, which I can mostly ignore at this stage -- focusing just on the Java/C# parts. Except of course for solving my confusion about ArrayChunk .

Alex Miller (Clojure team) 2023-02-05T18:30:48.009799Z

> Is there a reason for this? no, probably not

dmiller 2023-02-06T02:11:14.656569Z

Good. Now I can sleep more easily.

😄 1