Created
September 27, 2011 20:07
-
-
Save pangloss/1246084 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# setup: | |
def setup_graph | |
g = Pacer.tg | |
person = g.create_vertex type: 'person' | |
print '.' | |
1000.times { g.create_vertex type: 'person' } | |
print '.' | |
g.v.each { |v| v.add_edges_to :friend, g.v.random(rand(100)) } | |
print '.' | |
1000.times { g.create_vertex :type => 'product' } | |
print '.' | |
g.v(type: 'person').each { |person| g.v(type: 'product').random(rand(100)).each { |prod| prod.add_edges_to :rated, person, weight: rand(5) } } | |
puts '.' | |
[g, person] | |
end | |
groups = person.in(:rated).out(:rated).is_not(person).uniq.group.values_route(:default) { |friend| friend.in_e(:rated).lookahead { |e| e.out_v.out(:rated).is(person) }[:weight] } | |
# this is a special version of reduce, specifically for group routes: | |
groups.reduce(0, :values) { |r, v| r + v } | |
# or you can turn the route into a regular array of results, but then you have to use regular enumerable methods: | |
groups.to_a.each { |g| g.set_values(:sum, g.values.sum) }.sort_by { |g| -g.values(:sum) }.map { |g| [g.key[:name], g.values(:sum) } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment