packages feed

atomo-0.2.1: prelude/parameter.atomo

operator right 0 =!

Parameter = Object clone
Parameter new: v := Parameter clone do:
  { default = v
  }

(p: Parameter) value: _ := p default

macro define: (x: Dispatch) as: root :=
  `(match: ~(x as: Pattern) on: (Parameter new: ~root))

(p: Parameter) show :=
  "<" .. p _? show .. ">"

(p: Parameter) _? := p value: self

(p: Parameter) =! v :=
  (p) value: (self) = v

(p: Parameter) set-default: v :=
  (p) default = v

with: (p: Parameter) as: new do: (action: Block) :=
  { old = p _?
    action before: { p =! new } after: { p =! old }
  } call

with-default: (p: Parameter) as: new do: (action: Block) :=
  { old-default = p default
    old = p _?
    action before: { p set-default: new; p =! new } after: {
      p set-default: old-default
      p =! old
    }
  } call

macro with: (bs: List) do: action :=
  bs contents
    (reduce-right:
      { `(~a -> ~b) x |
        `{ with: ~a as: ~b do: ~x }
      } with: action)
    contents head

macro with-defaults: (bs: List) do: action :=
  bs contents
    (reduce-right:
      { `(~a -> ~b) x |
        `{ with-default: ~a as: ~b do: ~x }
      } with: action)
    contents head

macro modify: param as: change do: action :=
  `(with: ~param as: (~change call: [~param _?]) do: ~action)