Permalink
Browse files

Added an integral Prism to Numeric.Lens

  • Loading branch information...
1 parent dd24d11 commit 47a78836dc91a457d504c63f82a42610f2111ab3 @ekmett committed Jan 20, 2013
Showing with 12 additions and 1 deletion.
  1. +12 −1 src/Numeric/Lens.hs
View
@@ -8,13 +8,24 @@
-- Stability : provisional
-- Portability : portable
-------------------------------------------------------------------------------
-module Numeric.Lens (base) where
+module Numeric.Lens (base, integral) where
import Control.Lens
import Data.Char (chr, ord, isAsciiLower, isAsciiUpper, isDigit)
import Data.Maybe (fromMaybe)
import Numeric (readInt, showIntAtBase)
+-- | This 'Prism' extracts can be used to model the fact that every 'Integral'
+-- type is a subset of 'Integer'.
+--
+-- Embedding through the 'Prism' only succeeds if the 'Integer' would pass
+-- through unmodified when re-extracted.
+integral :: (Integral a, Integral b) => Prism Integer Integer a b
+integral = prism toInteger $ \ i -> let a = fromInteger i in
+ if toInteger a == i
+ then Right a
+ else Left i
+
-- | A prism that shows and reads integers in base-2 through base-36
--
-- >>> "100" ^? base 16

0 comments on commit 47a7883

Please sign in to comment.