cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Pause, resolving how to handle anonymous content

How to add another variable in function Reduce

hello, I have created a little traveling line for the bus like following

I want to get the shortest time path between "start" and "fin" with the function reduce, I hope to calculate the sum of running time(temps), and when it changes the bus, we add the time of Changement(att),temps and att are parameters in relationships.

the following is the code I want to realize

match (a:station {name:"start"}),(b:station{name:"fin"}),
p=((a)-[*]->(b))
with p,
reduce(s=0,r in relationships(p) #and ln=type(relationship(p))# (I know it's incorrect| case type(r) when ln then s+r.temps else s+r.temps+r.att and ln=type(r) end)AS temps
return p, temps order by temps asc limit 1

Is it possible to add another variable like ln in function reduce to realize it or is there another solution?
Thank you

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Hello @zhiyizhou1995 and welcome to the Neo4j community

You can try to to use a list as accumulator: reduce(s=[0, 0]...) and then to add s[0] + 1 or s[1] + 1.

Regards,
Cobra

View solution in original post

4 REPLIES 4

Cobra
Ninja
Ninja

Hello @zhiyizhou1995 and welcome to the Neo4j community

You can try to to use a list as accumulator: reduce(s=[0, 0]...) and then to add s[0] + 1 or s[1] + 1.

Regards,
Cobra

thank you! You mean s[0], s[1] here represent different variables? it can also be a string?

Cobra
Ninja
Ninja

Take a list l = [0, 0], l[0] will be the first element and l[1] will be the second element so you can add integer to the first or the second element, you can also use a list of strings I guess if you want to merge strings like l = ['', ''].

@Cobra Thanks a lot!