Declaring arrays in soy file - google-closure-compiler

I am pretty new to Google closures. I am not sure how to declare and assign values to arrays in Soy file (google Closure).
Please advise.

just moving this to an answer for future reference:
this compiles for me: {let $P : [1,2,3,4]/} -- compiles down to this: var output = opt_sb || new soy.StringBuilder(); var P__soy461 = [1, 2, 3, 4]; so it should work fine

Related

Exception: Bad value (line 9, file "Code")

I want to read my data from my firebase and write it in google sheet. When i run this Apps Script I got the error
Exception: Bad value (line 9, file "Code")
var ss= SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var range=sheet.getRange(1,1,4,2);
var data= getFirebaseData("Entries");
Logger.log(data)
range.setValues(JSON.parse(data))
}
function getFirebaseData(data){
var firebaseUrl = "**";
var secret = "**";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl,secret);
var result = base.getData(data);
return result;
}
the data in Firebase is like this:
Entries
date:"09/09/2020"
docAmount:88
docNo:55
partyName: "ffg"
Solutions that I might accept,(solve this issue)or (show a better way to do the process)
You want to write an object's (data) key/value pairs to two columns in a sheet. setValues accepts a 2D array, not an object, so you should first transform data to a 2D array:
const array = Object.keys(data).map(key => [key, data[key]]);
sheet.getRange(1, 1, array.length, array[0].length).setValues(array);
Note:
Making the range dimensions dependent on the array dimensions (array.length, array[0].length) will give you more flexibility (what if the object has 5 properties instead of 4?).
This will only work for simple objects, not if they have nested properties.
Reference:
setValues(values)
Object.keys()
getData(path, optQueryParameters)

Java 7 => Java 8 : for to foreach on a Map with calculation

I begin with Java 8 and i have a migration project. I have read a lot of documentation and tutorial to use foreach or streams but i have a little last problem. I don't find the answer, just tutorial easy example.
I'm trying to transform this loop :
for ( Map.Entry<Neuron, Double> entry: this.entries.entrySet() ) {
value += entry.getKey().getExitValue() * entry.getValue();
}
This solution doesn't match and i know why (anonymous class => final/local var)
this.entries.forEach( (neuron, weight) -> {
value += neuron.getExitValue() * weight;
});
But only with a foreach i don't know how do this simple operation.
I think it's very easy but...
I have try with stream but i have similar problems.
Double sum = entries.entrySet()
.stream()
.forEach( entry-> { ? } );
Thanks you in advance.
As #Holger said in the comments above - in this case it is better to use mapToDoble. However there is still a way to do it using forEach loop. Please note that it is an ugly, dirty trick and it is just for demonstration purposes and it shouldn't be used in production code. As we know only final or effectively final variables can be used with lambda expressions, that's why value += is an illegal expression. Java-8 added a few new classes to java.util.concurrent.atomic one of them is DoubleAdder. You can use it with lambda:
DoubleAdder adder = new DoubleAdder();
stream.forEach(e -> adder.add(e.getKey().getExitValue() * e.getValue()));
System.out.println(adder.sum());
I don't see any cases when this should be used instead of mapToDouble
I introduced a list to stall the values and then do calculation with list.
final List<BigDecimal> valuesList = new ArrayList<>();
otherList.stream().forEach(val-> valuesList.add(map.get(val)));
final BigDecimal lastValue = valuesList.stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);

Metalkit: MTLBuffer and pointers in swift 3

I started with Metalkit and I have a very simple kernel as a test case.
kernel void compute(device float* outData [[ buffer(0) ]])
{
outData[0] = 234.5;
outData[3] = 345.6;
}
This "computed" data is stored in a MTLBuffer.
var buffer : MTLBuffer?
...
buffer = device.makeBuffer(length: MemoryLayout<Float>.size * 5, options: [])
...
commandBuffer.waitUntilCompleted()
At this point the kernel has written some test data to the MTLBuffer.
Question is how I should access that data from my main program?
I get a unsafeMutableRawPointer from buffer.contents(). How do I get a swift array of values that I can use everywhere else (displaying on screen, writing to file,...)?
These snippets work in this very simple app, but I am not sure if they are correct:
let raw = buffer.contents()
let b = raw.bindMemory(to: Float.self, capacity: 5)
print(b.advanced(by: 3).pointee)
let a = raw.assumingMemoryBound(to: Float.self)
print(a.advanced(by: 3).pointee)
let bufferPointer = UnsafeBufferPointer(start: b, count: 5)
let values = Array(bufferPointer)
print(values)
let value = raw.load(fromByteOffset: MemoryLayout<Float>.size * 3, as: Float.self)
print(value)
Both bindMemory and assumingMemoryBound work. Though assumingMemoryBound assumes the underlying bytes are already typed and bindMemory doesn't. I think that one of either should work, but not both. Which one should it be and why?
I use the code presented below to load to arrays, but I can't decide if mine or your version is best.
let count = 16
var array = [Float]()
array.reserveCapacity(count)
for i in 0..<count {
array.append(buffer.contents().load(fromByteOffset: MemoryLayout<Float>.size * i, as: Float.self))
}

