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

37
LINES

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

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

Libraryless. Click here for Pure Java version (2174L/14K).

static <A> A last(L<A> l) {
  return empty(l) ? null : l.get(l.size()-1);
}

static char last(S s) {
  ret empty(s) ? '#' : s.charAt(l(s)-1);
}

static int last(int[] a) {
  ret l(a) != 0 ? a[l(a)-1] : 0;
}

static double last(double[] a) {
  ret l(a) != 0 ? a[l(a)-1] : 0;
}

static <A> A last(A[] a) {
  ret l(a) != 0 ? a[l(a)-1] : null;
}

static <A> A last(Iterator<A> it) {
  A a = null;
  while ping (it.hasNext()) a = it.next();
  ret a;
}

static <A> A last(Cl<A> l) {
  if (l == null) null;
  Iterator<A> it = iterator(l);
  A a = null;
  while ping (it.hasNext()) a = it.next();
  ret a;
}

static <A> A last(SortedSet<A> l) {
  ret l == null ? null : l.last();
}

download  show line numbers  debug dex   

Travelled to 12 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, tslmcundralx, tvejysmllsmz, 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/12
Text MD5: d50ac42d211db7e97e4201080feed849
Transpilation MD5: d980334d72db29f570d2075141f58028
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-02 19:06:59
Source code size: 709 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 466 / 3049
Version history: 11 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1001038 & http://1001038.tinybrain.de