Why won't mapping change the array with this function:
array[1..5].map! { |part| "<p>#{part}</p>" }
I know that I could just assign array[1..5] to the result, but there's probably some better way to do it.
How should I do it?
[](*args)
Returns a new array populated with the given objects.
Source
So, you're actually applying map! to a new array of just that range, not the actual array.
Assignment is necessary.
Well, it does change the array. You're just not seeing it, because you never assign the array to anything, so it will be immediately garbage collected again.
Related
I have a to-many relationship, where a List can have many Items.
I then have a List variable, whith the list Entity that I need. No problems there.
The list is a tableview and the user can add rows, so when they add a row it adds it as an Item relationship to the List.
I can then use List.valueForKey("item") to get the Items. But the return type is AnyObject. Is there a better way than casting it to an NSSet and then having to go through and grt the values and putting them into an array?
In other words, how can I get the value of all the Item entities and put them into an array ?
Pd: the Relationship is Ordered.
You cannot successfully cast something to something it is not. Casting is merely a way of revealing the truth to the compiler. The implication is that you know more than the compiler does. But you must tell the compiler the truth! If you lie to the compiler, you will crash at runtime.
So, if casting to an array causes a crash at runtime, that's because this is not an array. You can only cast to what it really is.
What is the suggested approach for updating an objects value in an array, bearing in mind the array may have been reordered?
I'm wondering how dangerous using index based paths is, when an array could have possibly changed via a deletion, or reorder.
Would it be better to use objects instead, I wonder.
If you are using a mutable list, it is inherently unsafe to update an object by its position in a list. The right thing to do is to use deref. Assuming you have a list of references (the most common case) you can dereference a Model at its position in the list. This will ensure it points to the object's identity path rather than the index in the list. Then you can update the object directly without worrying about whether it has moved around in the list.
how to check if an array is sorted?
I am sorting using sort descriptors. Is there any API to check if an array is already in sorted order in Swift/Objective-C.
Thanks
i think there is no frame work, simply iterate truth the array, and check if the current element greater or equal (or less or equal, or which kind of sorting you look for) is. This is the easiest way. Look please at this Question Solution
As far as I know, there isn't a built in way to check if an array is already sort descriptors. The best way to check is to iterate through the array and check if each element should come before the element precedes it (using whatever definition of "should come before" you want for your sort). If you're sorting custom objects, you can write some sort of compareTo method that compares two objects of your class, which will make it convenient to check using the method I described.
I have an array that contains 2 objects. In order to store it on my backend server service, I need to store it inside of another array.
So later on, when I call my server and tell it I want the array object, it sends me a new array object that is holding my original array object.
I need to loop through the new array (that contains my original array), and then loop through all of the objects inside of my original array.
I know how to do a normal for loop and loop through an array, but I have never had to do it like this where you need to loop through an array that is contained inside of another array.
I have been thinking about ways to do this now for about an hour and really have no clue. I think what I need to do is technically called "looping through nested arrays" but I can't seem to find anything about doing this with objective-c.
Thanks for the help.
Use a nested for loop and you can iterate through the objects in both arrays:
for(NSArray* array in arrays){
for(object* thing in array){
//do what you want with thing in arrays
}
}
Do you need to loop through every object in both arrays, or do you need to fetch the object from the outer array and just loop through that?
If you need to loop through all objects in both arrays, #JMarsh 's code will do that.
If you only need to fetch the inner array, then just use an explicit fetch Following JMarsh's format:
NSArray *innerArray = arrays[1]; //Or whatever array index is correct
for(id thing in innerArray)
{
//do what you want with thing
}
Is the best way to sort an array in Delphi is "alphanumeric".
I found this comment in an old code of my application
" The elements of this array must be in ascending, alphanumeric
sort order."
If so ,what copuld be the reason?
-Vas
There's no "best" way as to how to sort the elements of an array (or any collection for that fact). Sort is a humanized characteristic (things are not usually sorted) so I'm guessing the comment has more to do with what your program is expecting.
More concretely, there's probably other section of code elsewhere that expect the array elements to be sorted alphanumerically. It can be something so simple as displaying it into a TreeView already ordered so that the calling code doesn't have to sort the array first.
Arrays are represented as a contiguous memory assignment so that access is fast. Internally the compiler just does a call to GetMem asking for SizeOf(Type) * array size. There's nothing in the way the elements are sorted that affects the performance or memory size of the arrays in general. It MUST be in the program logic.
Most often an array is sorted to provide faster search times. Given a list of length L, I can compare with the midpoint (L DIV 2) and quickly determine if I need to look at the greater half, or the lesser half, and recursively continue using this pattern until I either have nothing to divide by or have found my match. This is what is called a Binary search. If the list is NOT sorted, then this type of operation is not available and instead I must inspect every item in the list until I reach the end.
No, there is no "best way" of sorting. And that's one of the reasons why you have multiple sorting techniques out there.
With QuickSort, you even provide the comparison function where you determine what order you ultimately want.
Sorting an array in some way is useful when you're trying to do a binary search on the array. A binary search can be extremely fast, compared to other methods. But if the sort error is wrong, the search will be unable to find the record.
Other reasons to keep arrays sorted are almost always for cosmetic reasons, to decide how the array is sent to some output.
The best way to re-order an array depends of the length of the array and the type of data it contains. A QuickSort algorithm would give a fast result in most cases. Delphi uses it internally when you're working with string-lists and some other lists. Question is, do you really need to sort it? Does it really need to stay an array even?
But the best way to keep an array sorted is by keeping it sorted from the first element that you add to it! In general, I write a wrapper around my array types, which will take care of keeping the array ordered. The 'Add' method will search for the biggest value in the array that's less or equal to the value that I want to add. I then insert the new item right after that position. To me, that would be the best solution. (With big arrays you could use the binary search method again to find the location where you need to insert the new record. It's slower than appending records to the end but you never have to wonder if it's sorted or not, since it is...