Skip to content

Commit

Permalink
Added modify', a strict version of modify.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Palmer committed Apr 18, 2013
1 parent b234302 commit 5f53cfc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion Data/IntTrie.hs
Expand Up @@ -17,7 +17,7 @@
-------------------------------------------------------------

module Data.IntTrie
( IntTrie, identity, apply, modify, overwrite )
( IntTrie, identity, apply, modify, modify', overwrite )
where

import Control.Applicative
Expand Down Expand Up @@ -88,6 +88,22 @@ modifyPositive x f ~(BitTrie one even odd)
| testBit x 0 = BitTrie one even (modifyPositive (x `shiftR` 1) f odd)
| otherwise = BitTrie one (modifyPositive (x `shiftR` 1) f even) odd


-- | Modify the function at one point (strict version)
modify' :: (Ord b, Num b, Bits b) => b -> (a -> a) -> IntTrie a -> IntTrie a
modify' x f (IntTrie neg z pos) =
case compare x 0 of
LT -> (IntTrie $! modifyPositive' (-x) f neg) z pos
EQ -> (IntTrie neg $! f z) pos
GT -> IntTrie neg z $! modifyPositive' x f pos

modifyPositive' :: (Num b, Bits b) => b -> (a -> a) -> BitTrie a -> BitTrie a
modifyPositive' x f (BitTrie one even odd)
| x == 1 = (BitTrie $! f one) even odd
| testBit x 0 = BitTrie one even $! modifyPositive' (x `shiftR` 1) f odd
| otherwise = (BitTrie one $! modifyPositive' (x `shiftR` 1) f even) odd


-- | Overwrite the function at one point
--
-- > overwrite i x = modify i (const x)
Expand Down
2 changes: 1 addition & 1 deletion data-inttrie.cabal
@@ -1,7 +1,7 @@
Name: data-inttrie
Description:
A simple lazy, infinite trie from integers.
Version: 0.0.8
Version: 0.0.9
Stability: experimental
Synopsis: A lazy, infinite trie of integers.
License: BSD3
Expand Down

0 comments on commit 5f53cfc

Please sign in to comment.