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

ifndef EnforceFunctionTypes
static L map(Iterable l, O f) { ret map(f, l); }

static L map(O f, Iterable l) {
  L x = emptyList(l);
  if (l != null) for ping (O o : l)
    x.add(callF(f, o));
  ret x;
}

// map: func(key, value) -> list element
static L map(Map map, O f) {
  new L x;
  if (map != null) for ping (O _e : map.entrySet()) {
    Map.Entry e = (Map.Entry) _e;
    x.add(callF(f, e.getKey(), e.getValue()));
  }
  ret x;
}

static L map(O f, O[] l) { ret map(f, asList(l)); }
static L map(O[] l, O f) { ret map(f, l); }

static L map(O f, Map map) {
  ret map(map, f);
}

endifndef

ifclass F1
  static <A, B> L<B> map(Iterable<A> l, F1<A, B> f) { ret map(f, l); }

  static <A, B> L<B> map(F1<A, B> f, Iterable<A> l) {
    L x = emptyList(l);
    if (l != null) for ping (A o : l)
      x.add(callF(f, o));
    ret x;
  }
endif

static <A, B> L<B> map(IF1<A, B> f, Iterable<A> l) { ret map(l, f); }
static <A, B> L<B> map(Iterable<A> l, IF1<A, B> f) {
  L x = emptyList(l);
  if (l != null) {
    var it = l.iterator();
    if (it.hasNext()) {
      var pingSource = pingSource();
      do {
        ping(pingSource);
        x.add(f.get(it.next()));
      } while (it.hasNext());
    }
  }
  ret x;
}
  
static <A, B> L<B> lambdaMapLike map(IF1<A, B> f, A[] l) { ret map(l, f); }
static <A, B> L<B> map(A[] l, IF1<A, B> f) {
  L x = emptyList(l);
  if (l != null) for ping (A o : l)
    x.add(f.get(o));
  ret x;
}
  
static <A, B, C> L<C> map(Map<A, B> map, IF2<A, B, C> f) {
  new L x;
  if (map != null) for ping (Map.Entry<A, B> e : map.entrySet()) {
    x.add(f.get(e.getKey(), e.getValue()));
  }
  ret x;
}

// new magic alias for mapLL - does it conflict?

static <A, B> L<A> map(IF1<A, B> f, A data1, A... moreData) {
  L x = emptyList(l(moreData)+1);
  x.add(f.get(data1));
  if (moreData != null) for ping (A o : moreData)
    x.add(f.get(o));
  ret x;
}

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: 1157 / 1512
Version history: 24 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1009337 - map_precise - like map(), but use precise calling
#1009797 - mapWrap - wrap all objects in list in a class of your choice
#1010000 - map_pcall - map() with safe calling (skip elements when failed)
#1013130 - mapReversed - map & reverse list
#1015378 - mapAndRecordToMap
#1018013 - mapKeyAndFunction_lhm - iterate on list, keep object as key, function result as value, use LinkedHashMap
#1019625 - mapNonNulls - map, then nonNulls
#1021167 - mapLines - map(lines(...))
#1021324 - mapNuObject - map(), but calls a class's constructor instead of a function
#1023745 - map_curry - map, curry the function with 1 argument
#1025136 - oMap - call object method on list
#1025376 - mapOverClone - cloneList + map
#1026014 - lambdaMap - map function with lambdaMapLike
#1027913 - map_ping - map + ping [dev.]
#1028320 - pnlMap - pnl+map
#1030039 - safeMap - safe version of map()
#1032630 - mapToMultiSet
#1033641 - mapMatrix