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

81
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 (9270L/51K).

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 byte last(byte[] a) {
  ret l(a) != 0 ? a[l(a)-1] : 0;
}

meta-for int also as long {
  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;
  if (l cast L) ret (A) last(l);
  if (l cast SortedSet) ret (A) last(l);
  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();
}

ifclass ReverseChain
static <A> A last(ReverseChain<A> l) {
  ret l?.element;
}
endif

ifclass IntBuffer
static int last(IntBuffer buf) {
  ret buf.get(buf.size()-1);
}
endif

ifclass ByteBuffer
static byte last(ByteBuffer buf) {
  ret buf.get(buf.size()-1);
}
endif

ifclass DoubleBuffer
static double last(DoubleBuffer l) {
  ret l.last();
}
endif

ifclass LongBuffer
static long last(LongBuffer l) {
  ret l.last();
}
endif

ifclass CompactLinkedHashSet
static <A> A last(CompactLinkedHashSet<A> set) {
  ret set?.last();
}
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/23
Text MD5: 38c07f9218f42ae76a43e83166ef56a7
Transpilation MD5: 3317538eaf10fa4f751435120c683bb3
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-10-03 19:52:17
Source code size: 1462 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 742 / 3534
Version history: 22 change(s)
Referenced in: [show references]