In Python, you can have a list (similar to an array in swift):
>>> li=[0,1,2,3,4,5]
And do the slice assignment for any / all list:
>>> li[2:]=[99]
>>> li
[0, 1, 99]
Swift has a similar slice assignment (this is in the interactive shell swift):
1> var arr=[0,1,2,3,4,5]
arr: [Int] = 6 values {
[0] = 0
[1] = 1
[2] = 2
[3] = 3
[4] = 4
[5] = 5
}
2> arr[2...arr.endIndex-1]=[99]
3> arr
$R0: [Int] = 3 values {
[0] = 0
[1] = 1
[2] = 99
}
So far so good. But there are a few problems.
Firstly, swift does not work for an empty list or index after endIndex. Python adds if the slice index after the end index:
>>> li=[]
>>> li[2:]=[6,7,8]
>>> li
[6, 7, 8]
>>> li=[0,1,2]
>>> li[999:]=[999]
>>> li
[0, 1, 2, 999]
The equivalent in swift is a mistake:
4> var arr=[Int]()
arr: [Int] = 0 values
5> arr[2...arr.endIndex-1]=[99]
fatal error: Can't form Range with end < start
It is easy to check and copy.
The second problem is the killer: it is very slow. Consider this Python code to do the exact summation of a list of floats:
def msum(iterable):
"Full precision summation using multiple floats for intermediate values"
partials = [] # sorted, non-overlapping partial sums
for x in iterable:
i = 0
for y in partials:
if abs(x) < abs(y):
x, y = y, x
hi = x + y
lo = y - (hi - x)
if lo:
partials[i] = lo
i += 1
x = hi
partials[i:] = [x]
return sum(partials, 0.0)
, hi/lo, msum([.1]*10) 1.0 , 0.9999999999999999. C- msum Python.
:
func msum(it:[Double])->Double {
var partials=[Double]()
for var x in it {
var i=0
for var y in partials{
if abs(x) < abs(y){
(x, y)=(y, x)
}
let hi=x+y
let lo=y-(hi-x)
if abs(lo)>0.0 {
partials[i]=lo
i+=1
}
x=hi
}
if partials.endIndex>i {
partials[i...partials.endIndex-1]=[x]
}
else {
partials.append(x)
}
}
return partials.reduce(0.0, combine: +)
}
:
import Foundation
var arr=[Double]()
for _ in 1...1000000 {
arr+=[10, 1e100, 10, -1e100]
}
print(arr.reduce(0, combine: +)) // will be 0.0
var startTime: CFAbsoluteTime!
startTime = CFAbsoluteTimeGetCurrent()
print(msum(arr), arr.count*5) // should be arr.count * 5
print(CFAbsoluteTimeGetCurrent() - startTime)
7 . Python native msum 2,2 ( 4 ), fsum 0,09 ( 90 )
partials[i...partials.endIndex-1]=[x] arr.removeRange(i..<arr.endIndex), . , .
:
- :
partials[i...partials.endIndex-1]=[x] - / ?