DEV Community

Discussion on: Grokking Lenses

Collapse
 
bytesource profile image
Bytesource

Sorry, I tried to upload the screenshot again, this time on desktop, but it didn't appear.

This is the function I have been struggling with until about five minutes ago:

let setCreditCardPostcode (postcode: Postcode) =
(Address.modifyPostcode
>> CreditCard.modifyAddress
>> User.modifyCreditCard)
(fun _ -> postcode)

I just couldn't figure out what (fun _ -> postcode) does. But when I factored out the composed modifier, I realized that (fun _ -> postcode) was the argument that is passed to Address.modifyPostcode:

// (Postcode -> Postcode) -> (User -> User)
let modifyCreditCardPostcode =
Address.modifyPostcode
>> CreditCard.modifyAddress
>> User.modifyCreditCard

// Postcode -> (User -> User)
let setCreditCartPostcode (postcode: Postcode) =
modifyCreditCardPostcode (fun _ -> postcode)

Actually, you explained this in your post, but somehow (fun _ -> postcode) just looked alien to me ;-)

Now that I understand this function, the rest of the post will make much more sense for me.

Thanks again for taking on such a difficult to explain topic and make it accessible!

Thread Thread
 
choc13 profile image
Matt Thornton • Edited

Awesome yeah you’ve got it! 🙌

The (fun _ -> postCode) does look a bit funny yeah. It’s just a lambda that ignores its input and always returns the value of the postCode argument that was passed in to the outer function. So it’s just a transformer that doesn’t depend on the current value. This is all setters are, they just overwrite what is currently stored with a completely new value.