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.

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 IDoubleBuffer
static double last(IDoubleBuffer l) {
  ret l.last();
}
endif

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

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

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

ifclass SynchronizedLongBuffer
static long last(SynchronizedLongBuffer 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/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: 1012 / 3859
Version history: 26 change(s)
Referenced in: [show references]