I'm expecting arrayTester to look at each array within the array and if both elements are empty strings for them to be removed from the array. Instead I am getting back the original array.
Expected result = [[], ['string', '']]
Actual result = [['',''], ['string', '']]
Any ideas?
let data = [['',''], ['string', '']]
const emptyString = R.equals('')
const removeEmptyElementsFunc = R.reject(R.isEmpty)
const arrayTester = (data) => {
R.forEach(
R.when(
R.all(emptyString),
R.map(removeEmptyElementsFunc)
)(data))
return data
}
arrayTester(data)
If you need to map an array of empty strings to an empty array, the only thing you need to do is to make sure that it is indeed only made of empty strings. (The "transformation" is rather easy: just return an empty array.)
all(isEmpty) will return true if an array is made of empty stuff. always([]) will always return an empty array.
const arrayTester = map(when(all(isEmpty), always([])));
console.log(
arrayTester([['', ''], ['a', 'b', '']]),
arrayTester([['', '', ''], ['a', 'b', '']]),
arrayTester([['', 'x', ''], ['a', 'b', '']])
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
<script>const {map, when, all, isEmpty, always} = R;</script>
Related
In ES6 you can do
const var1 = 'variableName';
const object = {
[var1] : 'value'
}
How to use a variable for the key in a map just like above, in Groovy?
You use brackets round the key to inform groovy it's a variable
def object = [
(var1): 'value'
]
In groovy you can do the same as:
def var1 = 'variableName'
def object = [
"$var1":'value'
]
Can anyone help me how to do this query:
var x = db.Home.Where(b => b.HomeID == id).Select(b => b.Longitude && b.Latitude).ToList();
I want to select Latitude and Longitude values.
This will select the columns and will return anonymous type.
.Select(b => new { Longitude = b.Longitude, Latitude = b.Latitude })
InfowareEntities Infoware = new InfowareEntities();
IEnumerable admn = Infoware.Admissions.Where(x => x.AdmissionNo == 1520002);
In my admission database table I have columns- admissionNo, firstname, lastname...
now from above lambda expression I need to get firstname.
Please help me with this.
Try chaining a select like this
.Where(x => x.AdmissionNo == 15200).select (n => n.FirstName)
to get a list of names.
I believe you need this,
InfowareEntities Infoware = new InfowareEntities();
IEnumerable admn = Infoware.Admissions.Where(x => x.AdmissionNo == 1520002);
var arrayOfFirstNames = admn.select(n => n.FirstName).ToArray();
You can also use "ToList();" if you want list of first names..
I have a list as below
def qresultList = [
[location: 'a', txs: 10],
[location: 'b', txs: 20],
[location: 'a', txs: 30]
]
I want to get distinct list of locations with sum of txs for same location.. so I am doing groupby on location like this:
def totalsByLocation1 = qresultList.groupBy{ it.location }.
collectEntries{ key, vals -> [key, vals*.txs.sum()] }
The above code is inside SummaryUtilsService/getWorldSummary function
I am getting the following error
No signature of method: java.util.LinkedHashMap.collectEntries() is applicable for argument types: (summary.SummaryUtilsService$_getWorldSummary_closure3) values: [summary.SummaryUtilsService$_getWorldSummary_closure3#2750e6c9]
Update: the actual result from the query is
def qresultList = [
['a', 10],
['b', 20],
['a', 30]
]
so Its a list of lists..
From earlier questions, I assume you're using Grails 1.3.7 or something
The pre-groovy 1.8.X way of doing this is:
def totalsByLocation1 = qresultList.groupBy{ it.location }.inject([:]) { map, val ->
map << [ (val.key): val.value*.txs.sum() ]
}
Edit
If your input list is:
def qresultList = [
['a', 10],
['b', 20],
['a', 30]
]
Then you will need something like:
qresultList.groupBy { it[ 0 ] }.collectEntries { k, v ->
[ (k): v*.getAt( 1 ).sum() ]
}
For a list of maps:
assert [[a:40], [b:20]] == qresultList.groupBy {it.location}.collect {[(it.key): it.value.sum{it.txs}]}
For a map:
def locationTransactionSums = [:]
qresultList.groupBy {it.location}.each {
locationTransactionSums.(it.key) = it.value.sum{it.txs}
}
assert [a:40, b:20] == locationTransactionSums
I'm trying to have a good access to multi-dimensional arrays with string indexes in Lua, here's basically what I'm trying to do:
rules =
{
{"S_RIGHT", "A_STOP", "S_RESULT"},
}
matrix = {}
for _,v in pairs(rules) do
if( matrix[ v[1] ] == nil ) then
matrix[ v[1] ] = {}
end
matrix[ v[1] ][ v[2] ] = v[3]
end
-- results in error ( attempt to index field 'S_NO' a nil value)
var = matrix["S_NO"]["S_RESULT"]
assert(var == nil, "Var should be nil")
A way to do it but quite verbose is:
var = matrix["S_NO"]
if var ~= nil then
var = var["S_RESULT"]
end
assert(var == nil, "Var should be nil")
Is there a way to make the first case to work ? ( less verbose )
Ok,
Found the answer.
If matrix is going to be read-only a correct approach would be:
local empty = {}
setmetatable(matrix, {__index=function() return empty end})
If I would like to allow writes and it's specifically two levels of tables, I could do:
setmetatable(matrix, {__index=function(t,k) local new={} rawset(t,k,new) return new end}
Hope this helps!