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)

1  
static <A> L<A> takeFirst(L<A> l, int n) {
2  
  ret l(l) <= n ? l : newSubListOrSame(l, 0, n);
3  
}
4  
5  
static <A> L<A> takeFirst(int n, L<A> l) {
6  
  ret takeFirst(l, n);
7  
}
8  
9  
static S takeFirst(int n, S s) { ret substring(s, 0, n); }
10  
static S takeFirst(S s, int n) { ret substring(s, 0, n); }
11  
12  
static <A> L<A> takeFirst(int n, Iterable<A> i) {
13  
  if (i == null) null;
14  
  L l = new L;
15  
  Iterator<A> it = i.iterator();
16  
  repeat n { if (it.hasNext()) l.add(it.next()); else break; }
17  
  ret l;
18  
}

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: 411 / 502
Version history: 11 change(s)
Referenced in: [show references]

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