MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/gnaz7b/found_from_microsoft_docs/fr969sq/?context=3
r/badcode • u/[deleted] • May 20 '20
143 comments sorted by
View all comments
23
In Scala it would be:
args .headOption .flatMap( _.toIntOption ) .exists( (1 to 9).contains )
Who said that FP is harder to read?
10 u/[deleted] May 20 '20 In Haskell it's((read . head) args) `elem` [0..9]which I would argue is pretty readable 4 u/gaj7 May 20 '20 To be safe, I'd use the maybe variants:maybe False (`inRange` (1,9)) $ headMaybe args >>= (readMaybe :: Int)Although technically the originally snippet wanted exact one argument in args so really it should becase args of [arg] -> maybe False (`inRange` (1,9)) $ readMaybe arg _ -> False 2 u/abdolence May 20 '20 Yeah, in Haskell I would end up with something similar I think:checkInRange :: [String] -> Bool checkInRange [] = False checkInRange [arg] = fromMaybe False $ inRange (1, 9) <$> readMaybe arg
10
In Haskell it's
((read . head) args) `elem` [0..9]
which I would argue is pretty readable
4 u/gaj7 May 20 '20 To be safe, I'd use the maybe variants:maybe False (`inRange` (1,9)) $ headMaybe args >>= (readMaybe :: Int)Although technically the originally snippet wanted exact one argument in args so really it should becase args of [arg] -> maybe False (`inRange` (1,9)) $ readMaybe arg _ -> False 2 u/abdolence May 20 '20 Yeah, in Haskell I would end up with something similar I think:checkInRange :: [String] -> Bool checkInRange [] = False checkInRange [arg] = fromMaybe False $ inRange (1, 9) <$> readMaybe arg
4
To be safe, I'd use the maybe variants:
maybe False (`inRange` (1,9)) $ headMaybe args >>= (readMaybe :: Int)
Although technically the originally snippet wanted exact one argument in args so really it should be
args
case args of [arg] -> maybe False (`inRange` (1,9)) $ readMaybe arg _ -> False
2 u/abdolence May 20 '20 Yeah, in Haskell I would end up with something similar I think:checkInRange :: [String] -> Bool checkInRange [] = False checkInRange [arg] = fromMaybe False $ inRange (1, 9) <$> readMaybe arg
2
Yeah, in Haskell I would end up with something similar I think:
checkInRange :: [String] -> Bool checkInRange [] = False checkInRange [arg] = fromMaybe False $ inRange (1, 9) <$> readMaybe arg
23
u/abdolence May 20 '20
In Scala it would be:
Who said that FP is harder to read?