I have an Object MyObject which has a List of ThatObjects where each ThatObject is a list of ThoseObjects.
MyObject {
List<ThatObject> thatObjects;
}
ThatObject {
List<ThoseObject> thoseObjects
}
If I have a handle to MyObject, is it possible to get a handle to all thoseObjects in one list joined to together? Without have to iterate and make the joined list myself?
Thanks
Given:
class MyObject {
List thatObjects
}
class ThatObject {
List thoseObjects
}
We can make a test object of:
def o = new MyObject( thatObjects:[ new ThatObject( thoseObjects:[ 1, 2 ] ),
new ThatObject( thoseObjects:[ 3, 4 ] ) ] )
Then a simple walk through the properties gives us:
assert o.thatObjects.thoseObjects == [ [1, 2], [3, 4] ]
And call flatten to get a single list:
assert o.thatObjects.thoseObjects.flatten() == [ 1, 2, 3, 4 ]
Or, you could use collectMany
assert o.thatObjects.collectMany { it.thoseObjects } == [ 1, 2, 3, 4 ]
Related
Thank you aspok for your help!
My goal is to get my list to be [3, 3, 4] and then get a count of unique values within it. Can anyone point me in the right direction for doing this?
My script consumes a JSON and puts all F4211_LNID values into a list. [3.1, 3.9, 4]. I need to now round all decimal places down.
I'm not sure if it's doable, but I am trying to use Math.floor(intListItems) to round my array values down. When I try this I receive the following error: Exception No signature of method: static java.lang.Math.floor() is applicable for argument types: (ArrayList) values: [[3.1, 3.9, 4]] Possible solutions: floor(double), log(double), find(), macro(groovy.lang.Closure), acos(double), cos(double)
I see my simplified list in the error, but I can't get it to round down and not sure what the error means.
(UPDATED) My Working Groovy
// Read Input Values
String aInputJson = aInputMap.InputJson ?: "{}"
// Initialize Output Values
def intListItems = []
def uniqueCount = 0
// Parse JSON
def json = new JsonSlurper().parseText( aInputJson )
// Determine Row Numbers
def rowset = json?.fs_DATABROWSE_F4211?.data?.gridData?.rowset
intListItems = rowset.collect{ Math.floor(it.F4211_LNID) }
intListItems.unique()
uniqueCount = intListItems.size()
JSON I am using.
{
"fs_DATABROWSE_F4211": {
"title": "Data Browser - F4211 [Sales Order Detail File]",
"data": {
"gridData": {
"id": 58,
"fullGridId": "58",
"rowset": [
{
"F4211_LNTY": "S",
"F4211_CPNT": 0,
"F4211_MCU": " 114000",
"F4211_DSC2": "NAS133N3EK166",
"F4211_NXTR": "580",
"F4211_LNID": 3.1,
"F4211_DOCO": 2845436
},
{
"F4211_LNTY": "S",
"F4211_CPNT": 0,
"F4211_MCU": " 114000",
"F4211_DSC2": "NAS133N3EK166",
"F4211_NXTR": "580",
"F4211_LNID": 3.9,
"F4211_DOCO": 2845436
},
{
"F4211_LNTY": "S",
"F4211_CPNT": 0,
"F4211_MCU": " 114000",
"F4211_DSC2": "NAS133N3EK166",
"F4211_NXTR": "580",
"F4211_LNID": 4,
"F4211_DOCO": 2845436
}
],
"summary": {
"records": 1,
"moreRecords": false
}
}
},
"errors": [],
"warnings": []
},
"currentApp": "DATABROWSE_F4211",
"timeStamp": "2000-06-01:09.42.02",
"sysErrors": []
}
You are getting the error Exception No signature of method: static java.lang.Math.floor() is applicable for argument types: (ArrayList) because there is no version of Math.floor() that accepts a List as a parameter.
Instead, you need to call Math.floor() on each individual item in the list. The easiest way to do this is in the collect { } call you are already doing.
def flooredList = rowset.collect { Math.floor(it.F4211_LNID) }
assert flooredList == [3.0, 3.0, 4.0]
Below is the Input_dict for data analysis:
input_dict =
{
"C:\\arm64\\lib_apple.so": { "func-abc": [5,6,7,8], "func-123":[1,1,1,1] },
"C:\\arm64\\lib_banana.so": { "func-123": [2,3,4], "func-rt": [0,0] },
"C:\\armeabi\\lib_banana.so": { "func-123": [1,0,0], "func-rt": [1,5] },
"C:\\armeabi\\lib2.so": { "func-0": [1]},
"C:\\x86\\lib_apple.so": { "func-so": [5,6,7,8], "func-123": [2,2,1,1] },
"C:\\x86\\lib_banana.so": { "func-rt": [2,0] },
"C:\\x86\\lib2.so": { "func-0": [1,2,3]}
}
The aim is to compare the 'values' of functions with same name of different architectures(arm64,armeabi,x86).
In other words, I want to compare the "lists" of functions(with same name) in different libraries(.so) files.
For example: Comparing func-123: [2,3,4] with func-123: [1,0,0] from arm64\lib_banana.so and armeabi\lib_banana.so respectively.
One of the desired output could be:
{ lib_apple.so: { func-123: [arm64,[1,1,1,1]],[x86,[2,2,1,1]]}}
You can restructure your function data to order by their name first, then supported architectures. Afterwards, print out those functions that appear in multiple architectures:
from collections import defaultdict
from pathlib import PureWindowsPath
lib2func = {
r'C:\arm64\lib_apple.so': { 'func-abc': [5,6,7,8], 'func-123': [1,1,1,1] },
r'C:\arm64\lib_banana.so': { 'func-123': [2,3,4], 'func-rt': [0,0] },
r'C:\armeabi\lib_banana.so': { 'func-123': [1,0,0], 'func-rt': [1,5] },
r'C:\armeabi\lib.so': {},
r'C:\armeabi\lib2.so': { 'func-0': [1]},
r'C:\x86\lib_apple.so': { 'func-so': [5,6,7,8], 'func-123': [2,2,1,1] },
r'C:\x86\lib_banana.so': { 'func-rt': [2,0] },
r'C:\x86\lib2.so': { 'func-0': [1,2,3] },
}
# restructure
func2arch = defaultdict(dict)
for lib_path, functions in lib2func.items():
path = PureWindowsPath(lib_path)
lib = path.name
arch = path.parent.name
for func_name, func_val in functions.items():
func2arch[(lib, func_name)][arch] = func_val
# find functions defined for multiple architectures
for (lib, func_name), arch_data in func2arch.items():
if len(arch_data) < 2:
continue # skip functions that only appear once
print(lib, func_name, arch_data)
gives
lib_apple.so func-123 {'arm64': [1, 1, 1, 1], 'x86': [2, 2, 1, 1]}
lib_banana.so func-123 {'arm64': [2, 3, 4], 'armeabi': [1, 0, 0]}
lib_banana.so func-rt {'arm64': [0, 0], 'armeabi': [1, 5], 'x86': [2, 0]}
lib2.so func-0 {'armeabi': [1], 'x86': [1, 2, 3]}
The above code assumes that library/function name pairs are unique.
I am using the below function to get number of duplicated values in an array.But i want to get this result sorted descending order with respect to the values.
function countRequirementIds() {
const counts = {};
const sampleArray = RIDS;
sampleArray.forEach(function(x) { counts[x] = (counts[x] || 0) + 1; });
console.log(typeof counts); //object
return counts
}
Output:
{
"1": 4,
"2": 5,
"4": 1,
"13": 4
}
required output:
{
"2": 5,
"1": 4,
"13": 4,
"4": 1,
}
Javascript object keys are unordered as explained here: Does JavaScript guarantee object property order?
So sorting objects by keys is impossible. However if order is of a matter for you I would suggest using array of tuples:
const arrayOfTuples = [
[ "1", 4],
[ "2", 5],
[ "4", 1],
[ "13", 4],
]
arrayOfTuples.sort((a,b) => b[1] - a[1]);
console.log(arrayOfTuples);
// => [ [ '2', 5 ], [ '1', 4 ], [ '13', 4 ], [ '4', 1 ] ]
The sort command. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort Arrays of objects can be sorted by comparing the value of one of their properties.
I need to update (in bulk) many entities.
Each entity has a field that its value is an array.
I want to concat a whole array to the existed array in mongo.
For example:
Assume we have the field 'myField', and (its value) the array saved in mongo is: [4, 5, 6]
I want to concat the array [1, 2, 3] to this field, so the result:
myField: [1, 2, 3, 4, 5, 6]
I tried some options:
pushAll - but it is no longer available.
usePushEach: true, in options - not working, still get the same error:
"Unknown modifier: $pushAll. Expected a valid update modifier or pipeline-style update specified as an array"
I read about concat - but it is not looks compatiable.
Thanks in advance!
you can use $addToSet to add the values to existing array and avoid the duplicates like so,
[
{
id: 1,
values: [
1,
2,
3
]
}
]
db.collection.update({
id: 1
},
{
"$addToSet": {
values: {
"$each": [
5,
7,
1,
44
]
}
}
})
https://mongoplayground.net/p/S3HfWajg9r_
Given the list:
list: [
object1: {
id: 22,
name: "Tom"
},
object2: {
id: 12,
name: "Mary"
},
object3: {
id: 44,
name: "Tom"
}
]
Instead of using nested loops, is there any other one-liner that would get ONLY duplicated names from this list ? So the return list would be ["Tom"]
Another way would be to group them with a count, and find all the ones where the count is greater than one:
list.name.countBy { it }.findResults { it.value > 1 ? it.key : null }
Or indeed as #Daniel says:
list.values().name.countBy { it }.findResults { it.value > 1 ? it.key : null }
Depending on your structure...
Close to a oneliner is the below code. The idea is to create a list of unique named elements, and remove them from the original list.
def list = [
[ id: 22, name: "Tom"],
[ id: 12, name: "Mary"],
[ id: 44, name: "Tom"]
]
def unique = list.toUnique { a, b -> a.name <=> b.name }
list.removeAll(unique)
list*.name
removeAll returns booleon, so that complicates it if you need 1 line of code, the unique variable can be foldet into the method call.
Notice, .toUnique() returns a new collection .unique() removes from the collection called on.