Make variable immutable after initial assignment - swift

Is there a way to make a variable immutable after initializing/assigning it, so that it can change at one point, but later become immutable? I know that I could create a new let variable, but is there a way to do so without creating a new variable?
If not, what is best practice to safely ensure a variable isn't changed after it needs to be?
Example of what I'm trying to accomplish:
var x = 0 //VARIABLE DECLARATION
while x < 100 { //VARIABLE IS CHANGED AT SOME POINT
x += 1
}
x = let x //MAKE VARIABLE IMMUTABLE AFTER SOME FUNCTION IS PERFORMED
x += 5 //what I'm going for: ERROR - CANNOT ASSIGN TO IMMUTABLE VARIABLE

You can initialize a variable with an inline closure:
let x: Int = {
var x = 0
while x < 100 {
x += 1
}
return x
}()

There's no way I know of that lets you change a var variable into a let constant later on. But you could declare your constant as let to begin with and not immediately give it a value.
let x: Int /// No initial value
x = 100 /// This works.
x += 5 /// Mutating operator '+=' may not be used on immutable value 'x'
As long as you assign a value to your constant sometime before you use it, you're fine, since the compiler can figure out that it will eventually be populated. For example if else works, since one of the conditional branches is guaranteed to get called.
let x: Int
if 5 < 10 {
x = 0 /// This also works.
} else {
x = 1 /// Either the first block or the `else` will be called.
}
x += 5 /// Mutating operator '+=' may not be used on immutable value 'x'

Related

How can I search through this array for a specific value then return the other value in the tuple?

I have a large array of (x,y) pairs:
P =
[
(0.0, 500000.09999999998),
(0.001, 18.332777589999999),
(0.002, 18.332221480000001),
(0.0030000000000000001, 18.331665000000001),
(0.0040000000000000001, 18.331108140000001),
(0.0050000000000000001, 18.33055092),
(0.0060000000000000001, 18.32999332),
...
]
I now need to use this in my code. I need to search for a specific x-value and, if the x-value exists, return its corresponding y-value.
Note: If there is a better format I could put my (x,y) pairs in, please feel free to let me know. For example, 2 separate arrays where one holds the x-values and the other holds the y-values. Then I could use the index to find the corresponding y-value or something.
Edit:
A user made a very good point in the comments: how can I reliably compare x == 0.001?
The way I will be using my code is this: I am evaluating a function f(x) at values of x. However, if at a particular value of x there is a y value in the P array, then I need to do an extra subtraction calculation (the details of which are not too important here). The problem, then, is that what if I pass the x value 0.001 in there and the P array does not have a correpsonding y value, but it does have one for 0.001000000009?? Then the code will say there is no value, but in reality it is reasonably close to the intended x value.
I'd suggest to let your array to be an array of CGPoints. It's simply:
A structure that contains a point in a two-dimensional coordinate
system.
However, if you want to get the y values based on searching the x:
let myArray = [
(0.0, 500000.09999999998),
(0.001, 18.332777589999999),
(0.002, 18.332221480000001),
(0.0030000000000000001, 18.331665000000001),
(0.0040000000000000001, 18.331108140000001),
(0.0050000000000000001, 18.33055092),
(0.0060000000000000001, 18.32999332),
]
// this array should contains y values for a given x value
// for example, I search the x value of 0.0
let yValues = myArray.filter { $0.0 == 0.0 }.map { $0.1 }
print(yValues) // [500000.09999999998]
Hope this helped.
A good way of doing this is by declaring this function:
func getValueFromTuples(tupleArr:[(Double,Double)],n:Double)->Double?{
for tuple in tupleArr{
if tuple.0 == n{
return tuple.1
}
}
return nil
}
Then, you can use it like this:
var tupleArray: [(Double,Double)] = [(1.0, 12.0),(2.0,23.0),(3.0,34.0),(4.0,45.0),(5.0,56.0)]
var x:Double = 1.0
print(getValueFromTuples(tupleArr: tupleArray,n:x) ?? "No value found") // 12.0
Where the n argument is the value to be found, the tuple is the key-value pair formed by the numbers and getValueFromTuples returns the value y if x has been found, else nil.
This returns "No value found" if the value does not exist in the array of tuples.
Hope this helps!
Your x value all seem to increase by 0.001. If that is the case, you could also calculate the index and return the y value at this index. This would be a lot more efficient.
func calculateIndex(forX x: Double) -> Int {
let increase = 0.001
return Int(x/0.001)
}
You can use the find method to find the index of the x value and then return the y value. I would multiply your values by 1000 and then compare the Int instead of comparing Double.
func findYValue(forX x: Double) -> Double? {
let multiply = 1000
let x = Int(multiply*x)
if let index = array.index(where: { Int($0.0 * multiply) == x }) {
return array[index].1 //found the y value
}
return nil //x is not in the array
}
Instead of using tuples, I would personally use CGPoint. The class has an x and a y property, which makes your code more readable.
Microsoft gives a very thorough explanation of how to compare 2 doubles. The basic premise is that you need to define a certain level of tolerance. The article the explores how to pick a good tolerance in most cases.
Here's code translated to Swift:
func areEqual(_ lhs: Double, _ rhs: Double, units: Int = 3) -> Bool {
let lValue = Int64(bitPattern: lhs.bitPattern)
let rValue = Int64(bitPattern: rhs.bitPattern)
let delta = lValue - rValue
return abs(delta) <= Int64(units)
}
Test:
var n = 0.0
for _ in 0..<10 {
n += 0.1
}
// n should equal 1 but it does not
print(n == 1.0) // false
print(areEqual(1.0, n)) // true
Back to your problem, it becomes straight forward after you defined how to test for equality in 2 doubless:
let x = 0.003
if let y = p.first(where: { areEqual($0.0, x) })?.1 {
print(y)
}

