Why result of reversed() does behave itself like being an Array? [duplicate] - swift

I'm wondering about the reversed() method on a swift Array:
var items = ["a", "b", "c"]
items = items.reversed()
the signature of the reversed method from the Apple doc says that it returns a
ReversedRandomAccessCollection<Array<Element>>
could that be assigned back to items without doing what the apple doc say which is
For example, to get the reversed version of an array, initialize a new Array instance from the result of this reversed() method.
or would it give problem in the future? (since the compiler doesn't complain)

There are 3 overloads of reversed() for an Array in Swift 3:
Treating the Array as a RandomAccessCollection,func reversed() -> ReversedRandomAccessCollection<Self> (O(1))
Treating the Array as a BidirectionalCollection,func reversed() -> ReversedCollection<Self> (O(1))
Treating the Array as a Sequence,func reversed() -> [Self.Iterator.Element] (O(n))
By default, reversed() pick the RandomAccessCollection's overload and return a ReversedRandomAccessCollection. However, when you write
items = items.reversed()
you are forcing the RHS to return a type convertible to the LHS ([String]). Thus, only the 3rd overload that returns an array will be chosen.
That overload will copy the whole sequence (thus O(n)), so there is no problem overwriting the original array.
Instead of items = items.reversed(), which creates a copy of the array, reverse that and copy it back, you could reach the same effect using the mutating function items.reverse(), which does the reversion in-place without copying the array twice.

Related

How do i use joined() function after using sorted() in swift?

let var1 = "AnyCode".sorted()
print(var1.joined(separator:""))
ERROR: No exact matches in call to instance method 'joined'
I am trying to join the array after sorting the string. = "AnyCode"
I was expecting the output was = ACdenoy
But it is giving an error.
A Swift String is a collection of Characters, and sorted() applied to a collection returns an array with the collection elements in sorted order.
So var1 has the type [Character], and you can simply create a new string from that array with:
let var1 = "AnyCode".sorted()
print(String(var1)) // ACdenoy
Alternatively to Martin R's answer (but not better than that answer), you might have said
print(var1.map(String.init).joined())
...turning the characters to strings before trying to join the array elements.

Swift array address is not same as &array[0]

Document said:
An in-out expression that contains a mutable variable, property, or subscript reference of type Type, which is passed as a pointer to the address of the left-hand side identifier.
A [Type] value, which is passed as a pointer to the start of the array.
But when I run the following code :
func print<Type>(unsafePointer pointer: UnsafePointer<Type>) {
print("\(pointer) ==> \(pointer.pointee) : \(Type.self)")
}
var array = [1, 2, 3, 4]
print(unsafePointer: array)
print(unsafePointer: &array[0])
I get
0x0000000104204240 ==> 1 : Int
0x00007ffeefbff440 ==> 1 : Int
Why their addresses are different?
Here
print(unsafePointer: array)
a pointer to the first element of the array storage is passed to the function. And here
print(unsafePointer: &array[0])
the subscript operator is called on the array (returning an Int) and the address of that (temporary) integer is passed to the function, not the address where the original array element is stored.
That becomes more obvious if you call the functions twice:
var array = [1, 2, 3, 4]
print(unsafePointer: array) // 0x00007ffeefbff2e0
print(unsafePointer: array) // 0x00007ffeefbff2e0, same as previous address
print(unsafePointer: &array[0]) // 0x00007ffeefbff320
print(unsafePointer: &array[0]) // 0x00007ffeefbff340, different from previous address
In addition, passing an in-out expression to a function can make a temporary copy, see for example Swift: always copies on inout?.
Arrays in Swift have value semantics, not the reference semantics of arrays in C or Objective-C. The reason you're seeing different addresses (and addresses at all) is that every time you pass the array as a parameter, you're actually telling Swift to bridge your Array struct to an instance of NSArray.

coffee script - remove from one array based on second array contents

I have 2 arrays - one has attribute called vcdName and the other has attribute called name. I want to remove from array 1 all entries where I find the value of vcdName in the second array. So:
array one
data...vcdName=a
data...vcdName=b
array two
data...name=a
I want to filter array one and remove the first entry (because vcdName value a is found in array two).
I understand I can use lodash functions and I tried the following. I believeusing is will return me those entries that match and I need to put in filteredArray those entries where no match is found in arraytwo.
filteredArray = _.filter(#arrayone, (vcd) -> vcd.vcdName is #arraytwo)
I hope I have not gotten too confusing. It feels this should be moderately easier than I am making it.
Generically I need to do this
for each entry in arrayone
for each entry in arraytwo
does entry from arrayone.vcdName == arraytwo.name - if yes then I do not want that entry from arrayone
I hope that helps
filter is in js core so I don't see why you'd need to use the lodash function, but anyway you can do it like this:
arraytwo_names = #arraytwo.reduce (dict, entry) ->
dict[entry.name] = true
dict
, {}
results = #arrayone.filter (x) ->
!arraytwo_names[x.vcdName]
The reduce is not strictly speaking necessary, since you could loop through arraytwo each iteration of the filter, but it is an optimization and makes the runtime O(N) and not O(N^2).

Scala create array of empty arrays

I am trying to create an array where each element is an empty array.
I have tried this:
var result = Array.fill[Array[Int]](Array.empty[Int])
After looking here How to create and use a multi-dimensional array in Scala?, I also tried this:
var result = Array.ofDim[Array[Int]](Array.empty[Int])
However, none of these work.
How can I create an array of empty arrays?
You are misunderstanding Array.ofDim here. It creates a multidimensional array given the dimensions and the type of value to hold.
To create an array of 100 arrays, each of which is empty (0 elements) and would hold Ints, you need only to specify those dimensions as parameters to the ofDim function.
val result = Array.ofDim[Int](100, 0)
Array.fill takes two params: The first is the length, the second the value to fill the array with, more precisely the second parameter is an element computation that will be invoked multiple times to obtain the array elements (Thanks to #alexey-romanov for pointing this out). However, in your case it results always in the same value, the empty array.
Array.fill[Array[Int]](length)(Array.empty)
Consider also Array.tabulate as follows,
val result = Array.tabulate(100)(_ => Array[Int]())
where the lambda function is applied 100 times and for each it delivers an empty array.

What does Array as Object mean in Basic4Android?

SQL1.ExecNonQuery2("INSERT INTO table1 VALUES(?,?,?)",Array As Object("def",3,4))
I don't seem to understand why the argument list in the above statement is declared in the form of Array as Object('xx','xx''xx').How is it exactly being converted into a list parameter ?
Array As xxx is a shorthand syntax for declaring a new array and assigning the values.
Array As Object("def", 3, 4)
Is equivalent to:
Dim arr As Object(3)
arr(0) = "def" : arr(1) = 3 : arr(1) = 4
Basic4android automatically wraps arrays as lists when needed. The items are not copied, it is the whole array that is wrapped in a list. Therefore the above code is valid as it creates an array which is then wrapped as a List.