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

18
LINES

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

JavaX fragment (include)

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 <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;
}

Author comment

Began life as a copy of #1003099

download  show line numbers  debug dex   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, 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/12
Text MD5: e978f16cea917a91b2a597a96bdb1a3e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-23 17:37:09
Source code size: 496 bytes / 18 lines
Pitched / IR pitched: No / No
Views / Downloads: 410 / 501
Version history: 11 change(s)
Referenced in: #1006654 #1014754 #1021957 #1022354 #1025353 #1025552 #1025950

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