Skip to content

Add Pair-based edges initializer to Graph types #198

Open

Description

@bwetherfield

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
        ]
    )

Activity

  1. jsbean commented on Nov 3, 2018

    @jsbean
    Member

    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) 
        {
            ...
        }
    }
  2. jsbean commented on Nov 3, 2018

    @jsbean
    Member

    See DictionaryLiteral and KeyValuePairs.

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

  3. jsbean commented on Nov 3, 2018

    @jsbean
    Member

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

    Give it a try. No guarantees here.

  4. bwetherfield commented on Nov 3, 2018

    @bwetherfield
    MemberAuthor

    Oh! Totally forgot about Hashability!

  5. jsbean commented on Nov 3, 2018

    @jsbean
    Member

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

  6. jsbean commented on Nov 3, 2018

    @jsbean
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions