Skip to content

Instantly share code, notes, and snippets.

@bolshakov
Last active November 15, 2019 19:18
Show Gist options
  • Save bolshakov/94f7b80116f37d9e5fe0ae91b31cc527 to your computer and use it in GitHub Desktop.
Save bolshakov/94f7b80116f37d9e5fe0ae91b31cc527 to your computer and use it in GitHub Desktop.
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
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