Equivalent IN operator in entity framework

var query = from c in context.Albums
where c.AlbumID in albumIds
select c.Albumname;
Here albumIds is IENUM<> of some custom type(in my case its an Entity)
When I do the above query I get an error
Operator == cannot be applied to type int and IEnumerable
This error is acceptable but how do I overcome this
Turn it around slightly - you want to check whether your enumerable of album ids contains the id of an album you have just iterated to in your linq. So something like this ...
albumIds = {1,2,13,25,277,567};
var query = context.Albums.Where(x=> albumIds.Contains(x.ID));
(Sorry, writing example code without a tool in front of me so forgive any obvious mistakes. Hopefully you can get the idea from this though).
var albumIds= new string[] { "900", "801", "802", "803","906" };
var lstData = context.tbl.Where(
x => (x.TimeCreated >= yesterday && x.TimeCreated < today) &&
albumIds.Contains(x.TransactionSetId)
).Select(x => x.X12_Interchange).ToList();

Changes to one variable propagates to another

For example I have two ArrayCollection's - firstAC and secondAC. If I do secondAC = firstAC, and than I make changes to secondAC (prehaps put a filterfunction on it) it somehow propagates to firstAC, would anyone tell me why that happens in Flex or Actionscript 3?
What can I do if I only want secondAC to get all data from firstAC but then when I make changes to secondAC it does not show in firstAC?
Thanxs a bunch for answers!
Ladislav
When you write secondAC = firstAC, you simply state that secondAC and firstAC are references to the same array collection.
What you want is to clone the first collection (as in, copy all elements one by one).
You should be able to do it with something like :
secondAC = new ArrayCollection();
secondAC.addAll(firstAC);
I have no idea of Flex or Actionscript, but looks like firstAC and secondAC point to the same array, therefore that's expected.
What you should do is just create another array, copy members, and they will be two real different entities.
Instead of secondAC = firstAC, you can try secondAC.addAll(firstAC).
In ECMAScript languages (AS1-3, JavaScript, et al.), when you use
var foo = //some value which is not a String or a Number
what you are really saying is "foo now points to the same object as that other variable." This means that in this situation, both arrays will be the same value:
var foo:Array = [ 1, 2, 3 ];
foo = bar;
bar.push( 4 );
trace( foo ); // 1, 2, 3, 4
This also works for functions:
var foo:Array = [ 1, 2, 3 ];
adder( foo );
function adder( bar:Array ):void {
bar.push( 4 );
}
trace( foo ); // 1, 2, 3, 4
and it even works with XML:
var xml:XML = <root><foo/></root>;
var bar:XML = xml;
bar.children()[ 0 ].#bar = 1;
trace( xml.toXMLString() ); // <root><foo bar="1"/></root>
This is called "passing by reference" instead of "passing by value" or "passing by copy". It means that every time that an item is referenced, each variable will point to the same object.
There are many ways to get around this, and most of them depend on your context. For arrays, my favorite is Array.concat(), which returns a literal clone of the array. This means that anything I do to the returned value will not effect the original in any way. If I'm dealing with XML, however, I will do something like: var xml2:XML = XML( xml.toXMLString() );.
In your case, I would actually recommend that you use:
var secondAC:ArrayCollection = new ArrayCollection( firstAC.source.concat() );
This has the major benefits of not only being faster (it relies on compiled code instead of Flex SDK code and it also does not first instantiate a new array and then re-populate it), but it also has the distinct benefit of being available in older versions of Flex 3's SDK -- it is entirely backwards compatible.

Resources