@@ -87,15 +87,11 @@ public function split(?string $delimiter = null): Sequence
8787 return $ this ->chunk ();
8888 }
8989
90- $ parts = \explode ($ delimiter , $ this ->value );
91- /** @var Sequence<self> */
92- $ sequence = Sequence ::of ();
93-
94- foreach ($ parts as $ part ) {
95- $ sequence = ($ sequence )(new self ($ part , $ this ->encoding ));
96- }
97-
98- return $ sequence ;
90+ /** @psalm-suppress ImpureFunctionCall */
91+ return Sequence ::of (...\array_map (
92+ fn ($ value ) => new self ($ value , $ this ->encoding ),
93+ \explode ($ delimiter , $ this ->value ),
94+ ));
9995 }
10096
10197 /**
@@ -108,15 +104,11 @@ public function split(?string $delimiter = null): Sequence
108104 #[\NoDiscard]
109105 public function chunk (int $ size = 1 ): Sequence
110106 {
111- /** @var Sequence<self> */
112- $ sequence = Sequence ::of ();
113- $ parts = \mb_str_split ($ this ->value , $ size , $ this ->encoding ->toString ());
114-
115- foreach ($ parts as $ value ) {
116- $ sequence = ($ sequence )(new self ($ value , $ this ->encoding ));
117- }
118-
119- return $ sequence ;
107+ /** @psalm-suppress ImpureFunctionCall */
108+ return Sequence ::of (...\array_map (
109+ fn ($ value ) => new self ($ value , $ this ->encoding ),
110+ \mb_str_split ($ this ->value , $ size , $ this ->encoding ->toString ()),
111+ ));
120112 }
121113
122114 /**
@@ -287,14 +279,14 @@ public function words(string|\Stringable $charlist = ''): Map
287279 {
288280 /** @var list<string> */
289281 $ words = \str_word_count ($ this ->value , 2 , (string ) $ charlist );
290- /** @var Map< int, self> */
291- $ map = Map :: of () ;
282+ /** @var list<array{ int, self} > */
283+ $ pairs = [] ;
292284
293285 foreach ($ words as $ position => $ word ) {
294- $ map = ( $ map )( $ position , new self ($ word , $ this ->encoding )) ;
286+ $ pairs [] = [ $ position , new self ($ word , $ this ->encoding )] ;
295287 }
296288
297- return $ map ;
289+ return Map :: of (... $ pairs ) ;
298290 }
299291
300292 /**
@@ -315,14 +307,11 @@ public function pregSplit(string|\Stringable $regex, int $limit = -1): Sequence
315307 throw new InvalidRegex ('' , \preg_last_error ());
316308 }
317309
318- /** @var Sequence<self> */
319- $ sequence = Sequence ::of ();
320-
321- foreach ($ strings as $ string ) {
322- $ sequence = ($ sequence )(new self ($ string , $ this ->encoding ));
323- }
324-
325- return $ sequence ;
310+ /** @psalm-suppress ImpureFunctionCall */
311+ return Sequence ::of (...\array_map (
312+ fn ($ value ) => new self ($ value , $ this ->encoding ),
313+ $ strings ,
314+ ));
326315 }
327316
328317 /**
0 commit comments