594
you are viewing a single comment's thread
view the rest of the comments
[-] CanadaPlus@lemmy.sdf.org 14 points 4 days ago* (last edited 4 days ago)

That honestly seems like the best way to write conditionalBaptize but I still hate it. Probably because IRL you'd just rewrite baptism instead of retrofitting the function with a clever use of id.

[-] solrize@lemmy.ml 10 points 4 days ago* (last edited 4 days ago)

This is probably an ok use for a GADT. Something like:

{-# LANGUAGE DataKinds      #-}
{-# LANGUAGE GADTs          #-}
{-# LANGUAGE KindSignatures #-}

data Bap = Baptized | Unbaptized

data Person :: Bap -> * where
   Baptize :: Person Unbaptized -> Person Baptized
   NewPerson :: Person Unbaptized

conditionalBaptize :: Person a -> Person Baptized
conditionalBaptize p =
    case p of NewPerson -> Baptize p
              Baptize _ -> p

main = return ()
[-] KazuchijouNo@lemy.lol 13 points 4 days ago

Thank you for refactoring baptism. How do we push this to production now?

[-] Stizzah@lemmygrad.ml 4 points 4 days ago

Hey hey hey, let's start with a PR, we are not savages here aren't we?

[-] expr@programming.dev 5 points 3 days ago* (last edited 3 days ago)

It looks pretty normal to me as a professional Haskeller, though I suppose it's perhaps slightly cleaner to write it as conditionalBaptize p = fromMaybe p $ baptize p. It's largely just a matter of taste and I'd accept either version when reviewing an MR.

Edit: I just thought of another version that actually is far too clever and shouldn't be used:

conditionalBaptize = ap fromMaybe baptize, making use of the monad instance for ->. But yeah, don't do this.

this post was submitted on 17 Aug 2025
594 points (99.2% liked)

Programmer Humor

25843 readers
2109 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS