1
-
2
- struct State{T} backing:: T end
3
- State (st:: State ) = st
4
- backing (st:: State ) = getfield (st, :backing )
5
- function Base. show (io:: IO , st:: State )
6
- print (io, " State(" )
7
- show (IOContext (io, :compact => true ), backing (st))
8
- print (io, " )" )
9
- end
10
-
11
1
patch (x, x̄) = x .- x̄
12
2
13
3
function state (o, x)
@@ -17,30 +7,28 @@ function state(o, x)
17
7
return nothing
18
8
else
19
9
x, _ = functor (x)
20
- return State ( map (x -> state (o, x), x) )
10
+ return map (x -> state (o, x), x)
21
11
end
22
12
end
23
13
24
- function update (o, st, x, x̄s... )
14
+ function _update (o, st, x, x̄s... )
25
15
st, x̄ = apply (o, st, x, x̄s... )
26
16
return st, patch (x, x̄)
27
17
end
28
18
29
- function update (o, state:: State , x:: T , x̄s... ) where T
19
+ function update (o, state, x:: T , x̄s... ) where T
30
20
if all (isnothing, x̄s)
31
21
return state, x
32
22
elseif isnumeric (x)
33
- return update (o, backing ( state) , x, x̄s... )
23
+ return _update (o, state, x, x̄s... )
34
24
else
35
25
x̄s = map (x̄ -> functor (typeof (x), x̄)[1 ], x̄s)
36
26
x, restructure = functor (typeof (x), x)
37
- xstate = map ((state, x, x̄s... ) -> update (o, State ( state) , x, x̄s... ), backing ( state) , x, x̄s... )
38
- return State ( map (first, xstate) ), restructure (map (last, xstate))
27
+ xstate = map ((state, x, x̄s... ) -> update (o, state, x, x̄s... ), state, x, x̄s... )
28
+ return map (first, xstate), restructure (map (last, xstate))
39
29
end
40
30
end
41
31
42
- update (:: State , args... ) = throw (ArgumentError (" the optimiser state must be the 2nd argument" ))
43
-
44
32
# default all rules to first order calls
45
33
apply (o, state, x, dx, dxs... ) = apply (o, state, x, dx)
46
34
0 commit comments