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

24
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 (2700L/17K).

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, Iterable<A> i) {
  if (i == null) null;
  L l = new L;
  Iterator<A> it = i.iterator();
  repeat n { if (it.hasNext()) l.add(it.next()); else break; }
  ret l;
}

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

Author comment

Began life as a copy of #1003099

download  show line numbers  debug dex   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, 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/14
Text MD5: f0aa78297dcf97f89b66a342b982d44e
Transpilation MD5: 866db49ff7015581709694a9f1202739
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-09 22:14:42
Source code size: 665 bytes / 24 lines
Pitched / IR pitched: No / No
Views / Downloads: 463 / 590
Version history: 13 change(s)
Referenced in: [show references]

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