Hyperlinkv0.8.0-beta.28

MutableRef

MutableRef.decrementAndGetconsteffect/MutableRef.ts:247
(self: MutableRef<number>): number

Decrements a numeric MutableRef by 1 and returns the new value.

When to use

Use to decrement a numeric MutableRef and immediately read the updated value.

Example (Decrementing and reading refs)

import { MutableRef } from "effect"

const counter = MutableRef.make(5)

// Decrement and get the new value
const newValue = MutableRef.decrementAndGet(counter)
console.log(newValue) // 4
console.log(MutableRef.get(counter)) // 4

// Use in expressions
const lives = MutableRef.make(3)
console.log(`Lives remaining: ${MutableRef.decrementAndGet(lives)}`) // "Lives remaining: 2"

// Conditional logic based on decremented value
const attempts = MutableRef.make(3)
while (MutableRef.decrementAndGet(attempts) >= 0) {
  console.log("Retrying...")
  // retry logic
}
numeric
export const decrementAndGet = (self: MutableRef<number>): number => updateAndGet(self, (n) => n - 1)