Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I found this code interesting (not sarcasm) in demo/boiling.ml:

  let increase_level t =
    let level =
      match t.level with
      | No_Fire -> One_Fire
      | One_Fire -> Two_Fires
      | Two_Fires -> Three_Fires
      | Three_Fires -> Three_Fires
    in
    { t with level }
  ;;

  let decrease_level t =
    let level =
      match t.level with
      | No_Fire -> No_Fire
      | One_Fire -> No_Fire
      | Two_Fires -> One_Fire
      | Three_Fires -> Two_Fires
    in
    { t with level }
  ;;


Hmm, is there OCaml equivalent of Haskell's `succ`[1], which is inplemented for all sum types deriving `Enum`?

It would simplify these two functions to just `succ` and `pred`

[1]: https://hackage.haskell.org/package/base-4.20.0.1/docs/GHC-E...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: