I have two PromiseArray objects, from within a controller, e.g. this.get('content.skills') and this.get('allSkills').
I'd like to do what is essentially set subtraction. For example:
[1, 2, 3] - [2, 3] // => [1]
Is there a straightforward to do this? There's an alias for doing intersections. My guess is that it will use rejectBy somehow, but I'm not quite sure how.
You can use http://emberjs.com/api/classes/Ember.MutableArray.html#method_removeObjects with the Promise's content as long as they are fulfilled.
P1.removeObjects(P2.toArray());
Related
How can one return the factors of an integer in a list? e.g. list_factors(6); > [1,2,3,6]? Is something like this possible? I looked through the documentation but didn't find anything like this tied to "factor" or "prime".
I think this is what you want:
listify(divisors(6)); > [1, 2, 3, 6]
Is there a preferred and type-safe way without forced typecast to remove nils from Array?
[1, nil].select{|x| !!x}
// => Array(Int32 | Nil)
Something like special select?
P.S.
The use case when I hit this issue - I want to calculate median and the sort won't work:
[1, nil].select{|x| !!x}.sort
Map with zeros [1, nil].map{|x| x || 0} won't work as unlike let's say sum for some operations the length does matter (median for example).
Array#compact will remove nils from the Array:
[1, nil].compact # => [1] (Array(Int32))
I need to make a list of numbers. These numbers represent binary masks. The first 100 or so masks are all included in this range. In the next group of masks only certain masks are included. I need a list similar to the following.
[1,2,3,5,6,7,8,9,10,30,34,48,53,62]
Can I do something like [range(1,10),30,34,48,53,62]
or do I need to create my list using range(1,10) and then append the next list to it?
Thanks
Python 3 actually allow you to build a list literal prepending an * to any iterable objects - which are in turn expanded in place:
>>> [1,2, *range(10), *range(2)]
[1, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
If you need this n older Pythons, or if you'd prefer to keep readability for people not too proeficient in Python who might have to walk through your code, an option is just to concatenate your different list fragments using the + operator:
a = list(range(1,10)) + [ 30,34,48,53,62]
Looks like I had to use the list(range(1,10)+[47,34,57]
solution
In Prolog, I'm struggling to understand how to bind a list of lists to a variable. For instance, say I have the predicate makeList (which I don't know how to write), then I should be able to type:
makeList([[0, 0], [1, 0]]).
Now I want to refer to [[0, 0], [1, 0]] in another predicate with a variable, ListList, like:
predicateThatDoesSomething(ListList) :- write(ListList).
Expected Output:
[[0, 0], [1, 0]]
Obviously predicateThatDoesSomething() knows about the variable named ListList already. But how do I make the predicate makeList()? I want to be able to type makeList([[ANY, LIST, IN, HERE], [ANOTHER, LIST]]) and have that be ListList for example.
You have to write a predicate that merge the two list, thus this predicate must unify with something like:
makeList(List1, List2, [List1, List2]).
After defined this predicate in you knowled base, you can interrogate the prolog engine asking:
makeList([1,2],[2,4],D).
And you'll get: D = [[1, 2], [2, 4]]
To make the other predicate (defined in your KB)
predicateThatDoesSomething(ListList) :- write(ListList).
know the result (output parameter, the results of unification), you have to logically and the statements. So you have to ask:
makeList([1,2],[2,4],D) , predicateThatDoesSomething(D).
I have the following question for homework
Define a function append lists that
takes a list of lists and returns a
new list containing the sublist
values. For example, append lists([[1,
2], [3, 4], [5]]) should return the
list [1, 2, 3, 4, 5] and append
lists([[1, 2], [3], [[4, 5]]]) should
return the list [1, 2, 3, [4, 5]].
I've tried various ways of creating this function in order to append the list so it gives the desired output to no avail so I came here looking for some help. I've found a few other ways of going about this online, but they use extensive methods that we haven't even dabbled in as of yet in my CPSC 121 class. We're limited to the basics in what we've learned.
Any help would be much appreciated!
By now, it is likely that the assignment is gone, but here is a solution:
def append_lists(lists):
output = []
for l in lists:
for e in l:
output.append(e)
return output
This appends each element of each list to the output of the function, which eliminates exactly one level of nesting in the elements.