Index of a table row when found by predicate - atata

I'm finding a TableRow with a predicate thus:
Table.Rows[r => r.name == "blablabla"]
Is there any way to get the index of the row it finds as an int?

IndexOf method should work for that:
int index = Table.Rows.IndexOf(r => r.name == "blablabla");
To assert index:
Table.Rows.IndexOf(r => r.name == "blablabla").Should.Be(2);

Related

ArangoDB insert edge doesn't exists

I want to create unique edge between docment collection C1 and C3.
The unique constraint is id and kid.
I use the flow aql to create it, but i get more than one edge in the same id and kid.
how can i achieve it?
sorry for my poor english:)
for i in C1
filter i.id != null and i.id != ''
let exist = first(
for c in C2
filter i.id == c.id and i.kid == c.kid
limit 1
return c
)
filter exist == null
let result = first(
for h in C3
filter i.kid == h.kid
limit 1
return h
)
insert{_from:i._id, _to:result._id, id:i.id, kid:i.kid} INTO C2
My English not very good too^)!
But I think, that i see where you made a mistakes.
First, for your two collection, you can use this code:
LET data = [
{"parent":{"ID":"YOU_MUST_WRITE_HERE_ID_C1"},"child":{"KID":"YOU_MUST_WRITE_HERE_KID_C3"}},
{"parent":{"ID":"YOU_MUST_WRITE_HERE_NEXT_ID_C1"},"child":{"KID":"YOU_MUST_WRITE_HERE_NEXT_KID_C3"}}
]
FOR rel in data
LET parentId = FIRST(
FOR c IN C1
FILTER c.GUID == rel.parent.ID
LIMIT 1
RETURN c._id
)
LET childId = FIRST(
FOR c IN C3
FILTER c.GUID == rel.child.KID
LIMIT 1
RETURN c._id
)
FILTER parentId != null AND childId != null
INSERT { _from: childId, _to: parentId } INTO C2
RETURN NEW
I hope that it help you.
Second - Why do you use the ะก2 collection in this fragment?
let exist = first(
for c in C2

How to use WITHIN with 2 geo indexes in one collection?

I have 2 geo indexes in the collection.
I need to find all the documents on the first geo-index and another time on another geo-index.
LET cFrom = (
FOR c IN WITHIN("city", 22.5455400, 114.0683000, 3000, "geofrom")
FOR r IN rcity
FILTER r._from == c._id && r.user == "5010403" && r.type == "freight-m"
LIMIT 1
RETURN r)
LET cTo = (
FOR c IN WITHIN("city", 55.7522200, 37.6155600, 3000, "geoTo")
FOR r IN rcity
FILTER r._to == c._id && r.user == "5010403" && r.type == "freight-r"
LIMIT 100
RETURN r)

Spark GraphX subgraph method generate null.

I use subgraph to filter the graph vertices.
However when I collect the vertices, some null value are in there.
I can guarantee that the original graph vertices contains no null value.
class A ....
val graph = .... // no null value contained
val selected_id:Set[Int] = SomeAlgorithm collect() toSet
val sub = graph.subgraph(vpred = (id,data) => data match {
case x:A => selected_id contains x.id
case _ => true
})
sub.vertices.map(_._2).collect() filter (_ == null) foreach println //null printed out

groovy Hashmap - get the value count from a map

my following code
def traineeDetails = session.traineeDetailsForAuto
on printing gives:
traineeDetails = [name:[Hus, Vin], email:[hus#gmail.com, vin#gmail.com], phone:[9908877654, 9987655432], jobTitle:[SE, ST]]
def count = traineeDetails.name.size() gives correct value =2
but when the map key contains one value
def traineeDetails = session.traineeDetailsForAuto
on printing gives:
traineeDetails = [name:Hus, email:hus#gmail.com, phone:9987766543, jobTitle:SE]
def count= traineeDetails.name.size() gives wrong answer 3 which is the total number of character in name
but here i need to get total count of value that the key name holds..
how to do it?
If you're going to mix types in a map, then you're going to need to check the type:
def count = traineeDetails.name.with { it instanceof Collection ? it.size() : 1 }
Using your examples, it works fine:
traineeDetails = [name:['Hus', 'Vin'], email:['hus#gmail.com', 'vin#gmail.com'], phone:['9908877654', '9987655432'], jobTitle:['SE', 'ST']]
count = traineeDetails.name.with { it instanceof Collection ? it.size() : 1 }
assert count == 2
traineeDetails = [name:'Hus', email:'hus#gmail.com', phone:'9987766543', jobTitle:'SE']
count = traineeDetails.name.with { it instanceof Collection ? it.size() : 1 }
assert count == 1
Can you provide actual non-working examples?

How can I translate this Groovy function to C#?

I would like to translate to C# the following Groovy code
def find_perfect_numbers(def number) {
(2..number).findAll { x-> (1..(x/2)).findAll{ x % it == 0 }.sum() == x }
}
which I got from here.
This is what I have, but it's not ready yet, doesn't compile either. I don't understand the groovy code good enough.
public List<int> find_perfect_numbers(int number)
{
List<int> lst = new List<int>();
lst = 2.To(number).FindAll(x => (1.To(x/2)).FindAll( x % it == 0).Sum() == x);
return lst;
}
I can't translate the part x % it == 0 (because "it" is an index).
I want the C# code to look as much like the groovy function as possible. Specifically, the line lst = 2.To( .....
I don't want to use a different solution to find perfect numbers (I have another working function already). For me this is only about the syntax, not about a good "perfect numbers function".
It's OK to create new (extension) functions that help doing this, just like the To function I used:
For the To function above I have used this StackOverflow function:
Generating sets of integers in C#
and changed it a little so that it returns a List of int instead of an array of int
public static class ListExtensions
{
public static List<int> To(this int start, int end)
{
return Enumerable.Range(start, end - start + 1).ToList();
}
}
Can anyone help me?
=== Update ===
This is what I have now, but it's not working yet, I get
DivideByZeroException was unhandled at the part s.value % s.idx == 0:
lst = 2.To(number).FindAll(x => ((1.To(x / 2)).Select((y, index) => new {value = y, idx = index}).Where( s => s.value % s.idx == 0).Sum(t => t.value) == (decimal)x));
I found it myself.
lst = 2.To(number)
.FindAll(x => ((1.To(x / 2))
.Select((y, index) => new {value = y, idx = index+1})
.Where( s => x % s.idx == 0)
.Sum(t => t.value) == (decimal)x));
Not as pretty as the Groovy one, but it works.

Resources