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

105
LINES

< > BotCompany Repo | #1001038 // last function (get last element of list or last character in string)

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

Transpiled version (9270L) is out of date.

1  
static <A> A last(L<A> l) {
2  
  return empty(l) ? null : l.get(l.size()-1);
3  
}
4  
5  
static char last(S s) {
6  
  ret empty(s) ? '#' : s.charAt(l(s)-1);
7  
}
8  
9  
static byte last(byte[] a) {
10  
  ret l(a) != 0 ? a[l(a)-1] : 0;
11  
}
12  
13  
meta-for int also as long {
14  
  static int last(int[] a) {
15  
    ret l(a) != 0 ? a[l(a)-1] : 0;
16  
  }
17  
}
18  
19  
static double last(double[] a) {
20  
  ret l(a) != 0 ? a[l(a)-1] : 0;
21  
}
22  
23  
static <A> A last(A[] a) {
24  
  ret l(a) != 0 ? a[l(a)-1] : null;
25  
}
26  
27  
static <A> A last(Iterator<A> it) {
28  
  A a = null;
29  
  while ping (it.hasNext()) a = it.next();
30  
  ret a;
31  
}
32  
33  
static <A> A last(Cl<A> l) {
34  
  if (l == null) null;
35  
  if (l cast L) ret (A) last(l);
36  
  if (l cast SortedSet) ret (A) last(l);
37  
  Iterator<A> it = iterator(l);
38  
  A a = null;
39  
  while ping (it.hasNext()) a = it.next();
40  
  ret a;
41  
}
42  
43  
static <A> A last(SortedSet<A> l) {
44  
  ret l == null ? null : l.last();
45  
}
46  
47  
ifclass ReverseChain
48  
static <A> A last(ReverseChain<A> l) {
49  
  ret l?.element;
50  
}
51  
endif
52  
53  
ifclass IntBuffer
54  
static int last(IntBuffer buf) {
55  
  ret buf.get(buf.size()-1);
56  
}
57  
endif
58  
59  
ifclass ByteBuffer
60  
static byte last(ByteBuffer buf) {
61  
  ret buf.get(buf.size()-1);
62  
}
63  
endif
64  
65  
ifclass DoubleBuffer
66  
static double last(DoubleBuffer l) {
67  
  ret l.last();
68  
}
69  
endif
70  
71  
ifclass IDoubleBuffer
72  
static double last(IDoubleBuffer l) {
73  
  ret l.last();
74  
}
75  
endif
76  
77  
ifclass ILongBuffer
78  
static long last(ILongBuffer l) {
79  
  ret l.last();
80  
}
81  
endif
82  
83  
ifclass LongBuffer
84  
static long last(LongBuffer l) {
85  
  ret l.last();
86  
}
87  
endif
88  
89  
ifclass SynchronizedDoubleBuffer
90  
static double last(SynchronizedDoubleBuffer l) {
91  
  ret l.last();
92  
}
93  
endif
94  
95  
ifclass SynchronizedLongBuffer
96  
static long last(SynchronizedLongBuffer l) {
97  
  ret l.last();
98  
}
99  
endif
100  
101  
ifclass CompactLinkedHashSet
102  
static <A> A last(CompactLinkedHashSet<A> set) {
103  
  ret set?.last();
104  
}
105  
endif

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001038
Snippet name: last function (get last element of list or last character in string)
Eternal ID of this version: #1001038/27
Text MD5: 0c5be7ff8e935c0e55bc05abc98436dc
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-08 14:04:28
Source code size: 1858 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 896 / 3726
Version history: 26 change(s)
Referenced in: [show references]