code generation with Xtend - xtend

I am implementing my own DSL and using Xtend to generate C code. I have developed small prototype using XPand/Xtend but now I need to convert prototype into XTend.
In Xpand, I used to define "Generate" function to generate C file but I don't know how can I do similar thing using XTend. Any help in this regard would be highly appreciated.
«DEFINE Generate FOR dsl::defs::module»
«FILE "Com.c" -»
/* DATE «dateString()» */
«EXPAND COM_C::COM_Def_C »
«ENDFILE»
«ENDDEFINE»
Thanks and Regards,
Hemal Bavishi

You could do something like this:
def xtendFunc() '''
/* DATE «dateString()» */
'''
or
def xtendFunc() {
var = '''/* DATE «dateString()» */'''
return var
}
(Assuming the 'dateString()' function is in the same class)
This in Xtend is called using template expressions(Enclosed within three quotes '''...'''). You can contain the result of the function in a val (final variable in Xtend) for the first case. Then use another function for the other template COM_C:COM_Def_C. Append the result into a variable and write into a file using simple java.

Related

I want to find a hash function to generate a hash with a given length

Just like the title, I want to find a hash function to generate a hash with a given length. From the Internet I found one solution is I can use the SHAKE256 technique. But I cannot find some java code to use the SHAKE256. Anyone can provide me some sample java code so that I can use it in my code?
I typed java shake256 into Google and found this solution as the 2nd result: https://github.com/aelstad/keccakj. It is a Java library called "keccakj" that is able to compute shake256 hashes, among others.
For shake256, to generate a hash of an arbitrary bit length d and d < 256, you can just trim the result of SHAKEDigest from bouncycastle.
public static byte[] hash(byte[] src, int byteLength){
//import org.bouncycastle.crypto.digests.SHAKEDigest;
Assert.assertTrue("do not support result length more than 32", byteLength<=32);
SHAKEDigest d = new SHAKEDigest(256);
byte[]checksumByte = new byte[32];
d.update(src,0,src.length);
d.doFinal(checksumByte,0);
return Arrays.copyOf(checksumByte,byteLength);
}

Error in mcp2matrix(model, linfct = linfct)

I don't understand why it is not working for the post hoc test. What did I do wrong?
modmisto<-lme(Cobertura~Tratamento, random=~1|Parcela, data=Cover_BraquiT3)
summary(modmisto)
tukey<-glht(modmisto, mcp(Tratamento="Tukey"))
Error in mcp2matrix(model, linfct = linfct) :
Variable(s) ‘Tratamento’ of class ‘character’ is/are not contained as a factor in ‘model’.
Any help with this will be very appreciated!
Tratatmento does not seem to be a factor variable, try put this before:
Cover_BraquiT3$Tratamento = as.factor(Cover_BraquiT3$Tratamento)
A variable in my data.frame was of type character.
The glht function did not recognize it as a factor in the model generated by the glm function.
I Tried:
variable = as.factor (variable)
I only managed using:
library (tibble)
data <-as_tibble (data)%>%
mutate (variable = factor (variable))

Grooviest way to clean malformed json in memory

Working with some json files, ran into some malformed ones with c style comments included. Assume I don't have ownership of these files, and changing them is not an option, I need to analyze the json data in an automated fashion. JsonSlurper dies when it sees these comments, so I wrote a method to remove the offending lines:
def filterComments(String raw){
def filtered = ""
raw.eachLine { line ->
def tl = line.trim()
if(!(tl.startsWith("//") || tl.startsWith("/**") || tl.startsWith("*"))){
filtered += line;}}
return filtered;
}
I really like Groovy and have turned to it as my maintenance tool of choice, but am not the "Grooviest" developer, this is an example. I would like a more Groovy way of accomplishing this.
Some additional notes: this is run as a script. If there is a way to make JsonSlurper disregard the comments instead of using this utility method, that solution would be considered more valuable. Thanks in advance!
Here's one way to do it:
def json = '''
// A comment
Foo f = new Foo(); // this is a comment
/*
* Multiline comment
*
*/
'''
def filterComments(str) {
str?.replaceAll(/(\/\/|\/\*|\*).*\n?/, '')?.trim()
}
assert filterComments(json) == 'Foo f = new Foo();'
This will remove any line that begins with /* or *, as well as anything after //.
My take:
def filterComments(str){
str.readLines().findAll{ !(it ==~ /^\s*(\*|\/\*\*|\/\/).*/) }.join('\n')
}

MATLAB: Is there a method to better organize functions for experiments?

I will run a set of experiments. The main method evaluated has the following signature:
[Model threshold] = detect(...
TrainNeg, TrainPos, nf, nT, factors, ...
removeEachStage, applyEstEachStage, removeFeatures);
where removeEachStage, applyEstEachStage, and removeFeatures are booleans. You can see that if I reverse the order of any of these boolean parameters I may get wrong results.
Is there a method in MATLAB that allows better organization in order to minimize this kind of error? Or is there any tool I can use to protect me against these errors?
Organization with a struct
You could input a struct that has these parameters as it's fields.
For example a structure with fields
setts.TrainNeg
.TrainPos
.nf
.nT
.factors
.removeEachStage
.applyEstEachStage
.removeFeatures
That way when you set the fields it is clear what the field is, unlike a function call where you have to remember the order of the parameters.
Then your function call becomes
[Model threshold] = detect(setts);
and your function definition would be something like
function [model, threshold] = detect(setts)
Then simply replace the occurrences of e.g. param with setts.param.
Mixed approach
You can also mix this approach with your current one if you prefer, e.g.
[Model threshold] = detect(in1, in2, setts);
if you wanted to still explicitly include in1 and in2, and bundle the rest into setts.
OOP approach
Another option is to turn detect into a class. The benefit to this is that a detect object would then have member variables with fixed names, as opposed to structs where if you make a typo when setting a field you just create a new field with the misspelled name.
For example
classdef detect()
properties
TrainNeg = [];
TrainPos = [];
nf = [];
nT = [];
factors = [];
removeEachStage = [];
applyEstEachStage = [];
removeFeatures =[];
end
methods
function run(self)
% Put the old detect code in here, use e.g. self.TrainNeg to access member variables (aka properties)
end
end

Zend Framework dom problem

I want to get website shortcut icon(favicon) and stylesheet path with zend_dom query
$dom = new Zend_Dom_Query($html);
$stylesheet = $dom->query('link[rel="stylesheet"]');
$shortcut = $dom->query('link[rel="shortcut icon"]');
Stylesheet query is work but shortcut icon query not work. How i do?
Thanks.
This appears to be an issue with Zend's css style query implementation. In Zend/Dom/Query.php, the query function calls a conversion function to convert the query into proper xpath format:
public function query($query)
{
$xpathQuery = Zend_Dom_Query_Css2Xpath::transform($query);
return $this->queryXpath($xpathQuery, $query);
}
However within the transform() method, they seem to be using some pretty basic regex to split up the string by spaces:
$segments = preg_split('/\s+/', $path);
Which basically means your link[rel="shortcut icon"] query now becomes two queries: link[rel="shortcut and icon"]
To get around this, you can use the method Zend_Dom_Query::queryXpath() and provide it with a proper xPath query. Like this:
$dom->queryXpath('//link[#rel="shortcut icon"]');