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

64
LINES

< > BotCompany Repo | #1001664 // max function

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

Libraryless. Click here for Pure Java version (129L/1K).

1  
static int max(int a, int b) { ret Math.max(a, b); }
2  
static int max(int a, int b, int c) { ret max(max(a, b), c); }
3  
static long max(int a, long b) { ret Math.max((long) a, b); }
4  
static long max(long a, long b) { ret Math.max(a, b); }
5  
static double max(int a, double b) { ret Math.max((double) a, b); }
6  
static float max(float a, float b) { ret Math.max(a, b); }
7  
static double max(double a, double b) { ret Math.max(a, b); }
8  
9  
static <A extends Comparable<A>> A max (Iterable<A> l) {
10  
  A max = null;
11  
  var it = iterator(l);
12  
  if (it.hasNext()) {
13  
    max = it.next();
14  
    while (it.hasNext()) {
15  
      A a = it.next();
16  
      if (cmp(a, max) > 0)
17  
        max = a;
18  
    }
19  
  }
20  
  ret max;
21  
}
22  
23  
/*Nah.
24  
static int max(Collection<Integer> c) {
25  
  int x = Integer.MIN_VALUE;
26  
  for (int i : c) x = max(x, i);
27  
  ret x;
28  
}*/
29  
30  
static double max(double[] c) {
31  
  if (c.length == 0) ret Double.MIN_VALUE;
32  
  double x = c[0];
33  
  for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
34  
  ret x;
35  
}
36  
37  
static float max(float[] c) {
38  
  if (c.length == 0) ret Float.MAX_VALUE;
39  
  float x = c[0];
40  
  for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
41  
  ret x;
42  
}
43  
44  
static byte max(byte[] c) {
45  
  byte x = -128;
46  
  for (byte d : c) if (d > x) x = d;
47  
  ret x;
48  
}
49  
50  
static short max(short[] c) {
51  
  short x = -0x8000;
52  
  for (short d : c) if (d > x) x = d;
53  
  ret x;
54  
}
55  
56  
static int max(int[] c) {
57  
  int x = Int.MIN_VALUE;
58  
  for (int d : c) if (d > x) x = d;
59  
  ret x;
60  
}
61  
62  
static <A extends Comparable<A>> A max(A a, A b) {
63  
  ret cmp(a, b) >= 0 ? a : b;
64  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, podlckwnjdmb, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, wnsclhtenguj, xrpafgyirdlv

No comments. add comment

Snippet ID: #1001664
Snippet name: max function
Eternal ID of this version: #1001664/9
Text MD5: 9445e7b40083c2918cd8cab8ffee97d2
Transpilation MD5: 93e488039d7a8645f98842b617575140
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-08 22:37:58
Source code size: 1572 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 853 / 5309
Version history: 8 change(s)
Referenced in: [show references]