Splat for removing an item from an array - coffeescript

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

Related

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

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

How to reuse array from Zend_Db_Select::getPart('where')

I see a similar question here, but it's 4-year-old. Is there a better way now to re-use the array obtained from getPart('where')?
getPath('where') returns an array of individual WHERE clause segments like below (example).
[0] => (`e`.`type` = 'bleh')
[1] => (AND `e`.`name` = 'blah')
I can do some string operations to clean it up the second segment and re-add to Zend_Db_Select, but it seems really hacky.

Strange behavior in coffeescript for loop

I'm trying to iterate over array in CoffeeScript but there is some kind of strange behavior.
For example look on this code:
kapa = [1]
for i in kapa
console.log 't'
kapa.push 1
I expect that this code produce an infinite sequence of 't' symbols in console. But in reality it prints only one 't'.
What is the logic behind this behavior?
And how can I achieve expected behavior?
Explanation:
elements= [1]
for i in elements
proccess(element)
if someCond
newElement = ...
element.push(newElement) #
I want to achieve behavior when all newElements will be processed
The reason it only prints it out once is because that coffeescript compiles to this JS:
var i, kapa, _i, _len;
kapa = [1];
for (_i = 0, _len = kapa.length; _i < _len; _i++) {
i = kapa[_i];
console.log('t');
kapa.push(1);
}
So it calculates the length of the array in the beginning and doesn't get updated when you kapa.push(1). I would say this is expected behavior and moreover, modifying an array while you are iterating over it sounds like a bad idea. In other languages like Java, you would get an Exception if you tried to do that.
If you really want it to print infinite t and push 1 onto the array every time, you would need an infinite loop:
kapa = [1]
while true
console.log( 't' )
kapa.push( 1 )
(but thats obviously discouraged and I'm not sure why you would want that behavior)
UPDATE
Based on the updated question, you could use the array as a queue-like structure
elements= [1]
while nextElem = elements.shift()
proccess(nextElem)
if someCond
newElement = ...
elements.push(newElement)
Given your added explanation, maybe you are using your array as a queue:
elements= [1]
while elements.length
proccess(elements.shift())
if someCond
newElement = ...
element.push(newElement)
Notice the use of a while loop (whose end condition is evaluated at each iteration), and elements.shift that removes the first element of the queue.

Looping through documents in matlab

I am attempting to loop through the variable 'docs' which is a cell array that holds strings, i need to make a for loop that colllects the terms in a cell array and then uses command 'lower' and unique to create a dictionary.
Here is the code i've tried sp far and i just get errors
docsLength = length(docs);
for C = 1:docsLength
list = tokenize(docs, ' .,-');
Mylist = [list;C];
end
I get these errors
Error using textscan
First input must be of type double or string.
Error in tokenize (line 3)
C = textscan(str,'%s','MultipleDelimsAsOne',1,'delimiter',delimiters);
Error in tk (line 4)
list = tokenize(docs, ' .,-');
Generically, if you get an "must be of type" error, that means you are passing the wrong sort of input to a function. In this case you should look at the point in your code where this is taking place (here, in tokenize when textscan is called), and doublecheck that the input going in is what you expect it to be.
As tokenize is not a MATLAB builtin function, unless you show us that code we can't say what those inputs should be. However, as akfaz mentioned in comments, it is likely that you want to pass docs{C} (a string) to tokenize instead of docs (a cell array). Otherwise, there's no point in having a loop as it just repeatedly passes the same input, docs, into the function.
There are additional problems with the loop:
Mylist = [list; C]; will be overwritten each loop to consist of the latest version of list plus C, which is just a number (the index of the loop). Depending on what the output of tokenize looks like, Mylist = [Mylist; list] may work but you should initialise Mylist first.
Mylist = [];
for C = 1:length(docs)
list = tokenize(docs{C}, ' .,-');
Mylist = [Mylist; list];
end

Understanding the syntax

I have this couple of lines which is difficult to understand..
oframes1 = do_localmax( difofg.octave{o}, 0.8*thresh, difofg.smin ) ;
oframes = [oframes1 , do_localmax( - difofg.octave{o}, 0.8*thresh, difofg.smin)] ;
here,
do_localmax is a function
thresh is a variable
difofg is also a function
I understand that the 1st line calls the function and passes the parameters but it is difficult understanding the second line and also what kind of syntax is difofg.octave{o}
Syntactically:
difofg is not a function; it's a variable, probably a struct or a class object. difofg.octave and difofg.smin get the element named octave or smin from that struct/object.
difofg.octave is apparently a cell array, and difofg.octave{o} gets the oth element of that cell array.
The second line creates an array with two elements: the first is oframes1, and the second is the result of the second call to do_localmax. Maybe this equivalent code will make it clearer what's happening:
oframes1 = do_localmax( difofg.octave{o}, 0.8*thresh, difofg.smin);
oframes2 = do_localmax( -difofg.octave{o}, 0.8*thresh, difofg.smin);
oframes = [oframes1, oframes2];