Last active
November 29, 2020 02:05
-
-
Save LouiS0616/489c755530941a1d1e5df21c2f482306 to your computer and use it in GitHub Desktop.
Dartで嵌ったけど、原因が分かったやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<List<U>> deepMap2D_F<T, U>(List<List<T>> src, U Function(T) f) { | |
var ret = <List<U>>[]; | |
for(List<T> innerList in src) { | |
ret.add([for(var e in innerList) f(e)]); | |
} | |
return ret; | |
} | |
extension <T,U> on List<List<T>> { | |
List<List<U>> deepMap2D_M(U Function(T) f) { | |
var ret = <List<U>>[]; | |
for(List<T> innerList in this) { | |
ret.add([for(var e in innerList) f(e)]); | |
} | |
return ret; | |
} | |
} | |
extension <T> on List<List<T>> { | |
List<List<U>> deepMap2D_M2<U>(U Function(T) f) { | |
var ret = <List<U>>[]; | |
for(List<T> innerList in this) { | |
ret.add([for(var e in innerList) f(e)]); | |
} | |
return ret; | |
} | |
} | |
void main() { | |
List<List<dynamic>> src = [['foo', 'bar'], ['baz', 'foobar']]; | |
var dst1 = deepMap2D_F(src, (e) => e as String); | |
print(dst1.runtimeType); // List<List<String>> | |
var dst2 = src.deepMap2D_M((e) => e as String); | |
print(dst2.runtimeType); // List<List<dynamic>> | |
var dst3 = src.deepMap2D_M2((e) => e as String); | |
print(dst3.runtimeType); // List<List<String>> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment