1
+ use crate :: iterable:: Iterable ;
1
2
use crate :: ifn:: IFn ;
2
3
use crate :: value:: { ToValue , Value } ;
3
4
use std:: rc:: Rc ;
4
5
5
6
use crate :: error_message;
6
- use crate :: persistent_list:: PersistentList :: Cons ;
7
- use crate :: persistent_list:: ToPersistentListIter ;
8
- use crate :: persistent_vector:: ToPersistentVectorIter ;
9
- use crate :: type_tag:: TypeTag ;
10
- use itertools:: Itertools ;
7
+ use crate :: protocol:: ProtocolCastable ;
11
8
12
9
/// clojure.string/join ; joins a coll of items together as a string
13
10
/// (join
@@ -22,55 +19,34 @@ impl ToValue for JoinFn {
22
19
}
23
20
impl IFn for JoinFn {
24
21
fn invoke ( & self , args : Vec < Rc < Value > > ) -> Value {
25
- if args. len ( ) == 1 || args. len ( ) == 2 {
26
- let separator = if args. len ( ) == 2 {
27
- args. get ( 0 ) . unwrap ( ) . to_string ( )
28
- } else {
29
- String :: from ( "" )
30
- } ;
31
- let coll = if args. len ( ) == 1 {
32
- args. get ( 0 )
33
- } else {
34
- args. get ( 1 )
35
- } ;
36
- match coll. unwrap ( ) . to_value ( ) {
37
- Value :: PersistentList ( Cons ( head, tail, count) ) => {
38
- return if count == 0 {
39
- Value :: String ( String :: from ( "" ) )
40
- } else if count == 1 {
41
- Value :: String ( head. to_string ( ) )
42
- } else {
43
- Value :: String (
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
- )
53
- }
54
- }
55
- Value :: PersistentVector ( pvec) => {
56
- return if pvec. vals . len ( ) == 0 {
57
- Value :: String ( String :: from ( "" ) )
58
- } else {
59
- Value :: String ( String :: from (
60
- pvec. vals
61
- . iter ( )
62
- . map ( |x| x. to_string ( ) )
63
- . collect :: < Vec < std:: string:: String > > ( )
64
- . join ( & separator)
65
- . as_str ( ) ,
66
- ) )
67
- }
68
- }
69
- _ => Value :: String ( String :: from ( "" ) ) ,
70
- }
71
- } else {
22
+ if args. len ( ) != 1 && args. len ( ) != 2 {
72
23
return error_message:: wrong_varg_count ( & [ 1 , 2 ] , args. len ( ) ) ;
73
24
}
25
+
26
+ let separator = if args. len ( ) == 1 {
27
+ String :: from ( "" )
28
+ } else {
29
+ args. get ( 0 ) . unwrap ( ) . to_string ( )
30
+ } ;
31
+
32
+ let coll = if args. len ( ) == 1 {
33
+ args. get ( 0 )
34
+ } else {
35
+ args. get ( 1 )
36
+ } ;
37
+ if let Some ( iterable) = coll. unwrap ( ) . try_as_protocol :: < Iterable > ( ) {
38
+ Value :: String (
39
+ iterable
40
+ . iter ( )
41
+ . map ( |x| x. to_string ( ) )
42
+ . collect :: < Vec < std:: string:: String > > ( )
43
+ . join ( & separator)
44
+ )
45
+ }
46
+ else {
47
+ Value :: String ( String :: from ( "" ) )
48
+ }
49
+
74
50
}
75
51
}
76
52
0 commit comments