Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

40
LINES

< > BotCompany Repo | #1000810 - join function + array version

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3747L/22K).

1  
public static <A> S join(S glue, Iterable<A> strings) {
2  
  if (strings == null) ret "";
3  
  if (strings cast Collection) {
4  
    if (strings.size() == 1) ret str(first(strings));
5  
  }
6  
  new StringBuilder buf;
7  
  Iterator<A> i = strings.iterator();
8  
  if (i.hasNext()) {
9  
    buf.append(i.next());
10  
    while (i.hasNext())
11  
      buf.append(glue).append(i.next());
12  
  }
13  
  ret buf.toString();
14  
}
15  
16  
public static S join(S glue, S... strings) {
17  
  ret join(glue, Arrays.asList(strings));
18  
}
19  
20  
public static S join(S glue, O... strings) {
21  
  ret join(glue, Arrays.asList(strings));
22  
}
23  
24  
static <A> S join(Iterable<A> strings) {
25  
  ret join("", strings);
26  
}
27  
28  
static <A> S join(Iterable<A> strings, S glue) {
29  
  ret join(glue, strings);
30  
}
31  
32  
public static S join(String[] strings) {
33  
  ret join("", strings);
34  
}
35  
36  
ifclass Pair
37  
static S join(S glue, Pair p) {
38  
  ret p == null ? "" : str(p.a) + glue + str(p.b);
39  
}
40  
endif

Author comment

Began life as a copy of #2000507

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000810
Snippet name: join function + array version
Eternal ID of this version: #1000810/13
Text MD5: f159eb3b145824cd854661538376206f
Transpilation MD5: 8d6d07032dbedecc83f8f2ab5c1029da
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-15 17:04:19
Source code size: 921 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 740 / 12002
Version history: 12 change(s)
Referenced in: [show references]