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

89
LINES

< > BotCompany Repo | #1000628 - first function

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

Libraryless. Click here for Pure Java version (4526L/25K).

ifndef NoGenericFunctions
static O first(O list) {
  ret first((Iterable) list);
}
endifndef

static <A> A first(L<A> list) {
  return empty(list) ? null : list.get(0);
}

static <A> A first(A[] bla) {
  ret bla == null || bla.length == 0 ? null : bla[0];
}

static <A, B> Pair<A, B> first(Map<A, B> map) {
  ret mapEntryToPair(first(entrySet(map)));
}

static <A, B> Pair<A, B> first(MultiMap<A, B> mm) {
  if (mm == null) null;
  var e = first(mm.data.entrySet());
  if (e == null) null;
  ret pair(e.getKey(), first(e.getValue());
}

ifclass IterableIterator
static <A> A first(IterableIterator<A> i) {
  ret first((Iterator<A>) i);
}
endif

static <A> A first(Iterator<A> i) {
  ret i == null || !i.hasNext() ? null : i.next();
}

static <A> A first(Iterable<A> i) {
  if (i == null) ret null;
  Iterator<A> it = i.iterator();
  ret it.hasNext() ? it.next() : null;
}

static Char first(S s) { ret empty(s) ? null : s.charAt(0); }
static Char first(CharSequence s) { ret empty(s) ? null : s.charAt(0); }

ifclass Pair
static <A, B> A first(Pair<A, B> p) {
  ret p == null ? null : p.a;
}
endif

ifclass T3
static <A, B, C> A first(T3<A, B, C> t) {
  ret t == null ? null : t.a;
}
endif

static Byte first(byte[] l) { 
  ret empty(l) ? null : l[0];
}

ifclass IntBuffer
static int first(IntBuffer buf) {
  ret buf.get(0);
}
endif

ifclass ByteBuffer
static byte first(ByteBuffer buf) {
  ret buf.get(0);
}
endif

static <A> A first(A[] l, IF1<A, Bool> pred) {
  ret firstThat(l, pred);
}

static <A> A first(Iterable<A> l, IF1<A, Bool> pred) {
  ret firstThat(l, pred);
}

static <A> A lambdaMapLike first(IF1<A, Bool> pred, Iterable<A> l) {
  ret firstThat(pred, l);
}

ifclass AppendableChain
static <A> A first(AppendableChain<A> a) {
  ret a == null ? null : a.element;
}
endif

download  show line numbers  debug dex  old transpilations   

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

2 comment(s) hidden. show

Snippet ID: #1000628
Snippet name: first function
Eternal ID of this version: #1000628/21
Text MD5: 5cc497233724dc4ab2d7fe3e2efff430
Transpilation MD5: d4d282beb541df1e639c069e52b3e048
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-23 02:33:11
Source code size: 1874 bytes / 89 lines
Pitched / IR pitched: No / No
Views / Downloads: 1020 / 2667
Version history: 20 change(s)
Referenced in: [show references]