-module(eval@context). -compile(no_auto_import). -export([get/0, set/1, then_set/2, modify/1, update/2]). -spec get() -> eval:eval(EYH, any(), EYH). get() -> eval:from(fun(Ctx) -> {Ctx, {ok, Ctx}} end). -spec set(EYM) -> eval:eval(nil, any(), EYM). set(Ctx) -> eval:from(fun(_) -> {Ctx, {ok, nil}} end). -spec then_set(eval:eval(EYR, EYS, EYT), EYT) -> eval:eval(EYR, EYS, EYT). then_set(Eval, Ctx) -> update(Eval, fun(_, _) -> Ctx end). -spec modify(fun((EZA) -> EZA)) -> eval:eval(nil, any(), EZA). modify(F) -> _pipe = get(), eval:then(_pipe, fun(Ctx) -> set(F(Ctx)) end). -spec update(eval:eval(EZF, EZG, EZH), fun((EZH, EZF) -> EZH)) -> eval:eval(EZF, EZG, EZH). update(Eval, F) -> _pipe = eval:map2(Eval, get(), fun(A, Ctx) -> {Ctx, A} end), eval:then( _pipe, fun(Pair) -> {Ctx@1, A@1} = Pair, _pipe@1 = set(F(Ctx@1, A@1)), eval:map(_pipe@1, fun(_) -> A@1 end) end ).