Last active
May 3, 2021 14:30
-
-
Save davidchambers/45aa0187a32fbac6912d4b3b4e8530c5 to your computer and use it in GitHub Desktop.
Revisions
-
davidchambers revised this gist
Apr 27, 2021 . 1 changed file with 1 addition and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,21 +2,14 @@ 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)); -
davidchambers created this gist
Apr 27, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ import S from 'sanctuary'; import Identity from 'sanctuary-identity'; // Constant :: b -> Constant a b const Constant = x => ({ '@@type': 'sanctuary-constant/Constant', 'fantasy-land/map': f => Constant (x), 'fantasy-land/extract': () => x, }); // 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 => S.extract (l (Constant) (x)); // 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: '[email protected]'}; console.log (view (email) (user)); console.log (over (email) (S.toUpper) (user));