What is the prescribed way to append a value to an Array in CoffeeScript? I've checked the PragProg CoffeeScript book but it only discusses creating, slicing and splicing, and iterating, but not appending.
Good old push still works.
x = []
x.push 'a'
Far better is to use list comprehensions.
For instance rather than this:
things = []
for x in list
things.push x.color
do this instead:
things = (x.color for x in list)
If you are chaining calls then you want the append to return the array rather than it's length.
In this case you can use .concat([newElement])
Has to be [newElement] as concat is expecting an array like the one its concatenating to.
Not efficient but looks cool in the right setting.
Related
I am trying to understand different Swift's basics better. I bumped on the reversed array concept in Paul Hudson's videos.
He said that the array will be printed in the same order as the original one, but also that other operations will be done on the reversed version of the array.
And so I did this:
let presidents = ["Bush", "Obama", "Trump", "Biden"]
let reversedPresidents = presidents.reversed()
print(reversedPresidents[2])
And I received:
No exact matches in call to subscript
error.
Why?
It's explained reversed documentation.
First of all, it returns the type ReversedCollection<Array<Element>>
Secondly, it doesn't really change the order of array, as explained here:
or ordinary collections c having bidirectional indices:
c.reversed() does not create new storage
c.reversed().map(f) maps eagerly and returns a new array
In other words: reversed() is able to iterate the provided array in reverse order, but does not create a reversed array
So if you want to create a reversed array, you can either do this:
let reversedPresidents = presidents.reversed().map { $0 }
or, as mentioned in comment above,
let reversedPresidents = Array(presidents.reversed())
I'd like to know if there's a way to understand if an array contain another array inside him. I ask this question because I'm trying to loop throught two or plus nested arrays but first I need to check this in Angular.
Thanks in advance for the help
var a = [[1,2,3],[4,5,6]]
console.log(Array.isArray(a[0]))
You can check this using Array.isArray function.
There are many ways this can be accomplished, but it depends on the size of the parent array, how deeply nested it's elements are, how does much does performance matter, etc.
If your array only goes one level deep (inner arrays don't contain arrays), you can use these methods:
var arr = [[1,2,3], 'foo', 4, ['foo', 'bar']];
// use .some() to test elements
arr.some((element) => Array.isArray(element));
// returns the index of the first found array, or -1 if no arrays are present
arr.indexOf((element) => Array.isArray(element));
This article gives a pretty good overview of all options, including the performance of each.
fun Sample(){
val x = 10
x = 11 // will give error as it cannot be reassigned
val arr = arrayOf(1,2,3)
arr[0] = 5 // will not give any error but why ? aren't they supposed to be final?
}
Why val doesn't works for arrays?
arr itself is immutable and can't be reassigned....the contents of array are not. For example you could not do:
val arr1 = arrayOf(1,2,3)
val arr2 = arrayOf(4,5,6)
arr1 = arr2 // error
An alternative if you want a "read-only" list is to use listOf()
By declaring and array as with val, you basically declare the reassigning for the array isn't possible, but array items are still reassignable
vals aren't really immutable data, it's better to think of them as read-only references. You can change the value of a var, but you can't change the thing a val points at. But that doesn't mean the thing itself can't change!
If the object itself is mutable, or has mutable var properties, or has val properties that point to other mutable things... you get the idea. You can't guarantee absolute immutable state, it's just not baked into the language (or Java itself - final just means you can't reassign the variable!)
That's why Kotlin has a bunch of features to try and help you avoid mutable state, or at least to prevent you from changing things. Collections (and the return types of the functions that operate on them) are usually immutable by default, and you have to explicitly ask for a mutableList or whatever. That's enforcing immutability by stopping you from accessing methods like add and remove, but it still doesn't stop you from putting mutable objects in there!
data classes are another tool, and that's sort of aimed at a more functional, data-driven approach. The idea is that ideally, you'll just put vals in them, and any non-primitive data types you use will also comprise of vals - so when you drill down, everything ends in immutable variables, so the whole thing is immutable overall. Things like the copy method allow you to "change" the data without actually mutating it, similar to a Copy On Write approach.
I'd also say that in my experience, it's not uncommon for arrays specifically to always be treated as a mutable type, though. Like there's no immutable version of an array in Kotlin, and things like F# (which encourages functional transformations on its types) explicitly has setters on its Array types. So if you need a read-only array... use a List!
I can append to a single array using
{append var='name' value='Bob' index='first'}
However, if I have a multi-dimensional array such as:
$name[first][last] = ['this','array']
and I want to append another value to the array at $name[first][last] e.g. to make the array like this:
$name[first][last] = ['this','array','appended']
how can I do this in the smarty template?
You can do this without using append:
{$name[first][last][] = 'this'}
{$name[first][last][] = 'array'}
{$name[first][last][] = 'appended'}
I must highlight though - templates should be used for specific purpose: to display prepared data; having to do the above is a code smell
I've tested many cases to try achieve it and I think it's not possible (in documentation there is also no info or example of multidimensional key or var)
You should also really think do you need it at all. Logic should be in PHP and role of Smarty is only displaying data not manipulating them
What is the most efficient way to empty an Array or Vector in ActionScript 3?
I've always just re-initialized them:
vector = new Vector.<T>();
array = [];
It doesn't look like there's an empty() function or anything similar.
Is there a better way?
Re-initializing the array is fine in most cases, since the garbage collector will just sweep up the old array. Still, if you want to empty an array without creating a new one you can set array.length = 0
Another option is to use the splice method.
Array::splice documentation
For an array the following call empties it:
array.splice(0);
For a vector the second parameter is enforced, so the call becomes:
vector.splice(0, vector.length);