Aktywne Wpisy

michalxd21 +165

Teuvo +16
z nosa leci woda przeciwhistaminowe leki na alergię nic nie dają jak ktoś zna sposób jak to zatrzymać to słucham
Skopiuj link
Skopiuj link

Regulamin
Reklama
Kontakt
O nas
FAQ
Osiągnięcia
Ranking
func sumOfPositives (_ numbers: [Int] ) -> Int {let positiveNum = numbers.flatMap{$0 > 0 ? $0 : 0}
var sum = 0
positiveNum.flatMap{sum += $0}
return sum
}
Wykonując ten kod dostaje dobre wyniki + error:
solution.swift:4:13: warning: result of call to 'flatMap' is unusedpositiveNum.flatMap{sum += $0}
I teraz proszę powiedzcie mi jak mam traktować takie coś - czy to duży błąd?
https://www.hackingwithswift.com/example-code/language/how-to-sum-an-array-of-numbers-using-reduce
let positiveNum = numbers.flatMap{$0 > 0 ? $0 : 0}
lepsze bedzie
let positiveNum = numbers.filter{$0 > 0 }
let x = [1,-1,2,-2,3,-3]let result = x.reduce(0) { $0 + max($1, 0) }
flatMapużywa się w określonym celu, i ponieważ twórcy języka nie dodali atrybutu @discardableResult to Ty używasz go źle. Pytania o sztywnego trzymania się braku błędów nie rozumiem - myślałem, że to jest fundament programowania, żeby nie było błędów.let x = [1,-1,2,-2,3,-3]let pos = x.filter { $0 > 0 }
let result = pos.reduce(0, +)