@@ -4,49 +4,87 @@ using Test
44a (x) = x^ 2
55greet (greeting, name, punctuation) = " $(greeting) , $(name)$(punctuation) "
66
7- @testset " Partial functions" begin
8- @test map ((+ )$ 2, [1 ,2 ,3 ]) == [3 , 4 , 5 ]
9- @test repr (map $ a) == " map(a, ...)"
10- @test (map $ a)([1 , 2 , 3 ]) == [1 , 4 , 9 ]
11-
12- @test greet (" Hello" , " Bob" , " !" ) == " Hello, Bob!"
13- sayhello = greet $ " Hello"
14- @test repr (sayhello) == " greet(\" Hello\" , ...)"
15- @test repr (" text/plain" , sayhello) == repr (sayhello)
16-
17- @test sayhello (" Bob" , " !" ) == " Hello, Bob!"
18- hi_bob = greet $ " Hi" $ " Bob" $ " !"
19- @test hi_bob isa PartialFunctions. PartialFunction{typeof (greet), Tuple{String, String, String}, NamedTuple{(), Tuple{}}}
20- @test hi_bob < | () == " Hi, Bob!"
21- @test sayhello < | (" Jimmy" , " ?" ). .. == " Hello, Jimmy?"
22-
23- @test greet $ (" Hi" , " Bob" ) < | " !" == " Hi, Bob!"
24- end
7+ @testset " PartialFunctions.jl" begin
8+ @testset " Partial functions" begin
9+ @test map ((+ )$ 2, [1 ,2 ,3 ]) == [3 , 4 , 5 ]
10+ @test repr (map $ a) == " map(a, ...)"
11+ @test (map $ a)([1 , 2 , 3 ]) == [1 , 4 , 9 ]
12+
13+ @test greet (" Hello" , " Bob" , " !" ) == " Hello, Bob!"
14+ sayhello = greet $ " Hello"
15+ @test repr (sayhello) == " greet(\" Hello\" , ...)"
16+ @test repr (" text/plain" , sayhello) == repr (sayhello)
2517
26- @testset " Reversed functions" begin
27- revmap = flip (map)
28- @test flip (revmap) == map
29- @test revmap ([1 ,2 ,3 ], sin) == map (sin, [1 ,2 ,3 ])
30-
31- func (x, y) = x - y
32- func (x, y, z) = x - y - z
33- @test func (1 , 2 ) == - 1
34- @test func (1 , 3 , 6 ) == - 8
35- flipped = flip (func)
36- @test flipped (2 , 1 ) == - 1
37- @test flipped (3 , 2 , 1 ) == - 4
38- end
18+ @test sayhello (" Bob" , " !" ) == " Hello, Bob!"
19+ hi_bob = greet $ " Hi" $ " Bob" $ " !"
20+ @test hi_bob isa PartialFunctions. PartialFunction{nothing , nothing , typeof (greet), Tuple{String, String, String}, NamedTuple{(), Tuple{}}}
21+ @test hi_bob < | () == " Hi, Bob!"
22+ @test sayhello < | (" Jimmy" , " ?" ). .. == " Hello, Jimmy?"
23+
24+ @test greet $ (" Hi" , " Bob" ) < | " !" == " Hi, Bob!"
25+ end
26+
27+ @testset " Reversed functions" begin
28+ revmap = flip (map)
29+ @test flip (revmap) == map
30+ @test revmap ([1 ,2 ,3 ], sin) == map (sin, [1 ,2 ,3 ])
31+
32+ func (x, y) = x - y
33+ func (x, y, z) = x - y - z
34+ @test func (1 , 2 ) == - 1
35+ @test func (1 , 3 , 6 ) == - 8
36+ flipped = flip (func)
37+ @test flipped (2 , 1 ) == - 1
38+ @test flipped (3 , 2 , 1 ) == - 4
39+ end
40+
41+ @testset " Keyword Arguments" begin
42+ a = [[1 ,2 ,3 ], [1 ,2 ]]
43+ sort_by_length = sort $ (; by = length)
44+ @test sort (a, by = length) == sort_by_length (a)
45+
46+ sort_a_by_length = sort $ (a, (;by = length))
47+ @test sort (a, by = length) == sort_a_by_length ()
3948
40- @testset " Keyword Arguments" begin
41- a = [[1 ,2 ,3 ], [1 ,2 ]]
42- sort_by_length = sort $ (; by = length)
43- @test sort (a, by = length) == sort_by_length (a)
49+ sort_a_by_length_2 = sort $ ((a,), (;by = length))
50+ @test sort_a_by_length == sort_a_by_length_2
4451
45- sort_a_by_length = sort $ (a, (; by = length))
46- @test sort (a, by = length) == sort_a_by_length ()
52+ @test repr ( sort_a_by_length) == " sort([[1, 2, 3], [1, 2]], ...; by = length, ...) "
53+ end
4754
48- sort_a_by_length_2 = sort $ ((a,), (;by = length))
49- @test sort_a_by_length == sort_a_by_length_2
55+ @testset " Generalized Partial Functions" begin
56+ @test map (@$ (+ (2 , _)), [1 ,2 ,3 ]) == [3 , 4 , 5 ]
57+ @test map (@$ (+ (_, 2 )), [1 ,2 ,3 ]) == [3 , 4 , 5 ]
58+ @test repr (@$ (map (a, _))) == " map(a, _)"
59+ @test (@$ (map (a, _)))([1 , 2 , 3 ]) == [1 , 4 , 9 ]
60+
61+ @test greet (" Hello" , " Bob" , " !" ) == " Hello, Bob!"
62+ sayhello = @$ greet (" Hello" , _, _)
63+ @test repr (sayhello) == " greet(\" Hello\" , _, _)"
64+ @test repr (" text/plain" , sayhello) == repr (sayhello)
5065
51- @test repr (sort_a_by_length) == " sort([[1, 2, 3], [1, 2]], ...; by = length, ...)"
52- end
66+ @test sayhello (" Bob" , " !" ) == " Hello, Bob!"
67+
68+ sayhellobob = sayhello (" Bob" )
69+ @test repr (sayhellobob) == " greet(\" Hello\" , \" Bob\" , ...)"
70+ @test sayhellobob (" !" ) == " Hello, Bob!"
71+
72+ hi_bob = @$ (@$ (greet (" Hi" , _, _))(" Bob" , _))
73+ @test @$ (hi_bob (" !" )) == " Hi, Bob!"
74+ @test hi_bob isa PartialFunctions. PartialFunction
75+ @test sayhello < | (" Jimmy" , " ?" ). .. == " Hello, Jimmy?"
76+
77+ @test hi_bob < | " !" == " Hi, Bob!"
78+
79+ @testset " Keyword Arguments" begin
80+ a = [[1 ,2 ,3 ], [1 ,2 ]]
81+ sort_by_length = @$ (sort (_; by = length))
82+ @test sort (a, by = length) == sort_by_length (a)
83+
84+ sorted_a_by_length = @$ (sort (a; by = length))
85+ @test sort (a, by = length) == sorted_a_by_length
86+
87+ @test repr (sort_by_length) == " sort(_; by = length)"
88+ end
89+ end
90+ end
0 commit comments