Created
August 11, 2012 17:24
-
-
Save Pet3ris/3325852 to your computer and use it in GitHub Desktop.
Comparison chaining for a blog post
This file contains 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
import Data.Monoid | |
chain :: (Ord a) => [a] -> [a] -> Ordering | |
chain xs = mconcat . (zipWith compare xs) | |
chain [1, 2, 3] [1, 2, -1] | |
-- GT | |
chain [1, 2, 3] [1, 2, 3] | |
-- EQ | |
chain [1, 2, 3] [1, 2, 4] | |
-- LT |
This file contains 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
public int compareTo(Foo that) { | |
return ComparisonChain.start() | |
.compare(this.aString, that.aString) | |
.compare(this.anInt, that.anInt) | |
.compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast()) | |
.result(); | |
} |
This file contains 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
public int compareTo(Foo that) { | |
int c1 = this.aString.compareTo(that.aString); | |
if (c1 != 0) return c1; | |
int c2 = this.anInt.compareTo(that.anInt); | |
if (c2 != 0) ... | |
} |
This file contains 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
-- lexicographical ordering | |
instance Monoid Ordering where | |
mempty = EQ | |
LT `mappend` _ = LT | |
EQ `mappend` y = y | |
GT `mappend` _ = GT | |
mconcat = foldr mappend mempty |
This file contains 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
import Control.Monad.Error | |
type Comparison = Either Ordering () | |
unchain :: Ordering -> Comparison | |
unchain EQ = Right () | |
unchain LT = Left LT | |
unchain GT = Left GT | |
runChain :: Comparison -> Ordering | |
runChain (Right _) = EQ | |
runChain (Left x) = x | |
-- Either requires an error instance, this is a bit ugly | |
instance Error Ordering where | |
noMsg = LT | |
strMsg _ = LT | |
-- Lowest possible precedence | |
infixl 0 <? | |
(<?) :: (Ord a) => a -> a -> Comparison | |
a <? b = unchain $ compare a b | |
runChain $ do | |
1 <? 1 | |
"abc" <? "ab" ++ "c" | |
2.0000 <? 1.9999 | |
-- GT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment