Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added an integral Prism to Numeric.Lens

  • Loading branch information...
commit 47a78836dc91a457d504c63f82a42610f2111ab3 1 parent dd24d11
Edward Kmett authored
Showing with 12 additions and 1 deletion.
  1. +12 −1 src/Numeric/Lens.hs
13 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
Please sign in to comment.
Something went wrong with that request. Please try again.