Last active
May 3, 2021 14:30
-
-
Save davidchambers/45aa0187a32fbac6912d4b3b4e8530c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import S from 'sanctuary'; | |
import Identity from 'sanctuary-identity'; | |
// lens :: (s -> a) -> (a -> s -> s) -> Lens s a | |
const lens = getter => setter => f => s => ( | |
S.map (v => setter (v) (s)) | |
(f (getter (s))) | |
); | |
// view :: Lens s a -> s -> a | |
const view = l => x => (l (S.Left) (x)).value; | |
// over :: Lens s a -> (a -> a) -> s -> s | |
const over = l => f => x => S.extract (l (y => Identity (f (y))) (x)); | |
// email :: Lens User String | |
const email = lens (user => user.email) (email => user => ({...user, email})); | |
// user :: User | |
const user = {id: 1, email: 'dc@davidchambers.me'}; | |
console.log (view (email) (user)); | |
console.log (over (email) (S.toUpper) (user)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment