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

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).

Related

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.

Splat for removing an item from an array

Although I know about splats, still I can't quite grasp the last line from the following code:
class Borrowable extends Decorator
constructor: (#libraryItem) ->
removeBorrower: (borrower) ->
#borrowers[t..t] = [] if ( t = #borrowers.indexOf(borrower) ) > -1
Btw, this code was copied from https://github.com/aksharp/Design-Patterns/blob/master/CoffeeScript/Decorator.coffee
Im assuming this is Destructuring Assignment, still I can't quite get my head around what's it's happening behind the scenes.
Could you help clarify this?
Let's have a closer look at the last line:
#borrowers[t..t] = [] if ( t = #borrowers.indexOf(borrower) ) > -1
I'm not sure if this form counts as Destructuring Assignment, probably it is.
First, it calls #borrowers.indexOf(borrower) to check that borrower is present inside of the #borrowers array and to get it's index.
It's conventional to use borrower in #borrowers form instead of #borrowers.indexOf(borrower) > -1, but in this case we need index of an element as well.
If borrower is present in #borrowers, it gets the part of the #borrowers array between indexes t and t
#borrowers[t..t]
which is the [borrower], and assigns it to the empty array [], thus removing the borrower from #borrowers array.
Here is js-like equivalent of this assignment:
#borrowers.splice t, 1

How to test, if an element is in a list?

Is there a built-in function to test, if a given element is in a list in Rexx?
I could not find one. The alternative would be to loop over the list and check each element manually.
No (unless things have changed); just loop through the list.
An alternative is instead / as well have a lookup variable
i.e.
lookup. = 0 /* not all versions of Rexx support
default initialisation like this */
....
addToList:
parse arg item
numberInList = numberInList + 1
list.numberInList = item
lookup.item = 1
return
You can then check if item is in the list by
if lookup.item = 1 then do
......
It depends what you mean by a list.
At work, I use classic REXX. I frequently store lists of words in a single variable, space delimited. So WORDPOS() is the built-in function I use.
If you are using a List class in ooREXX. then why not use the hasItem method from the Collection class.

How to check if an arbitrary number of matlab.unittest.constraints is satisfied by a cell array?

I have a cell array of matlab.unittest.constraints and a cell array of values. I'd like to see if the values match the constraints (respectively). Of course, I can just use a for cycle, something like the following code:
satisfied = zeros(1,argLength);
for i=1:argLength
satisfied(i) = satisfiedBy(cons{i}, val{i});
end;
answer = all(satisfied);
but knowing MATLAB, there must be a way to condense all that into a single line, I just don't know it. I compare the lengths of the arrays beforehand and return false if they're not equal.
Here is a possible CELLFUN statement:
satisfied = cellfun(#satisfiedBy, cons, val);
Make sure satisfiedBy returns only single numeric/logical value.

select items from a struct array matlab- based on condition

I have a struct array named Lst. Every struct has the following form:
Point (x,y)
Type (1-6)
I want get the separate array of points for each type. How can I get it?
Lst(Lst.Type==1);
won't work since Type is not a field of Lst but of Lst(i).
In addition, is there a way to save the indexes of each item or an alternative way to then combine them again to the original order?
L1 = Lst([Lst.Type]==1); will give you the subset L1 of Lst where Type == 1.
Likewise, you can use idx1 = find([Lst.Type]==1) to memorize your indexes.
EDIT: the above uses the [] operator to aggregate the field elements Type of Lst into an array. To your comment/question, you could use the exact same operator also to obtain an array of specific field elements X of a subset of the structured array, as in
X1 = [Lst([Lst.Type]==1).X];