Skip to content

Commit

Permalink
upd optics-by-example
Browse files Browse the repository at this point in the history
  • Loading branch information
deemp committed Jul 11, 2023
1 parent fd2394b commit a55e5ec
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 32 deletions.
14 changes: 2 additions & 12 deletions haskell/optics-by-example/README.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Notes on [Optics by example](https://leanpub.com/optics-by-example).

{- FOURMOLU_DISABLE -}

{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
Expand Down Expand Up @@ -52,7 +53,7 @@ Notes on [Optics by example](https://leanpub.com/optics-by-example).

{- FOURMOLU_ENABLE -}

module Main (main) where
module README (main) where

import Control.Applicative (Applicative (..))
import Control.Lens
Expand Down Expand Up @@ -3925,17 +3926,6 @@ ex74 = exercises ^.. traversed . at "pushups" . non 0
-- >>> ex74
-- [0,10,15]


x :: Map.Map Text.Text Int
x = fromList [("WORLD", 456)]

x1 :: Map.Map Text.Text Int
x1 = x & at "HELLO" . non 678 .~ 3

-- >>> x1
-- fromList [("HELLO",3),("WORLD",456)]


{-
1. Board
Expand Down
8 changes: 6 additions & 2 deletions haskell/optics-by-example/optics-by-example.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ source-repository head
type: git
location: https://github.com/value/optics-by-example

executable optics-by-example
main-is: README.hs
library
exposed-modules:
Extra
README
other-modules:
Paths_optics_by_example
hs-source-dirs:
src
default-extensions:
ImportQualifiedPost
OverloadedRecordDot
Expand Down
36 changes: 18 additions & 18 deletions haskell/optics-by-example/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ ghc-options:
- -Wpartial-fields
- -Wredundant-constraints

executables:
optics-by-example:
main: README.hs
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- aeson
- containers
- lens
- lens-aeson
- mtl
- text
- template-haskell
- bytestring
- either
- generic-lens
library:
source-dirs: src
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- aeson
- containers
- lens
- lens-aeson
- mtl
- text
- template-haskell
- bytestring
- either
- generic-lens
- split

tests:
docs:
Expand Down
52 changes: 52 additions & 0 deletions haskell/optics-by-example/src/Extra.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}

module Extra where

import Control.Lens
import Control.Monad.State (execState, modify)
import Data.Generics.Labels ()
import Data.Map (fromList)
import Data.Map qualified as Map
import Data.Text qualified as Text
import Data.Traversable (for)

{-
## `non`
-}

ex1 :: Map.Map Text.Text Int
ex1 = fromList [("WORLD", 456)]

ex2 :: Map.Map Text.Text Int
ex2 = ex1 & at "HELLO" . non 678 .~ 3

-- >>> x1
-- fromList [("HELLO",3),("WORLD",456)]

{-
## update at multiple indices
-}

ex3 :: [Text.Text] -> Int -> Map.Map Text.Text Int -> Map.Map Text.Text Int
ex3 ks val = execState (traverse (\k -> modify (at k ?~ val)) ks)

ex4 :: Map.Map Text.Text Int
ex4 = Map.empty & ex3 ["a", "b", "c"] 4

-- >>> ex4
-- fromList [("a",4),("b",4),("c",4)]

ex5 :: Map.Map String Integer
ex5 = Map.empty &~ for ["b", "c", "d"] (\k -> at k ?= 10)

-- >>> ex5
-- fromList [("b",10),("c",10),("d",10)]

0 comments on commit a55e5ec

Please sign in to comment.