Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pair-based edges initializer to Graph types #198

Open
bwetherfield opened this issue Nov 3, 2018 · 6 comments
Open

Add Pair-based edges initializer to Graph types #198

bwetherfield opened this issue Nov 3, 2018 · 6 comments

Comments

@bwetherfield
Copy link
Member

Like so, so that the edge type is constructed internally...

WeightedGraph<Tendency,Int>(
        [
            .up,
            .down
        ],
        [
           (.up,.up): 1,
           (.down,.down): 1
        ]
    )

at the moment we have to do:

WeightedGraph<Tendency,Int>(
        [
            .up,
            .down
        ],
        [
            UnorderedPair(.up,.up): 1,
            UnorderedPair(.down, .down): 1
        ]
    )
@jsbean
Copy link
Member

jsbean commented Nov 3, 2018

I think this would require an initializer like so:

extension WeightedGraph where Node == Tendency, Weight == Int {
    init <D> (_ nodes: Set<Tendency>, _ dictionaryLiteral: D) where D: DictionaryLiteral, where D.Element == (Tendency,Int) 
    {
        ...
    }
}

@jsbean
Copy link
Member

jsbean commented Nov 3, 2018

See DictionaryLiteral and KeyValuePairs.

You can't actually pass a real dictionary in, because tuples are not Hashable 🤦‍♂️ .

@jsbean
Copy link
Member

jsbean commented Nov 3, 2018

But if this is for syntax sugar, then you can use the DictionaryLiteral syntax.

Give it a try. No guarantees here.

@bwetherfield
Copy link
Member Author

Oh! Totally forgot about Hashability!

@jsbean
Copy link
Member

jsbean commented Nov 3, 2018

But KeyValuePairs doesn't actually require Hashable conformance :o, I am pretty sure.

@jsbean
Copy link
Member

jsbean commented Nov 3, 2018

You would need to do something like:

extension WeightedGraph {
    init (_ nodes: Set<Tendency>, _ keyValuesPairs: KeyValuePairs<Node,Weight>) {
        self.init(nodes, Dictionary(keyValuePairs.lazy.map { k,v in (UnorderedPair(k.0, k.1), v) })
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants