@@ -354,20 +354,42 @@ fn deepRealizeAt(rt: *Runtime, env: *env_mod.Env, v: Value, depth: i64) anyerror
354354 // no-op" — deepRealize returns the same Value when nothing was lazy.
355355 .array_map , .hash_map = > {
356356 var out = v ;
357- var ks = try map_collection .keys (rt , v );
358- while (ks .tag () == .list and list_collection .countOf (ks ) > 0 ) : (ks = list_collection .rest (ks )) {
359- const k = list_collection .first (ks );
360- const val = try map_collection .get (v , k );
357+ // Walk ENTRIES, not keys-then-get: a key that is not `=` to itself
358+ // (a list holding ##NaN) misses its own lookup, so `get` returned
359+ // nil, the re-assoc below then ADDED a phantom `k nil` entry and
360+ // the map printed one entry too many.
361+ // A re-KEYED entry rebuilds the map from its realized entries
362+ // instead of dissoc+assoc: `dissoc` of a self-unequal key is a
363+ // no-op, which also left a phantom.
364+ var rekeyed = false ;
365+ var es = try map_collection .seq (rt , v );
366+ while (es .tag () == .list and list_collection .countOf (es ) > 0 ) : (es = list_collection .rest (es )) {
367+ const entry = list_collection .first (es );
368+ const k = map_entry_collection .keyOf (entry );
369+ const val = map_entry_collection .valOf (entry );
361370 const rk = try deepRealizeAt (rt , env , k , depth + 1 );
362371 const rv = try deepRealizeAt (rt , env , val , depth + 1 );
363372 if (@intFromEnum (rk ) != @intFromEnum (k )) {
364- out = try map_collection . dissoc ( rt , out , k ) ;
365- out = try map_collection . assoc ( rt , out , rk , rv ) ;
373+ rekeyed = true ;
374+ break ;
366375 } else if (@intFromEnum (rv ) != @intFromEnum (val )) {
367376 out = try map_collection .assoc (rt , out , k , rv );
368377 }
369378 }
370- return out ;
379+ if (! rekeyed ) return out ;
380+ var rebuilt = map_collection .empty ();
381+ var rs = try map_collection .seq (rt , v );
382+ while (rs .tag () == .list and list_collection .countOf (rs ) > 0 ) : (rs = list_collection .rest (rs )) {
383+ const entry = list_collection .first (rs );
384+ rebuilt = try map_collection .assoc (
385+ rt ,
386+ rebuilt ,
387+ try deepRealizeAt (rt , env , map_entry_collection .keyOf (entry ), depth + 1 ),
388+ try deepRealizeAt (rt , env , map_entry_collection .valOf (entry ), depth + 1 ),
389+ );
390+ }
391+ const m = map_collection .metaOf (v );
392+ return if (m .isNil ()) rebuilt else try map_collection .withMeta (rt , rebuilt , m );
371393 },
372394 // Sorted variants ride the sorted module (comparator-aware assoc/
373395 // dissoc need env; a realized KEY is not re-keyed — the comparator
@@ -388,14 +410,22 @@ fn deepRealizeAt(rt: *Runtime, env: *env_mod.Env, v: Value, depth: i64) anyerror
388410 return out ;
389411 },
390412 .hash_set = > {
391- var out = v ;
413+ const out = v ;
392414 var es = try set_collection .seq (rt , v );
393415 while (es .tag () == .list and list_collection .countOf (es ) > 0 ) : (es = list_collection .rest (es )) {
394416 const e = list_collection .first (es );
395417 const re = try deepRealizeAt (rt , env , e , depth + 1 );
396418 if (@intFromEnum (re ) != @intFromEnum (e )) {
397- out = try set_collection .disj (rt , out , e );
398- out = try set_collection .conj (rt , out , re );
419+ // `disj` of a self-unequal element (a list holding
420+ // ##NaN) is a no-op, so disj+conj printed a phantom
421+ // second element. Rebuild from the realized elements.
422+ var rebuilt = set_collection .empty ();
423+ var rs = try set_collection .seq (rt , v );
424+ while (rs .tag () == .list and list_collection .countOf (rs ) > 0 ) : (rs = list_collection .rest (rs )) {
425+ rebuilt = try set_collection .conj (rt , rebuilt , try deepRealizeAt (rt , env , list_collection .first (rs ), depth + 1 ));
426+ }
427+ const m = set_collection .metaOf (v );
428+ return if (m .isNil ()) rebuilt else try set_collection .withMeta (rt , rebuilt , m );
399429 }
400430 }
401431 return out ;
0 commit comments