Skip to content
This repository
Browse code

Fixed our lens tutorial to match the new lens library

This resolves issue #22.
  • Loading branch information...
commit ae137a3efc9e27008dc914c9d7868eccd5398c26 1 parent bfb7f02
Doug Beardsley authored December 04, 2012

Showing 1 changed file with 28 additions and 27 deletions. Show diff stats Hide diff stats

  1. 55  src/Snap/Snaplet.hs
55  src/Snap/Snaplet.hs
@@ -147,37 +147,37 @@ import           Snap.Snaplet.Internal.Types
147 147
 -- /within the context/ of the base state. Given that Haskell datatypes are
148 148
 -- pure, how do you allow for this?
149 149
 --
150  
--- Our solution is to use /lenses/, as defined in the @data-lens@ library
151  
--- (<http://hackage.haskell.org/package/data-lens>). A lens, notated as
152  
--- follows:
  150
+-- Our solution is to use /lenses/, as defined in Edward Kmett's @lens@
  151
+-- library (<http://hackage.haskell.org/package/lens>). A lens, notated
  152
+-- as follows:
153 153
 --
154  
--- > Lens a b
  154
+-- > SimpleLens a b
155 155
 --
156  
--- is a \"getter\" and a \"setter\" rolled up into one. The @data-lens@
157  
--- library provides the following functions:
  156
+-- is conceptually a \"getter\" and a \"setter\" rolled up into one. The
  157
+-- @lens@ library provides the following functions:
158 158
 --
159  
--- > getL :: (Lens a b) -> a -> b
160  
--- > setL :: (Lens a b) -> b -> a -> a
161  
--- > modL :: (Lens a b) -> (b -> b) -> a -> a
  159
+-- > view :: (SimpleLens a b) -> a -> b
  160
+-- > set  :: (SimpleLens a b) -> b -> a -> a
  161
+-- > over :: (SimpleLens a b) -> (b -> b) -> a -> a
162 162
 --
163 163
 -- which allow you to get, set, and modify a value of type @b@ within the
164  
--- context of type of type @a@. The @data-lens@ package comes with a Template
165  
--- Haskell function called 'makeLenses', which auto-magically defines a lens
166  
--- for every record field having a name beginning with an underscore. In the
167  
--- @App@ example above, adding the declaration:
  164
+-- context of type @a@. The @lens@ package comes with a Template Haskell
  165
+-- function called 'makeLenses', which auto-magically defines a lens for every
  166
+-- record field having a name beginning with an underscore. In the @App@
  167
+-- example above, adding the declaration:
168 168
 --
169  
--- > makeLenses [''App]
  169
+-- > makeLenses ''App
170 170
 --
171 171
 -- would define lenses:
172 172
 --
173  
--- > foo                :: Lens App (Snaplet Foo)
174  
--- > bar                :: Lens App (Snaplet Bar)
175  
--- > someNonSnapletData :: Lens App String
  173
+-- > foo                :: SimpleLens App (Snaplet Foo)
  174
+-- > bar                :: SimpleLens App (Snaplet Bar)
  175
+-- > someNonSnapletData :: SimpleLens App String
176 176
 --
177  
--- The coolest thing about @data-lens@ lenses is that they /compose/, using
178  
--- the "Control.Category"'s generalization of the @(.)@ operator. If the @Foo@
179  
--- type had a field of type @Quux@ within it with a lens @quux :: Lens Foo
180  
--- Quux@, then you could create a lens of type @Lens App Quux@ by composition:
  177
+-- The coolest thing about @lens@ lenses is that they /compose/ using the
  178
+-- @(.)@ operator. If the @Foo@ type had a field of type @Quux@ within it with
  179
+-- a lens @quux :: SimpleLens Foo Quux@, then you could create a lens of type
  180
+-- @SimpleLens App Quux@ by composition:
181 181
 --
182 182
 -- > import Control.Category
183 183
 -- > import Prelude hiding ((.))    -- you have to hide (.) from the Prelude
@@ -187,13 +187,14 @@ import           Snap.Snaplet.Internal.Types
187 187
 -- > makeLenses [''Foo]
188 188
 -- >
189 189
 -- > -- snapletValue is defined in the framework:
190  
--- > snapletValue :: Lens (Snaplet a) a
  190
+-- > snapletValue :: SimpleLens (Snaplet a) a
191 191
 -- >
192  
--- > appQuuxLens :: Lens App Quux
193  
--- > appQuuxLens = quux . snapletValue . foo
  192
+-- > appQuuxLens :: SimpleLens App Quux
  193
+-- > appQuuxLens = foo . snapletValue . quux
194 194
 --
195  
--- Lens composition is very similar to function composition, but it gives you
196  
--- a composed getter and setter at the same time.
  195
+-- Lens composition is very similar to function composition except it works in
  196
+-- the opposite direction (think Java-style System.out.println ordering) and
  197
+-- it gives you a composed getter and setter at the same time.
197 198
 
198 199
 -- $monadSnaplet
199 200
 -- The primary abstraction in the snaplet infrastructure is a combination of
@@ -203,7 +204,7 @@ import           Snap.Snaplet.Internal.Types
203 204
 -- state.  This allows quux snaplet functions to access and modify the Quux
204 205
 -- data structure without knowing anything about the App or Foo data
205 206
 -- structures. It also lets other snaplets call functions from the quux
206  
--- snaplet if they have the quux snaplet's lens @Lens App (Snaplet Quux)@.
  207
+-- snaplet if they have the quux snaplet's lens @SimpleLens App (Snaplet Quux)@.
207 208
 -- We can view our application as a tree of snaplets and other pieces of data.
208 209
 -- The lenses are like pointers to nodes of the tree. If you have a pointer to
209 210
 -- a node, you can access the node and all of its children without knowing

0 notes on commit ae137a3

Please sign in to comment.
Something went wrong with that request. Please try again.