Last active
August 4, 2020 05:01
-
-
Save Ebmtranceboy/397e0d82042dc68e636624673a5d930c to your computer and use it in GitHub Desktop.
Map + Set lenses
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
module Main where | |
import Prelude | |
import Concur.Core (Widget) | |
import Concur.React (HTML) | |
import Concur.React.DOM (text, div', br', h3') as D | |
import Concur.React.Run (runWidgetInDom) | |
import Effect (Effect) | |
import Data.Lens (Lens', view, over, set) | |
import Data.Lens.At (at) | |
import Data.Map (Map, insert, empty) | |
import Data.Maybe (Maybe(..), fromMaybe, maybe) | |
import Data.Set (Set, insert, empty) as Set | |
tableMap :: Map Int String | |
tableMap = | |
insert 2 "prime" | |
$ insert 496 "perfect" | |
$ insert 1729 "cube" $ empty | |
_atPrime :: Lens' (Map Int String) (Maybe String) | |
_atPrime = at 2 | |
tableSet :: Set.Set Int | |
tableSet = Set.insert 12 $ Set.insert 32 $ Set.insert 75 Set.empty | |
_atComposite :: Lens' (Set.Set Int) (Maybe Unit) | |
_atComposite = at 75 | |
exerciseWidget :: forall a. Widget HTML a | |
exerciseWidget = | |
D.div' $ (pure <<< (_ <> D.br')) =<< | |
[ D.text $ show tableMap | |
, D.text $ show tableSet | |
, D.h3' [D.text "map basics"] | |
, D.text $ fromMaybe "no prime" $ view _atPrime tableMap | |
, D.text $ show $ set _atPrime (Just "even") tableMap | |
, D.text $ show $ over _atPrime (\x -> (<>) <$> x <*> Just " and even") tableMap | |
, D.h3' [D.text "set basics"] | |
, D.text $ maybe "no 75" (const "75 present") $ view _atComposite tableSet | |
, D.text $ show $ set (at 100) (Just unit) tableSet | |
, D.text $ show $ over (at 100) (const $ Just unit) tableSet | |
] | |
main :: Effect Unit | |
main = do | |
runWidgetInDom "main" exerciseWidget |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment