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

42
LINES

< > BotCompany Repo | #1007549 // takeFirst function (take first n elements of a list; or characters of a string)

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

Libraryless. Click here for Pure Java version (665L/4K).

static <A> L<A> takeFirst(L<A> l, int n) {
  ret l(l) <= n ? l : newSubListOrSame(l, 0, n);
}

static <A> L<A> takeFirst(int n, L<A> l) {
  ret takeFirst(l, n);
}

static S takeFirst(int n, S s) { ret substring(s, 0, n); }
static S takeFirst(S s, int n) { ret substring(s, 0, n); }

static CharSequence takeFirst(int n, CharSequence s) { ret subCharSequence(s, 0, n); }

static <A> L<A> takeFirst(int n, Iterator<A> it) {
  if (it == null) null;
  new L l;
  repeat n { if (it.hasNext()) l.add(it.next()); else break; }
  ret l;
}

static <A> L<A> takeFirst(int n, Iterable<A> i) {
  if (i == null) null;
  ret i == null ?: takeFirst(n, i.iterator());
}

static <A> L<A> takeFirst(int n, ItIt<A> i) {
  ret takeFirst(n, (Iterator<A>) i);
}

static int[] takeFirst(int n, int[] a) { ret takeFirstOfIntArray(n, a); }

static short[] takeFirst(int n, short[] a) { ret takeFirstOfShortArray(n, a); }

static byte[] takeFirst(int n, byte[] a) { ret takeFirstOfByteArray(n, a); }
static byte[] takeFirst(byte[] a, int n) { ret takeFirstOfByteArray(n, a); }

static double[] takeFirst(int n, double[] a) { ret takeFirstOfDoubleArray(n, a); }
static double[] takeFirst(double[] a, int n) { ret takeFirstOfDoubleArray(n, a); }

static <A, B> Map<A, B> takeFirst(int n, Map<A, B> map) {
  ret takeFirstFromMap(n, map);
}

Author comment

Began life as a copy of #1003099

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1007549
Snippet name: takeFirst function (take first n elements of a list; or characters of a string)
Eternal ID of this version: #1007549/22
Text MD5: 2fd86ca69d1deadcb2dacac50815d986
Transpilation MD5: 47a492df18c6ca6be97c63425600e598
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-04-25 23:02:18
Source code size: 1351 bytes / 42 lines
Pitched / IR pitched: No / No
Views / Downloads: 887 / 1116
Version history: 21 change(s)
Referenced in: [show references]