1- struct Foo
2- x
3- y
4- end
1+
2+ struct Foo; x; y; end
53@functor Foo
64
7- struct Bar
8- x
9- end
5+ struct Bar; x; end
106@functor Bar
117
12- struct Baz
13- x
14- y
15- z
16- end
17- @functor Baz (y,)
8+ struct OneChild3; x; y; z; end
9+ @functor OneChild3 (y,)
1810
19- struct NoChildren
20- x
21- y
22- end
11+ struct NoChildren2; x; y; end
2312
2413@static if VERSION >= v " 1.6"
2514 @testset " ComposedFunction" begin
3120 end
3221end
3322
23+ # ##
24+ # ## Basic functionality
25+ # ##
26+
3427@testset " Nested" begin
3528 model = Bar (Foo (1 , [1 , 2 , 3 ]))
3629
5346 @test fmap (f, x; exclude = x -> x isa AbstractArray) == x
5447end
5548
49+ @testset " Property list" begin
50+ model = OneChild3 (1 , 2 , 3 )
51+ model′ = fmap (x -> 2 x, model)
52+
53+ @test (model′. x, model′. y, model′. z) == (1 , 4 , 3 )
54+ end
55+
56+ # ##
57+ # ## Extras
58+ # ##
59+
5660@testset " Walk" begin
5761 model = Foo ((0 , Bar ([1 , 2 , 3 ])), [4 , 5 ])
5862
5963 model′ = fmapstructure (identity, model)
6064 @test model′ == (; x= (0 , (; x= [1 , 2 , 3 ])), y= [4 , 5 ])
6165end
6266
63- @testset " Property list" begin
64- model = Baz (1 , 2 , 3 )
65- model′ = fmap (x -> 2 x, model)
66-
67- @test (model′. x, model′. y, model′. z) == (1 , 4 , 3 )
68- end
69-
7067@testset " fcollect" begin
7168 m1 = [1 , 2 , 3 ]
7269 m2 = 1
7875
7976 m1 = [1 , 2 , 3 ]
8077 m2 = Bar (m1)
81- m0 = NoChildren (:a , :b )
78+ m0 = NoChildren2 (:a , :b )
8279 m3 = Foo (m2, m0)
8380 m4 = Bar (m3)
8481 @test all (fcollect (m4) .=== [m4, m3, m2, m1, m0])
8986 @test all (fcollect (m3) .=== [m3, m1, m2])
9087end
9188
89+ # ##
90+ # ## Vararg forms
91+ # ##
92+
93+ @testset " fmap(f, x, y)" begin
94+ @test true # TODO
95+ end
96+
97+ @testset " old test update.jl" begin
98+ struct M{F,T,S}
99+ σ:: F
100+ W:: T
101+ b:: S
102+ end
103+
104+ @functor M
105+
106+ (m:: M )(x) = m. σ .(m. W * x .+ m. b)
107+
108+ m = M (identity, ones (Float32, 3 , 4 ), zeros (Float32, 3 ))
109+ x = ones (Float32, 4 , 2 )
110+ m̄, _ = gradient ((m,x) -> sum (m (x)), m, x)
111+ m̂ = Functors. fmap (m, m̄) do x, y
112+ isnothing (x) && return y
113+ isnothing (y) && return x
114+ x .- 0.1f0 .* y
115+ end
116+
117+ @test m̂. W ≈ fill (0.8f0 , size (m. W))
118+ @test m̂. b ≈ fill (- 0.2f0 , size (m. b))
119+ end
120+
121+ # ##
122+ # ## FlexibleFunctors.jl
123+ # ##
124+
92125struct FFoo
93126 x
94127 y
@@ -102,13 +135,13 @@ struct FBar
102135end
103136@flexiblefunctor FBar p
104137
105- struct FBaz
138+ struct FOneChild4
106139 x
107140 y
108141 z
109142 p
110143end
111- @flexiblefunctor FBaz p
144+ @flexiblefunctor FOneChild4 p
112145
113146@testset " Flexible Nested" begin
114147 model = FBar (FFoo (1 , [1 , 2 , 3 ], (:y , )), (:x ,))
132165end
133166
134167@testset " Flexible Property list" begin
135- model = FBaz (1 , 2 , 3 , (:x , :z ))
168+ model = FOneChild4 (1 , 2 , 3 , (:x , :z ))
136169 model′ = fmap (x -> 2 x, model)
137170
138171 @test (model′. x, model′. y, model′. z) == (2 , 2 , 6 )
147180 @test all (fcollect (m4, exclude = x -> x isa Array) .=== [m4, m3])
148181 @test all (fcollect (m4, exclude = x -> x isa FFoo) .=== [m4])
149182
150- m0 = NoChildren (:a , :b )
183+ m0 = NoChildren2 (:a , :b )
151184 m1 = [1 , 2 , 3 ]
152185 m2 = FBar (m1, ())
153186 m3 = FFoo (m2, m0, (:x , :y ,))
0 commit comments