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

81
LINES

< > BotCompany Repo | #1003239 // map - call function on list (takes function name, Runnable or function object with "get" method) / now also works on maps

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

Libraryless. Click here for Pure Java version (9240L/51K).

1  
ifndef EnforceFunctionTypes
2  
static L map(Iterable l, O f) { ret map(f, l); }
3  
4  
static L map(O f, Iterable l) {
5  
  L x = emptyList(l);
6  
  if (l != null) for ping (O o : l)
7  
    x.add(callF(f, o));
8  
  ret x;
9  
}
10  
11  
// map: func(key, value) -> list element
12  
static L map(Map map, O f) {
13  
  new L x;
14  
  if (map != null) for ping (O _e : map.entrySet()) {
15  
    Map.Entry e = (Map.Entry) _e;
16  
    x.add(callF(f, e.getKey(), e.getValue()));
17  
  }
18  
  ret x;
19  
}
20  
21  
static L map(O f, O[] l) { ret map(f, asList(l)); }
22  
static L map(O[] l, O f) { ret map(f, l); }
23  
24  
static L map(O f, Map map) {
25  
  ret map(map, f);
26  
}
27  
28  
endifndef
29  
30  
ifclass F1
31  
  static <A, B> L<B> map(Iterable<A> l, F1<A, B> f) { ret map(f, l); }
32  
33  
  static <A, B> L<B> map(F1<A, B> f, Iterable<A> l) {
34  
    L x = emptyList(l);
35  
    if (l != null) for ping (A o : l)
36  
      x.add(callF(f, o));
37  
    ret x;
38  
  }
39  
endif
40  
41  
static <A, B> L<B> map(IF1<A, B> f, Iterable<A> l) { ret map(l, f); }
42  
static <A, B> L<B> map(Iterable<A> l, IF1<A, B> f) {
43  
  L x = emptyList(l);
44  
  if (l != null) {
45  
    var it = l.iterator();
46  
    if (it.hasNext()) {
47  
      var pingSource = pingSource();
48  
      do {
49  
        ping(pingSource);
50  
        x.add(f.get(it.next()));
51  
      } while (it.hasNext());
52  
    }
53  
  }
54  
  ret x;
55  
}
56  
  
57  
static <A, B> L<B> lambdaMapLike map(IF1<A, B> f, A[] l) { ret map(l, f); }
58  
static <A, B> L<B> map(A[] l, IF1<A, B> f) {
59  
  L x = emptyList(l);
60  
  if (l != null) for ping (A o : l)
61  
    x.add(f.get(o));
62  
  ret x;
63  
}
64  
  
65  
static <A, B, C> L<C> map(Map<A, B> map, IF2<A, B, C> f) {
66  
  new L x;
67  
  if (map != null) for ping (Map.Entry<A, B> e : map.entrySet()) {
68  
    x.add(f.get(e.getKey(), e.getValue()));
69  
  }
70  
  ret x;
71  
}
72  
73  
// new magic alias for mapLL - does it conflict?
74  
75  
static <A, B> L<A> map(IF1<A, B> f, A data1, A... moreData) {
76  
  L x = emptyList(l(moreData)+1);
77  
  x.add(f.get(data1));
78  
  if (moreData != null) for ping (A o : moreData)
79  
    x.add(f.get(o));
80  
  ret x;
81  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003239
Snippet name: map - call function on list (takes function name, Runnable or function object with "get" method) / now also works on maps
Eternal ID of this version: #1003239/25
Text MD5: bae09cc18c1a9e809d28edcddd2a1243
Transpilation MD5: 8c832f869efe047781c92df2588d44c6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-04 20:17:57
Source code size: 1959 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 1037 / 1360
Version history: 24 change(s)
Referenced in: [show references]