Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating Applicative Form to Monadic - get current timestamp

I have a working applicative form, but I want to add a textField that doesn't correspond to any field in my model. Everything is working okay except for one field. Part of my model is a UTCTime value that represents the timestamp of uploading. With applicative forms I had the following code:

-- some stuff
<*> aformM (liftIO getCurrentTime)
-- more stuff

and I can't seem to find the equivalent to aformM for monadic forms. I tried the following:

(timeRes, timeView) <- mformM (liftIO getCurrentTime)

hoping that there would be an equivalent function for monadic forms, similarly to how we have areq/mreq, aopt/mopt, etc., but my code wouldn't compile. So, my question is: is there a way to get the current timestamp using monadic forms?

like image 236
bstamour Avatar asked Mar 25 '26 09:03

bstamour


1 Answers

usually an update timestamp doesn't require to be shown, so in an MForm monad you just pick the value and add it to the applicative result.

   currentTime <- liftIO getCurrentTime

   (field1_Res, field1_View) <- mreq ...

   let  myRecord_Res = MyRecord <$> pure currentTime <*> field1_Res <*> field2_Res <*> ...
like image 141
Gabriel Riba Avatar answered Mar 28 '26 02:03

Gabriel Riba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!