9

Given two objects is there a simple way in Raku to find the nearest common ancestor in their tree of inheritence?

There are already some general answers for this:

How to find nearest common ancestor class of two objects?

Algorithm to find common ancestor of two nodes given

I was wondering if there's an idiomatic solution built-in to Raku already.

Joseph Brenner
  • 664
  • 3
  • 14

1 Answers1

8
  class A {}
  class B is A {}
  class C is B {}
  class D is B {}
  class E is D {}

  say E.^parents.first: * === D.^parents.any
wamba
  • 3,883
  • 15
  • 21
  • That's a good start, though in that example I would've said the "least common ancestor" would be D, even though you could argue that D is not technically an ancestor of itself: You can treat an E like a D and you can treat a D like a D, you don't have to treat both like a B. Perhaps something like this: say E.^parents.map({ .gist }).first({ $_ === D.^mro.map({ .gist }).any }); # (D) – Joseph Brenner Aug 21 '21 at 17:54
  • 1
    @JosephBrenner `sub ancestors2 (\class) { class, |class.^parents }; say E.&ancestors2.first: * === D.&ancestors2.any; # (D)`. – raiph Aug 22 '21 at 00:18