Swift Variable Declaration and Initialize

Is there a difference between how following bits of code work?
let x: Int = 4
and
let x: Int
x = 4
This one:
let x: Int = 4
creates a non-optional variable x and initialises it to 4. x can be used without issue.
This one:
let x: Int
// Cannot do anything with x yet
x = 4
creates a non-optional variable x with no defined value. It cannot be used without first assigning it to a value, either directly (as in your example) or by the result of some other statement. If you do try and use it, you'll get a compile-time error.
The only difference is that on the first one you are declaring a variable and assigning it at the same time, and the second one you declare it first and then assign it.
But there is no mayor difference.

what does ++ exactly mean in Swift?

i am learning Swift with a book aimed for people with little experience. One thing bothering me is the ++ syntax. The following is taken from the book:
var counter = 0
let incrementCounter = {
counter++
}
incrementCounter()
incrementCounter()
incrementCounter()
incrementCounter()
incrementCounter()
the book said counter is 5.
but i typed these codes in an Xcode playground. It is 4!
i am confused.
The post-increment and post-decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's original value prior to the increment (or decrement) operation
So when you see playground, current value of counter is being printed.
But after evaluation of function, the value of counter changes and you can see updated value on the next line.
x++ operator is an operator that is used in multiple languages - C, C++, Java (see the C answer for the same question)
It is called post-increment. It increments the given variable by one but after the current expression is evaluated. For example:
var x = 1
var y = 2 + x++
// the value of x is now 2 (has been incremented)
// the value of y is now 3 (2 + 1, x has been evaluated before increment)
This differs from the ++x (pre-increment) operator:
var x = 1
var y = 2 + ++x
// the value of x is now 2 (has been incremented)
// the value of y is now 4 (2 + 4, x has been evaluated after increment)
Note the operator is getting removed in the next version of Swift, so you shouldn't use it anymore.
It's always better to just write x += 1 instead of complex expressions with side effects.
The value of counter after your five calls to the incrementCounter closure will be 5, but the return of each call to incrementCounter will seemingly "lag" one step behind. As Sulthan writes in his answer, this is due to x++ being a post-increment operator: the result of the expression will be returned prior to incrementation
var x = 0
print(x++) // 0
print(x) // 1
Also, as I've written in my comment above, you shouldn't use the ++ and -- operators as they will be deprecated in Swift 2.2 and removed in Swift 3. However, if you're interested in the details of post- vs pre-increment operator, you can find good answers here on SO tagged to other languages, but covering the same subject, e.g.
What is the difference between ++i and i++?
It's worth mentioning, however, a point that is relevant to Swift > 2.1 however, and that don't really relate to the ++ operator specifically.
When you initiate the closure incrementCounter as
var someOne : Int = 0
let incrementCounter = {
someInt
}
The closure is implictly inferred to be of type () -> Int: a closure taking zero arguments but with a single return of type Int.
let incrementCounter: () -> Int = {
return someInt
}
Hence, what you seemingly "see" in you playground is the unused (non-assigned) return value of the call to incrementCounter closure; i.e., the result of the expression incrementCounter().
Whereas the value of counter is never really printed in the right block of your playground (unless you write a line where the result of that line:s expression is counter).
++ and -- before the identifier add/subtract one, and then return its value.
++ and -- after the identifier return its value, and then add/subtract 1.
They were removed in Swift 3.0, but you can add them back:
prefix operator --
prefix operator ++
postfix operator --
postfix operator ++
prefix func ++(_ a : inout Int) -> Int {
a += 1
return a
}
prefix func --(_ a : inout Int) -> Int {
a -= 1
return a
}
postfix func ++(_ a: inout Int) -> Int {
defer { a += 1 }
return a
}
postfix func --(_ a: inout Int) -> Int {
defer { a -= 1 }
return a
}
var a = 11
print(a++) // 11
print(a) // 12
var b = 5
print(--b) // 4
print(b) // 4
even though there are a lot of answers and all of them are clear i added this snippet to show you how to replace your code with 'new' syntax, where ++ and or -- are deprecated. at first your own code
var counter = 0
let incrementCounter = {
counter++
}
let i0 = incrementCounter() // 0
let i1 = incrementCounter() // 1
// .....
how to rewrite it in future Swift's syntax? lets try the recommended replacement ...
var counter = 0
let ic = {
counter += 1
}
let i0 = ic() // () aka Void !!!
let i1 = ic() // ()
but now the result of ic() is Void! Hm ... OK, the next attempt could looks like
var counter = 0
let ic = {
counter += 1
return counter
}
but now the code doesn't compile with error: unable to infer closure return type in current context :-), so we have to declare it (it was not necessary in our original version)
var counter = 0
let ic:()->Int = {
counter += 1
return counter
}
let i0 = ic() // 1
let i1 = ic() // 2
// .....
it works, but the results are not the same. that is because in original code ++ operator was used as post-increment operator. so, we need another adjustment of our 'new' version
var counter = 0
let ic:()->Int = {
let ret = counter
counter += 1
return ret
}
let i0 = ic() // 0
let i1 = ic() // 1
// .....
yes, i would like to see my familiar unary ++ and / or -- will be also in the future versions of Swift
The thing which you are doing is post increment.
First Learn the difference between Pre & Post Increment
In Post Increment, the value counter after increment contains incremented value (i.e 5)
but if we return, then it will contain old value (i.e 4).
In Pre Increment, both the value and return value is incremented.
Lets look into your code now,
counter++ makes a copy, increases counter, and returns the copy (old value).
so if you print counter it will have incremented value (i.e 5) but, if you return counter (i.e your doing so with incrementCounter) it contains the old value (i.e 4).
because of which incrementCounter only showing upto 4.
CHECK OUTPUT
Solution :
change counter++ to ++counter
CHECK OUTPUT

How do you assign a slice of numbers to an array in swift

x is an object that holds an array called point.
x implements the subscript operator so you can do things, like x[i] to get the array's ith element (of type T, which is usually an Int or Double).
This is what I want to do:
x[0...2] = [0...2]
But I get an error that says ClosedInterval<T> is not convertible to Int/Double.
Edit1:
Here is my object x:
let x = Point<Double>(dimensions:3)
For kicks and giggles: define x as [1.0,2.0,0.0]
I can get the first n elements via x[0...2].
What I want to know is how to update x[0...2] to hold [0.0, 0.0.0.0] in one fell swoop. Intuitively, I would want to do x[0...2] = [0...2]. This does not work as can be seen in the answers. I want to update x without iteration (on my end) and by hiding the fact that x is not an array (even though it is not).
[0...2] is an array with one element which, at best, will be a Range<Int> from 0 through 2. You can't assign that to a slice containing, say, Ints.
x[0...2] on the other hand is (probably) a slice, and Sliceable only defines a get subscript, not a setter. So even if the types were more compatible - that is, if you tried x[0...2] = 0...2, which at least is attempting to replace a range within x with the values of a similarly-sized collection - it still wouldn't work.
edit: as #rintaro points out, Array does support a setter subscript for ranges – so if x were a range you could do x[0...2] = Slice(0...2) – but it has to be a slice you assign, so I'd still go with replaceRange.
If what you mean is you want to replace entries 0 through 2 with some values, what you want is replaceRange, as long as your collection conforms to RangeReplaceableCollection (which, for example, Array does):
var x = [0,1,2,3,4,5]
var y = [200,300,400]
x.replaceRange(2..<5, with: y)
// x is now [0,1,200,300,400,5]
Note, the replaced range and y don't have to be the same size, the collection will expand/contract as necessary.
Also, y doesn't have to an array, it can be any kind of collection (has to be a collection though, not a sequence). So the above code could have been written as:
var x = [0,1,2,3,4,5]
var y = lazy(2...4).map { $0 * 100 }
x.replaceRange(2..<5, with: y)
edit: so, per your edit, to in-place zero out an array of any size in one go, you can do:
var x = [1.0,2.0,0.0]
// range to replace is the whole array's range,
// Repeat just generates any given value n times
x.replaceRange(indices(x), with: Repeat(count: x.count, repeatedValue: 0.0))
Adjust the range (and count of replacing entries) accordingly if you want to just zero out a subrange.
Given your example Point class, here is how you could implement this behavior assuming it's backed by an array under the hood:
struct Point<T: FloatLiteralConvertible> {
private var _vals: [T]
init(dimensions: Int) {
_vals = Array(count: dimensions, repeatedValue: 0.0)
}
mutating func replaceRange
<C : CollectionType where C.Generator.Element == T>
(subRange: Range<Array<T>.Index>, with newElements: C) {
// just forwarding on the request - you could perhaps
// do some additional validation first to ensure dimensions
// aren't being altered...
_vals.replaceRange(subRange, with: newElements)
}
}
var x = Point<Double>(dimensions:3)
x.replaceRange(0...2, with: [1.1,2.2,3.3])
You need to implement subscript(InvervalType) to handle the case of multiple assignments like this. That isn't done for you automatically.

