Created
August 25, 2020 04:32
-
-
Save ialameh/4cb571270451ea46ee5252d077164944 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
void main() { | |
Map<dynamic, dynamic> moneyOut = { | |
'App' : 70, | |
'Flutter' : 400, | |
'Issam Alameh': 260, | |
'Life Arms': 1100, | |
'Success': 30, | |
'Legend': 80, | |
'Smart Work': 850, | |
'this app is powerful': 2800, | |
}; | |
var list = moneyOut.keys.toList(); | |
list.swap(3,2); | |
print(moneyOut); | |
moneyOut = moneyOut.swap(3,2); | |
print(moneyOut); | |
moneyOut = moneyOut.swap(7,-4); | |
print(moneyOut); | |
} | |
extension ListUtils<T> on List<T> { | |
num sumBy(num f(T element)) { | |
num sum = 0; | |
for (var item in this) { | |
sum += f(item); | |
} | |
return sum; | |
} | |
swap(int oldIndex, int newIndex) { | |
if (newIndex >= this.length) newIndex = this.length - 1; | |
if (newIndex < 0) newIndex = 0; | |
var tmp = this[oldIndex]; | |
this[oldIndex] = this[newIndex]; | |
this[newIndex] = tmp; | |
} | |
} | |
extension MapUtils on Map { | |
swap(oldIndex, newIndex) { | |
print('$oldIndex, $newIndex'); | |
var tmpKeys = List.of(this.keys.toList()); | |
var tmpValues = List.of(this.values.toList()); | |
tmpKeys.swap(oldIndex, newIndex); | |
tmpValues.swap(oldIndex, newIndex); | |
return { | |
for (int index = 0; index < tmpKeys.length; index++) | |
tmpKeys[index]: tmpValues[index] | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment