Last active
November 15, 2019 19:18
-
-
Save bolshakov/94f7b80116f37d9e5fe0ae91b31cc527 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
def <=>(other) | |
head.match do |lm| | |
lm.none do | |
other.head.match do |rm| | |
rm.some { -1 } | |
rm.none { 0 } | |
end | |
end | |
lm.some do |left_head| | |
other.head.match do |rm| | |
rm.none { +1 } | |
rm.some do |right_head| | |
(left_head <=> right_head).yield_self do |comparison| | |
if comparison == 0 | |
tail <=> other.tail | |
else | |
comparison | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
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
def <=>(other) | |
left = self | |
right = other | |
until left.empty? || right.empty? | |
comparison = left.head! <=> right.head! | |
if comparison == 0 | |
left = left.tail | |
right = right.tail | |
else | |
return comparison | |
end | |
end | |
case [left.empty?, right.empty?] | |
when [true, true] then 0 | |
when [true, false] then -1 | |
when [false, true] then +1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment