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).

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 CharSequence takeFirst(int n, CharSequence s) { ret subCharSequence(s, 0, n); }
13  
14  
static <A> L<A> takeFirst(int n, Iterator<A> it) {
15  
  if (it == null) null;
16  
  new L l;
17  
  repeat n { if (it.hasNext()) l.add(it.next()); else break; }
18  
  ret l;
19  
}
20  
21  
static <A> L<A> takeFirst(int n, Iterable<A> i) {
22  
  if (i == null) null;
23  
  ret i == null ?: takeFirst(n, i.iterator());
24  
}
25  
26  
static <A> L<A> takeFirst(int n, ItIt<A> i) {
27  
  ret takeFirst(n, (Iterator<A>) i);
28  
}
29  
30  
static int[] takeFirst(int n, int[] a) { ret takeFirstOfIntArray(n, a); }
31  
32  
static short[] takeFirst(int n, short[] a) { ret takeFirstOfShortArray(n, a); }
33  
34  
static byte[] takeFirst(int n, byte[] a) { ret takeFirstOfByteArray(n, a); }
35  
static byte[] takeFirst(byte[] a, int n) { ret takeFirstOfByteArray(n, a); }
36  
37  
static double[] takeFirst(int n, double[] a) { ret takeFirstOfDoubleArray(n, a); }
38  
static double[] takeFirst(double[] a, int n) { ret takeFirstOfDoubleArray(n, a); }
39  
40  
static <A, B> Map<A, B> takeFirst(int n, Map<A, B> map) {
41  
  ret takeFirstFromMap(n, map);
42  
}

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: 741 / 981
Version history: 21 change(s)
Referenced in: [show references]