@@ -3,16 +3,16 @@ use crate::value::{ToValue, Value};
3
3
use std:: rc:: Rc ;
4
4
5
5
use crate :: error_message;
6
- use crate :: type_tag :: TypeTag ;
6
+ use crate :: persistent_list :: PersistentList :: Cons ;
7
7
use crate :: persistent_list:: ToPersistentListIter ;
8
8
use crate :: persistent_vector:: ToPersistentVectorIter ;
9
+ use crate :: type_tag:: TypeTag ;
9
10
use itertools:: Itertools ;
10
- use crate :: persistent_list:: PersistentList :: Cons ;
11
11
12
12
/// clojure.string/join ; joins a coll of items together as a string
13
13
/// (join
14
14
/// [coll]
15
- /// [separator coll])
15
+ /// [separator coll])
16
16
#[ derive( Debug , Clone ) ]
17
17
pub struct JoinFn { }
18
18
impl ToValue for JoinFn {
@@ -25,8 +25,14 @@ impl IFn for JoinFn {
25
25
if args. len ( ) == 1 || args. len ( ) == 2 {
26
26
let separator = if args. len ( ) == 2 {
27
27
args. get ( 0 ) . unwrap ( ) . to_string ( )
28
- } else { String :: from ( "" ) } ;
29
- let coll = if args. len ( ) == 1 { args. get ( 0 ) } else { args. get ( 1 ) } ;
28
+ } else {
29
+ String :: from ( "" )
30
+ } ;
31
+ let coll = if args. len ( ) == 1 {
32
+ args. get ( 0 )
33
+ } else {
34
+ args. get ( 1 )
35
+ } ;
30
36
match coll. unwrap ( ) . to_value ( ) {
31
37
Value :: PersistentList ( Cons ( head, tail, count) ) => {
32
38
return if count == 0 {
@@ -35,47 +41,55 @@ impl IFn for JoinFn {
35
41
Value :: String ( head. to_string ( ) )
36
42
} else {
37
43
Value :: String (
38
- String :: from (
39
- head. to_string ( ) ) + separator. as_str ( ) +
40
- tail. iter ( )
41
- . map ( |x| x. to_string ( ) )
42
- . collect :: < Vec < std:: string:: String > > ( )
43
- . join ( & separator) . as_str ( ) )
44
+ String :: from ( head. to_string ( ) )
45
+ + separator. as_str ( )
46
+ + tail
47
+ . iter ( )
48
+ . map ( |x| x. to_string ( ) )
49
+ . collect :: < Vec < std:: string:: String > > ( )
50
+ . join ( & separator)
51
+ . as_str ( ) ,
52
+ )
44
53
}
45
- } ,
54
+ }
46
55
Value :: PersistentVector ( pvec) => {
47
56
return if pvec. vals . len ( ) == 0 {
48
57
Value :: String ( String :: from ( "" ) )
49
58
} else {
50
59
Value :: String ( String :: from (
51
- pvec. vals . iter ( )
60
+ pvec. vals
61
+ . iter ( )
52
62
. map ( |x| x. to_string ( ) )
53
63
. collect :: < Vec < std:: string:: String > > ( )
54
- . join ( & separator) . as_str ( ) ) )
64
+ . join ( & separator)
65
+ . as_str ( ) ,
66
+ ) )
55
67
}
56
68
}
57
- _ => Value :: String ( String :: from ( "" ) )
69
+ _ => Value :: String ( String :: from ( "" ) ) ,
58
70
}
59
71
} else {
60
- return error_message:: wrong_varg_count ( & [ 1 , 2 ] , args. len ( ) ) ;
72
+ return error_message:: wrong_varg_count ( & [ 1 , 2 ] , args. len ( ) ) ;
61
73
}
62
74
}
63
75
}
64
76
65
77
#[ cfg( test) ]
66
78
mod tests {
67
79
mod reverse_tests {
68
- use crate :: value:: Value ;
69
- use std:: rc:: Rc ;
70
80
use crate :: clojure_string:: join:: JoinFn ;
71
81
use crate :: ifn:: IFn ;
72
82
use crate :: persistent_list:: PersistentList ;
73
83
use crate :: persistent_vector:: PersistentVector ;
84
+ use crate :: value:: Value ;
85
+ use std:: rc:: Rc ;
74
86
75
87
#[ test]
76
88
fn join_empty_collection_to_empty_string ( ) {
77
89
let join = JoinFn { } ;
78
- let args = vec ! [ Rc :: new( Value :: PersistentList ( vec![ ] . into_iter( ) . collect:: <PersistentList >( ) ) ) ] ;
90
+ let args = vec ! [ Rc :: new( Value :: PersistentList (
91
+ vec![ ] . into_iter( ) . collect:: <PersistentList >( ) ,
92
+ ) ) ] ;
79
93
assert_eq ! ( Value :: String ( String :: from( "" ) ) , join. invoke( args) ) ;
80
94
}
81
95
@@ -84,7 +98,10 @@ mod tests {
84
98
let join = JoinFn { } ;
85
99
let s = "hello" ;
86
100
let args = vec ! [ Rc :: new( Value :: PersistentList (
87
- vec![ Rc :: new( Value :: String ( String :: from( s) ) ) ] . into_iter( ) . collect:: <PersistentList >( ) ) ) ] ;
101
+ vec![ Rc :: new( Value :: String ( String :: from( s) ) ) ]
102
+ . into_iter( )
103
+ . collect:: <PersistentList >( ) ,
104
+ ) ) ] ;
88
105
assert_eq ! ( Value :: String ( String :: from( "hello" ) ) , join. invoke( args) ) ;
89
106
}
90
107
@@ -93,37 +110,62 @@ mod tests {
93
110
let join = JoinFn { } ;
94
111
let s = "hello" ;
95
112
let args = vec ! [ Rc :: new( Value :: PersistentList (
96
- vec![ Rc :: new( Value :: String ( String :: from( s) ) ) ,
97
- Rc :: new( Value :: I32 ( 5 ) ) ,
98
- Rc :: new( Value :: String ( String :: from( s) ) ) ]
99
- . into_iter( ) . collect:: <PersistentList >( ) ) ) ] ;
100
- assert_eq ! ( Value :: String ( String :: from( "hello5hello" ) ) , join. invoke( args) ) ;
113
+ vec![
114
+ Rc :: new( Value :: String ( String :: from( s) ) ) ,
115
+ Rc :: new( Value :: I32 ( 5 ) ) ,
116
+ Rc :: new( Value :: String ( String :: from( s) ) ) ,
117
+ ]
118
+ . into_iter( )
119
+ . collect:: <PersistentList >( ) ,
120
+ ) ) ] ;
121
+ assert_eq ! (
122
+ Value :: String ( String :: from( "hello5hello" ) ) ,
123
+ join. invoke( args)
124
+ ) ;
101
125
}
102
126
103
127
#[ test]
104
128
fn join_multiple_items_in_collection_with_separator_to_string ( ) {
105
129
let join = JoinFn { } ;
106
130
let s = "hello" ;
107
- let args = vec ! [ Rc :: new( Value :: String ( String :: from( ", " ) ) ) ,
108
- Rc :: new( Value :: PersistentList (
109
- vec![ Rc :: new( Value :: String ( String :: from( s) ) ) ,
110
- Rc :: new( Value :: I32 ( 5 ) ) ,
111
- Rc :: new( Value :: String ( String :: from( s) ) ) ]
112
- . into_iter( ) . collect:: <PersistentList >( ) ) ) ] ;
113
- assert_eq ! ( Value :: String ( String :: from( "hello, 5, hello" ) ) , join. invoke( args) ) ;
131
+ let args = vec ! [
132
+ Rc :: new( Value :: String ( String :: from( ", " ) ) ) ,
133
+ Rc :: new( Value :: PersistentList (
134
+ vec![
135
+ Rc :: new( Value :: String ( String :: from( s) ) ) ,
136
+ Rc :: new( Value :: I32 ( 5 ) ) ,
137
+ Rc :: new( Value :: String ( String :: from( s) ) ) ,
138
+ ]
139
+ . into_iter( )
140
+ . collect:: <PersistentList >( ) ,
141
+ ) ) ,
142
+ ] ;
143
+ assert_eq ! (
144
+ Value :: String ( String :: from( "hello, 5, hello" ) ) ,
145
+ join. invoke( args)
146
+ ) ;
114
147
}
115
148
116
149
#[ test]
117
150
fn join_multiple_items_in_pvec_collection_with_separator_to_string ( ) {
118
151
let join = JoinFn { } ;
119
152
let s = "hello" ;
120
- let args = vec ! [ Rc :: new( Value :: String ( String :: from( ", " ) ) ) ,
121
- Rc :: new( Value :: PersistentVector (
122
- vec![ Rc :: new( Value :: String ( String :: from( s) ) ) ,
123
- Rc :: new( Value :: I32 ( 5 ) ) ,
124
- Rc :: new( Value :: String ( String :: from( s) ) ) ]
125
- . into_iter( ) . collect:: <PersistentVector >( ) ) ) ] ;
126
- assert_eq ! ( Value :: String ( String :: from( "hello, 5, hello" ) ) , join. invoke( args) ) ;
153
+ let args = vec ! [
154
+ Rc :: new( Value :: String ( String :: from( ", " ) ) ) ,
155
+ Rc :: new( Value :: PersistentVector (
156
+ vec![
157
+ Rc :: new( Value :: String ( String :: from( s) ) ) ,
158
+ Rc :: new( Value :: I32 ( 5 ) ) ,
159
+ Rc :: new( Value :: String ( String :: from( s) ) ) ,
160
+ ]
161
+ . into_iter( )
162
+ . collect:: <PersistentVector >( ) ,
163
+ ) ) ,
164
+ ] ;
165
+ assert_eq ! (
166
+ Value :: String ( String :: from( "hello, 5, hello" ) ) ,
167
+ join. invoke( args)
168
+ ) ;
127
169
}
128
170
}
129
- }
171
+ }
0 commit comments