LoginSignup
2
1

More than 5 years have passed since last update.

DartでmapIndexedなど

Posted at

DartでmapIndexedなど作ってみた。
generatorは便利だね。TypeScriptにもあればいいのに

map_indexed.dart
Iterable<R> mapIndexed<T,R>(Iterable<T> list, R f(int index, T val)) sync*{
  int i = 0;
  for(T val in list){
    yield f(i++, val);
  }
}
map_notnull.dart
Iterable<R> mapNotNull<T,R>(Iterable<T> list, R f(T val)) sync*{
  for(T val in list){
    R result = f(val);
    if(result != null){
      yield result;
    }
  }
}
range.dart
Iterable<int> range(int size) sync* {
  for(int i = 0; i < size; i++){
    yield i;
  }
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1