How can I chain two rails Queries together elegantly?
By : user3561230
Date : March 29 2020, 07:55 AM
it helps some times I have the following model object: code :
Rails2:
insight_count = Insight.count(:conditions => {:supporter_id => 15, endorse_id => 15})
insights = Insight.all(:conditions => {:supporter_id => 15, endorse_id => 15})
Rails3:
insight_count = Insight.where(:supporter_id => 15, endorse_id => 15).count
insights = Insight.where(:supporter_id => 15, endorse_id => 15).all

PHP Coding Techniques, Is it Necessary/Proper Coding Technique to create variables prior to using them
By : Chris D
Date : March 29 2020, 07:55 AM
To fix the issue you can do In PHP, it is not necessary to define variables or object properties before you use them. Use of an undefined variable will imply a value of null. This will result in a E_NOTICE level log message, or if you are trying to request a method on an undefined object it will be E_ERROR and execution will stop. However, it is very good practice to ensure variables are always defined in your code. In PHP, this means not first assigning them inside of conditional statements and, declaring object properties in the class definition.

Huffman Coding for Markov Chain based on conditional distribution
By : yuv
Date : March 29 2020, 07:55 AM
This might help you Well, having solved the problem above, I decided to post the answer with the octave script in case anyone needs it in future. So, lets assume that there are 5 states {1,2,3,4,5} and I also have the Transition probability Matrix (5x5). I Huffman encoded and decoded the Markov chain for 1000 Monte Carlo experiments. code :
%starting State of the chain
starting_value = 1;
%Chain Length
chain_length = 100;
%# of Monte Carlo experiments
MC=1000;
%Variable to count all correct coding/encoding experiments
count=0;
%Create unique symbols, and assign probabilities of occurrence to them.
symbols = 1:5;
p1 = [.5 .125 .125 .125 0.125];
p2 = [.25 .125 .0625 .0625 0.5];
p3 = [.25 .125 .125 .25 0.25];
p4 = [.125 0 .5 .25 0.125];
p5 = [0 .5 .25 .25 0];
%Create a Huffman dictionary based on the symbols and their probabilities.
dict1 = huffmandict(symbols,p1);
dict2 = huffmandict(symbols,p2);
dict3 = huffmandict(symbols,p3);
dict4 = huffmandict(symbols,p4);
dict5 = huffmandict(symbols,p5);
% Create the transition matrix for each state
T= [0.5 0.125 0.125 0.125 0.125;
0.25 0.125 0.0625 0.0625 0.5;
0.25 0.125 0.125 0.25 0.25;
0.125 0 0.5 0.25 0.125 ;
0 0.5 0.25 0.25 0];
%Initialize Marcov Chain
chain = zeros(1,chain_length);
chain(1)=starting_value;
for i=1 :MC
comp=[];
dsig=[];
%Create Markov Chain
for i=2:chain_length
this_step_distribution = T(chain(i1),:);
cumulative_distribution = cumsum(this_step_distribution);
r = rand();
chain(i) = find(cumulative_distribution>r,1);
end
comp=huffmanenco(chain(1),dict1);
%Encode the random symbols.
for i=2:chain_length
if chain(i1)==1
comp = horzcat(comp,huffmanenco(chain(i),dict1));
elseif chain(i1)==2
comp = horzcat(comp,huffmanenco(chain(i),dict2));
elseif chain(i1)==3
comp = horzcat(comp,huffmanenco(chain(i),dict3));
elseif chain(i1)==4
comp = horzcat(comp,huffmanenco(chain(i),dict4));
elseif chain(i1)==5
comp = horzcat(comp,huffmanenco(chain(i),dict5));
end
end
%Decode the data. Verify that the decoded data matches the original data.
dsig(1)=starting_value;
comp=comp(length(dict1{1,1})+1:end);
for i=2:chain_length
if dsig(end)==1
temp=huffmandeco(comp,dict1);
comp=comp(length(dict1(temp(1)){1,1})+1:end);
elseif dsig(end)==2
temp=huffmandeco(comp,dict2);
comp=comp(length(dict2(temp(1)){1,1})+1:end);
elseif dsig(end)==3
temp=huffmandeco(comp,dict3);
comp=comp(length(dict3(temp(1)){1,1})+1:end);
elseif dsig(end)==4
temp=huffmandeco(comp,dict4);
comp=comp(length(dict4(temp(1)){1,1})+1:end);
elseif dsig(end)==5
temp=huffmandeco(comp,dict5);
comp=comp(length(dict5(temp(1)){1,1})+1:end);
end
dsig=horzcat(dsig,temp(1));
end
count=count+isequal(chain,dsig);
end
count

How to chain if else elegantly
By : skanne
Date : March 29 2020, 07:55 AM
Any of those help I have an odd piece of code. It's smelly but I can't think of way to make if more clear. , Chain your conditions instead, and eliminate redundant terms.

How to (elegantly) interrupt Promises chain execution with Q
By : Wayne
Date : March 29 2020, 07:55 AM
will be helpful for those in need Since you are using modern node, here is how I would write it using Q.async:

