Click here to Skip to main content
15,216,523 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
GeneralRe: having some fun looking into graph databases I Pin
BillWoodruff10-Feb-20 0:09
mveBillWoodruff10-Feb-20 0:09 
GeneralRe: having some fun looking into graph databases I Pin
phil.o10-Feb-20 0:13
mvephil.o10-Feb-20 0:13 
GeneralRe: having some fun looking into graph databases I Pin
F-ES Sitecore9-Feb-20 23:01
mveF-ES Sitecore9-Feb-20 23:01 
GeneralRe: having some fun looking into graph databases I Pin
Jon McKee10-Feb-20 6:29
professionalJon McKee10-Feb-20 6:29 
GeneralRe: having some fun looking into graph databases I Pin
BillWoodruff10-Feb-20 11:36
mveBillWoodruff10-Feb-20 11:36 
GeneralRe: having some fun looking into graph databases I Pin
Jon McKee10-Feb-20 13:25
professionalJon McKee10-Feb-20 13:25 
GeneralRe: having some fun looking into graph databases I Pin
BillWoodruff10-Feb-20 15:25
mveBillWoodruff10-Feb-20 15:25 
GeneralRe: having some fun looking into graph databases I Pin
Richard Deeming10-Feb-20 9:53
mveRichard Deeming10-Feb-20 9:53 
If you start with Jim, your C# will throw an exception. Smile | :)

You could try:
var friendsOfFriendsofJoe = friendsOfJoe
    .SelectMany(name => friendsByPerson.TryGetValue(name, out var myFriends) ? myFriends : Enumerable.Empty<string>())
    .Except(friendsOfJoe)
    .Where(name => name != "Joe");
Or you could go for the (only slightly brain-melting) functional option:
static class GraphExtensions
{
    public static IReadOnlyDictionary<string, IEnumerable<string>> FriendsByPerson(this IEnumerable<(string n1, string n2)> source)
    {
        return source.GroupBy(p => p.n1).ToDictionary(g => g.Key, g => g.Select(p => p.n2));
    }
    
    public static Func<string, IEnumerable<string>> FriendsOfFriends(this IReadOnlyDictionary<string, IEnumerable<string>> friendsByPerson)
    {
        return name =>
        {
            if (!friendsByPerson.TryGetValue(name, out var friends)) 
            {
                return Enumerable.Empty<string>();
            }
            
            return friends
                .SelectMany(f => friendsByPerson.TryGetValue(f, out var foaf) ? foaf : Enumerable.Empty<string>())
                .Except(friends)
                .Where(f => f != name);
        };
    }
}

...

Func<string, IEnumerable<string>> foafByPerson = people.FriendsByPerson().FriendsOfFriends();
foreach (string foaf in foafByPerson("Anna"))
{
    Console.WriteLine(foaf);
}
Obviously this assumes the connections are one-way - Jim knows Mike, but Mike doesn't know Jim. If Cypher / Neo4J assumes bi-directional connections, the code would get slightly more complicated:
public static IReadOnlyDictionary<string, IEnumerable<string>> FriendsByPerson(this IEnumerable<(string n1, string n2)> source)
{
    return source
        .Concat(source.Select(p => (n1: p.n2, n2: p.n1)))
        .GroupBy(p => p.n1)
        .ToDictionary(group => group.Key, group => group.Select(p => p.n2));
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: having some fun looking into graph databases I Pin
BillWoodruff10-Feb-20 11:39
mveBillWoodruff10-Feb-20 11:39 
GeneralI think the game designers should bear some responsibility here as well... Pin
OriginalGriff9-Feb-20 8:30
mveOriginalGriff9-Feb-20 8:30 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Jörgen Andersson9-Feb-20 8:44
professionalJörgen Andersson9-Feb-20 8:44 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
OriginalGriff9-Feb-20 9:00
mveOriginalGriff9-Feb-20 9:00 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Jörgen Andersson9-Feb-20 9:46
professionalJörgen Andersson9-Feb-20 9:46 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
OriginalGriff9-Feb-20 10:00
mveOriginalGriff9-Feb-20 10:00 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Jörgen Andersson9-Feb-20 10:07
professionalJörgen Andersson9-Feb-20 10:07 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
OriginalGriff9-Feb-20 10:16
mveOriginalGriff9-Feb-20 10:16 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Mark_Wallace9-Feb-20 10:19
MemberMark_Wallace9-Feb-20 10:19 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Jörgen Andersson9-Feb-20 20:20
professionalJörgen Andersson9-Feb-20 20:20 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Jörgen Andersson10-Feb-20 1:06
professionalJörgen Andersson10-Feb-20 1:06 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Mark_Wallace10-Feb-20 6:28
MemberMark_Wallace10-Feb-20 6:28 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Richard MacCutchan9-Feb-20 21:23
mveRichard MacCutchan9-Feb-20 21:23 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Gerry Schmitz9-Feb-20 9:15
mveGerry Schmitz9-Feb-20 9:15 
RantRe: I think the game designers should bear some responsibility here as well... Pin
Marc Clifton9-Feb-20 10:36
mvaMarc Clifton9-Feb-20 10:36 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
Sander Rossel9-Feb-20 21:46
professionalSander Rossel9-Feb-20 21:46 
GeneralRe: I think the game designers should bear some responsibility here as well... Pin
dandy7210-Feb-20 3:51
Memberdandy7210-Feb-20 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.