Task

Take a   Natural Number   in a given base and return the sum of its digits:

   110         sums to   1
   123410   sums to   10
   fe16       sums to   29
   f0e16     sums to   29


func digits(num, bas = 10) {
    while num != 0 {
        let (n, digit) = (num / bas, num % bas)
        num = n
        yield digit
    }
}
 
func Iterator.Sum(acc = 0) {
    for x in this {
        acc += x
    }
    return acc
}
 
func sumOfDigits(num, bas = 10) => digits(num, bas).Sum()
 
for e in [ 
    (num: 1, bas: 10),
    (num: 12345, bas: 10),
    (num: 123045, bas:10),
    (num: 0xfe, bas: 16),
    (num: 0xf0e, bas: 16)
    ] {
    print(sumOfDigits(e.num, e.bas))
}