mapDistinct
The status menu has an icon that shows "Healthy", "Wounded", or "Dying" depending on health. In a fight, health drops almost every second, but the icon only changes once in a while.
Deriving this status with map redraws the icon on every lost point of health, and nearly all of those redraws look exactly the same.
What mapDistinct doesโ
mapDistinct computes a new value from the original, just like map. The extra step is a comparison: when this result equals the previous one, downstream is not notified.
Results are compared with Objects.equals by default, or with a comparison function you pass in.
Refresh only when the condition changesโ
Above 10 health is healthy, above 4 is wounded, and anything lower is dying. Click "Next" to run the code line by line and see when the icon refreshes.
Full health, the condition is healthy.
MutableSignal<Integer> health = Signal.of(20);
Signal<String> condition = health.mapDistinct(value ->
value > 10 ? "Healthy" : value > 4 ? "Wounded" : "Dying"
);
Subscription subscription = condition.onDirty(() -> {
System.out.println("Now: " + condition.get());
});
health.set(16); // still healthy, prints nothing
health.set(8); // prints "Now: Wounded"
health.set(6); // still wounded, prints nothing
health.set(3); // prints "Now: Dying"
health.set(2); // still dying, prints nothing
subscription.close();

health | 20 | |
|---|---|---|
condition | Healthy |
Update only when entering a new chunkโ
A land-claim hint only cares which chunk the player stands in, not the exact coordinates. Shifting block coordinates right by 4 gives chunk coordinates, and records implement equals, so two ChunkPos with the same coordinates count as the same result.
The player is in chunk (0, 0).
record BlockPos(int x, int z) {}
record ChunkPos(int x, int z) {}
MutableSignal<BlockPos> position = Signal.of(new BlockPos(3, 3));
Signal<ChunkPos> chunk = position.mapDistinct(pos ->
new ChunkPos(pos.x() >> 4, pos.z() >> 4)
);
Subscription subscription = chunk.onDirty(() -> {
System.out.println("Entered chunk " + chunk.get());
});
position.set(new BlockPos(9, 3)); // still chunk (0, 0), prints nothing
position.set(new BlockPos(15, 12)); // still chunk (0, 0), prints nothing
position.set(new BlockPos(16, 12)); // prints "Entered chunk ChunkPos[x=1, z=0]"
position.set(new BlockPos(20, -1)); // prints "Entered chunk ChunkPos[x=1, z=-1]"
subscription.close();

position | (3, 3) | |
|---|---|---|
chunk | (0, 0) |
Custom result comparisonโ
Chat channel names ignore case, and surrounding spaces do not count. Pass a comparison function as the second argument.
MutableSignal<String> input = Signal.of("moon");
Signal<String> channel = input.mapDistinct(String::strip, String::equalsIgnoreCase);
Subscription subscription = channel.onDirty(() -> {
System.out.println("Switched to channel: " + channel.get());
});
input.set(" MOON "); // equal to moon once trimmed, prints nothing
input.set(" trade "); // prints "Switched to channel: trade"
subscription.close();
The input box's own input still holds " MOON "; only the derived channel ignores that change.
Caveatsโ
mapDistinct has to compute the result before it can compare, so while subscribed, it computes on every source change. map can wait until the value is read.
Use map when the result changes almost every time, such as health text or countdown seconds. Use mapDistinct when it often stays the same, such as a condition, the current chunk, or a "can afford" boolean.
While subscribed, the function runs on the thread that wrote the source, to decide whether to notify. Keep it short and compute only from its argument.
The comparison follows the same rules as Signal.of: an equivalence relation, short, no side effects.