Let type in a for in loop

I'm playing around with Swift.
Why is it possible to declare let type in a for loop? As far as I know, let means constant, so I'm confused.
func returnPossibleTips() -> [Int : Double] {
let possibleTipsInferred = [0.15, 0.18, 0.20]
//let possibleTipsExplicit:[Double] = [0.15, 0.18, 0.20]
var retval = Dictionary<Int, Double>()
for possibleTip in possibleTipsInferred {
let inPct = Int(possibleTip * 100)
retval[inPct] = calcTipWithTipPct(possibleTip)
}
return retval
}
The lifespan of the inPct constant is only during the loop iteration since it is block scoped:
for i in 1...5 {
let x = 5
}
println(x) // compile error - Use of unresolved identifier x
In every iteration inPct refers to a new variable. You can not assign to any of the inPcts in any iteration since they were declared with let:
for i in 1...5 {
let x = 5
x = 6 // compile error
}
In basic words when you define: let possibleTipsInferred = [0.15, 0.18, 0.20] it means that possibleTipsInferred is read-only variable. You can iterate over it but not change it.
Furthermore, in Swift you can't write:
let a:Int?
a = 5 // compile ERROR
because a has the value nil, and you can't change it.
in a for..in loop, each iteration i is recreated and receives a new instance per loop.
Therefore you can make it constant and write let.
The for loop executes a series of statements so each time we loop through we are in a new "scope." This is very similar to how C handles for loops.
Each time we run through the for loop the stack constant is freed so we can reassign the value of inPct.
If we had predeclared a variable outside of the for loop and given it a value we would update the variable's value each time we ran through the for loop.
Here's a link to the docs for for loops in swift:Control Flow