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

31
LINES

< > BotCompany Repo | #1000719 // dropLast function (drop last elements of array/list/string)

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

Libraryless. Click here for Pure Java version (2455L/15K).

1  
static <A> A[] dropLast(A[] a, int n default 1) {
2  
  if (a == null) null;
3  
  n = Math.min(n, a.length);
4  
  A[] b = arrayOfSameType(a, a.length-n);
5  
  System.arraycopy(a, 0, b, 0, b.length);
6  
  ret b;
7  
}
8  
9  
static <A> L<A> dropLast(L<A> l) {
10  
  ret subList(l, 0, l(l)-1);
11  
}
12  
13  
static <A> L<A> dropLast(int n, L<A> l) {
14  
  ret subList(l, 0, l(l)-n);
15  
}
16  
17  
static <A> L<A> dropLast(Iterable<A> l) {
18  
  ret dropLast(asList(l));
19  
}
20  
21  
static S dropLast(S s) {
22  
  ret substring(s, 0, l(s)-1);
23  
}
24  
25  
static S dropLast(S s, int n) {
26  
  ret substring(s, 0, l(s)-n);
27  
}
28  
29  
static S dropLast(int n, S s) {
30  
  ret dropLast(s, n);
31  
}

Author comment

Began life as a copy of #1000705

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000719
Snippet name: dropLast function (drop last elements of array/list/string)
Eternal ID of this version: #1000719/9
Text MD5: 7f4ea095e27006e7f7958a71d05215f7
Transpilation MD5: cb25bbeb95b8b3dc94f5e451c6906f56
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-11 18:49:57
Source code size: 623 bytes / 31 lines
Pitched / IR pitched: No / No
Views / Downloads: 672 / 1312
Version history: 8 change(s)
Referenced in: [show references]