Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

You could extend the Array prototype with a custom method:
***// check if an element exists in array using a comparer function
// comparer : function(currentElement)
Array.prototype.inArray = function(comparer) {
for(var i=0; i < this.length; i++) {
if(comparer(this[i])) return true;
}
return false;
};

// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function(element, comparer) {
if (!this.inArray(comparer)) {
this.push(element);
}
};

var array = [{ name: "tom", text: "tasty" }];
var element = { name: "tom", text: "tasty" };
array.pushIfNotExist(element, function(e) {
return e.name === element.name && e.text === element.text;
});
***
3 years ago
You can use the Standard PHP Library (SPL) to "hide" the recursion.
***$a = array(1,2,array(3,4, array(5,6,7), 8), 9);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
foreach($it as $v) {
echo $v, " ";
}***

prints
***1 2 3 4 5 6 7 8 9***
3 years ago
You should try this
***var z = new int[x.Length + y.Length];
x.CopyTo(z, 0);
y.CopyTo(z, x.Length);***
3 years ago
Here is what works for me:
***[1, "", 2, "hello", nil].reject(&:blank?)***

output:
***[1, 2, "hello"]***
3 years ago
You can use **Array.sort**.

Here's an example:
***var arr = [{
"updated_at": "2012-01-01T06:25:24Z",
"foo": "bar"
},
{
"updated_at": "2012-01-09T11:25:13Z",
"foo": "bar"
},
{
"updated_at": "2012-01-05T04:13:24Z",
"foo": "bar"
}
]

arr.sort(function(a, b) {
var keyA = new Date(a.updated_at),
keyB = new Date(b.updated_at);
// Compare the 2 dates
if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
});

console.log(arr);***
3 years ago
If order is not important and you don't need to worry about duplicates then you can use set intersection:
***>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) & set(b))
[1, 3, 5]***
3 years ago
***
$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
$xml = new SimpleXMLElement('');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();***

results in
***

bla
foo
stack
***

keys and values are swapped - you could fix that with **array_flip()** before the array_walk. **array_walk_recursive** requires PHP 5. you could use **array_walk** instead, but you won't get **'stack' => 'overflow'** in the xml then.
3 years ago
If you would like to prepend array (a1 with an array a2) you could use the following:

***var a1 = [1, 2];
var a2 = [3, 4];
Array.prototype.unshift.apply(a1, a2);
console.log(a1);
// => [3, 4, 1, 2]***
3 years ago
For people using ES2015 (ES6)
You can now use the Spread Syntax to concatenate arrays:
***const arr1 = [0, 1, 2],
arr2 = [3, 4, 5];

const result1 = [...arr1, ...arr2]; // -> [0, 1, 2, 3, 4, 5]

// or...

const result2 = [...arr2, ...arr1]; // -> [3, 4, 5, 0, 1, 2]***
3 years ago
Use a **Set** or **NSOrderedSet** to remove duplicates, then convert back to an **Array**:
***let uniqueUnordered = Array(Set(array))
let uniqueOrdered = Array(NSOrderedSet(array: array))***
3 years ago
The method is the following:
***brew tap homebrew/science
brew install Caskroom/cask/xquartz
brew install r***

The **gcc** package (will be installed automatically as a required dependency) in the **homebrew/science** tap already contains the latest fortran compiler (**gfortran**), and most of all: the whole package is precompiled so it saves you a lot of compilation time.

This answer will also work for El Capitan and Mac OS Sierra.

In case you don't have XCode Command Line Tools (CLT), run from terminal:
***xcode-select --install***
3 years ago
I like putting different functionality in their own files.

But I don't like R's package system. It's rather hard to use.

I prefer a lightweight alternative, to place a file's functions inside an environment (what every other language calls a "namespace") and attach it. For example, I made a 'util' group of functions like so:

***util = new.env()

util$bgrep = function [...]

util$timeit = function [...]

while("util" %in% search())
detach("util")
attach(util)***

This is all in a file util.R. When you source it, you get the environment 'util' so you can call **util$bgrep()** and such; but furthermore, the **attach()** call makes it so just **bgrep()** and such work directly. If you didn't put all those functions in their own environment, they'd pollute the interpreter's top-level namespace (the one that **ls()** shows).
3 years ago