Reactivity in Svelte with previous value

This method uses a little helper function that stores the previous value for you and takes a callback and the initial value as arguments.

  function memory(fn, initial) {
      let oldValue = initial

      return function (newValue) {
          fn(newValue, oldValue)
          oldValue = newValue

  const compare = memory((n, o) => {
      console.log(n, o)

  let value = 0
  $: compare(value)