0

given the following function

maxInt :: Array Number -> Int
maxInt xs = fromMaybe 0 $ join $ fromNumber <$> maximum xs

maxInt [ 2.0, 4.0, 1.0, 5.0 ] => 5

Is there some a more elegant way to do this? There is a lot of stripping away Maybes

hao
  • 10,138
  • 1
  • 35
  • 50
robkuz
  • 9,488
  • 5
  • 29
  • 50
  • What `maximum` function is this you're referring to? Evidently none of [the standard ones](https://www.haskell.org/hoogle/?hoogle=maximum) – these don't return `Maybe` values (though arguably they should. Also, what is `fromNumber`? – leftaroundabout Apr 15 '16 at 13:20

2 Answers2

2

Apply fromNumber to the entire input first, then take the maximum of that list. This way, all invalid values are converted to Nothing first, which maximum will ignore.

maxInt = fromMaybe 0 . maximum . (map fromNumber)

(This works because Ord a => Maybe a is an instance of Ord; Nothing is less than any Just value, and Just values are ordered by the underlying value.)

This also fixes a potential bug, if fromNumber (maximum xs) == Nothing. In that case, maxInt would return 0, even if there is some slightly smaller value y such that fromNumber y is not Nothing.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • fromNumber could be expensive. Your bug could be his intended behavior. – Gurkenglas Apr 15 '16 at 13:30
  • It seems unlikely that *n* calls to `fromNumber` is any more expensive than the work that the `Number` instance of `Ord` specifies to implement `maximum`. I also find it unlikely that the intended maximum of `[Just 5, Nothing]` is supposed to be 0. – chepner Apr 15 '16 at 13:44
  • It doesnt exactly answer my question (or rather implicitly with "NO") & the `join` is missing as `maximum . (map fromNumber) :: Maybe (Maybe Int)`. Nevertheless I learned that `maximum` works on `Maybe`s as well. Thats cool – robkuz Apr 25 '16 at 11:57
  • In general my question was more if the expression could be condensed much much more (which I think it can't). Sometimes I find Haskells/Purescripts orgy of nested `Maybe`s pretty exhausting ;-) – robkuz Apr 25 '16 at 12:03
  • It might help if you supplied the definition of `Array` and `Number`. – chepner Apr 25 '16 at 13:00
2

Are you sure you want to handle that with a default case of 0? If so, you're looking for maxInt xs = fromMaybe 0 $ fromNumber =<< maximum xs. If not, keep the Maybe around and do it like maxInt = fromNumber <=< maximum.

Gurkenglas
  • 2,317
  • 9
  • 17