Skip to content

Instantly share code, notes, and snippets.

@Headmast
Last active October 5, 2020 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Headmast/d884e0c4f660d40c68013a845e021bbe to your computer and use it in GitHub Desktop.
Save Headmast/d884e0c4f660d40c68013a845e021bbe to your computer and use it in GitHub Desktop.
// 1
void reverse([String text = '']) {
print('New line: ${text.split(' ').reversed.join(' ')}');
}
void main() {
var a = 'hello world';
reverse();
}
//2
double average([List <double>? darray]) {
double sum = 0;
if ((darray == null) || (darray.length == 0)) {
return double.nan;
}
darray.forEach(
(value) {
sum += value;
}
);
return sum/darray.length;
}
void main() {
List <double> darray = [2, 4, 2, 3];
print ('Average: ${average(darray)}');
}
@artem-zaitsev
Copy link

if ветку можно заменить с помощью null-safety. Предлагаю почитать про работу с null в оф. доке и переписать функцию average в более простом формате.

@artem-zaitsev
Copy link

double sum;
double len = darray?.length ?? 0;
darray?.forEach();
return sum/len;

Можно еще так с использованием аналога элвиса из Котлин( ?? )

@Headmast
Copy link
Author

Headmast commented Oct 5, 2020

Да только на 0 потом делить нельзя)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment