User Tools

Site Tools


codesnippets:statemonads

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
codesnippets:statemonads [2021/05/04 00:37] – [A decoder] f2b216codesnippets:statemonads [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== State monads ====== ====== State monads ======
-~~DISCUSSION ~~ 
  
   * inspired by [[http://brandon.si/code/the-state-monad-a-tutorial-for-the-confused/|Brandon Simmons: The State Monad: A Tutorial for the Confused?]]   * inspired by [[http://brandon.si/code/the-state-monad-a-tutorial-for-the-confused/|Brandon Simmons: The State Monad: A Tutorial for the Confused?]]
Line 332: Line 331:
         --               /      /  /                /    /     /         --               /      /  /                /    /     /
         --              v      v  v                v    v     v         --              v      v  v                v    v     v
-        print ((parse "|4:4736|3:|13:7364537284958|2:12|3:123|11:44543.23456") :: (Maybe MyData))+        print ((decode "|4:4736|3:|13:7364537284958|2:12|3:123|11:44543.23456") :: (Maybe MyData))
  
 type DecodeState = [Char] -- the remaining chars type DecodeState = [Char] -- the remaining chars
  
 class Decoder tValue where class Decoder tValue where
-    parse :: [Char] -> (Maybe tValue) +    decode :: DecodeState -> (Maybe tValue) 
-    parse lch = S.evalState parseMaybeS lch +    decode lch = S.evalState decodeMaybeS lch 
-    parseMaybeS :: S.State DecodeState (Maybe tValue) +    decodeMaybeS :: S.State DecodeState (Maybe tValue) 
-    parseListS :: S.State DecodeState [tValue] +    decodeListS :: S.State DecodeState [tValue] 
-    parseListS +    decodeListS 
         do         do
             nCount <- takeRepetitionCountS             nCount <- takeRepetitionCountS
-            liValues <- parseNListS nCount+            liValues <- decodeNListS nCount
             return liValues             return liValues
-    parseNListS :: Integer -> S.State DecodeState [tValue] +    decodeNListS :: Integer -> S.State DecodeState [tValue] 
-    parseNListS lCount+    decodeNListS lCount
         | lCount > 0 =          | lCount > 0 = 
             do             do
-                mx <- parseMaybeS+                mx <- decodeMaybeS
                 Sfr.ifJust mx                 Sfr.ifJust mx
                     {- then -} (\x ->                      {- then -} (\x -> 
                         do                         do
-                            liValues <- parseNListS (lCount - 1)+                            liValues <- decodeNListS (lCount - 1)
                             return (x : liValues))                             return (x : liValues))
                     {- else -} (return [])                     {- else -} (return [])
Line 363: Line 362:
  
 instance Decoder MyData where instance Decoder MyData where
-    parseMaybeS +    decodeMaybeS 
         do         do
-            mn1 <- parseMaybeS+            mn1 <- decodeMaybeS
             Sfr.ifJust mn1             Sfr.ifJust mn1
                 {- then -} (\n1 ->                  {- then -} (\n1 -> 
                     do                     do
-                        ln2 <- parseListS +                        ln2 <- decodeListS 
-                        mn3 <- parseMaybeS+                        mn3 <- decodeMaybeS
                         Sfr.ifJust mn3                         Sfr.ifJust mn3
                             {- then -} (\n3 -> return (Just (MyData n1 ln2 n3)))                             {- then -} (\n3 -> return (Just (MyData n1 ln2 n3)))
Line 378: Line 377:
  
 instance Decoder Integer where instance Decoder Integer where
-    parseMaybeS +    decodeMaybeS 
         do         do
             nLength <- takeRepetitionCountS              nLength <- takeRepetitionCountS 
Line 387: Line 386:
  
 instance Decoder Double where instance Decoder Double where
-    parseMaybeS +    decodeMaybeS 
         do         do
             nLength <- takeRepetitionCountS              nLength <- takeRepetitionCountS 
Line 486: Line 485:
 Just (MyData 4736 [7364537284958,12,123] 44543.23456) Just (MyData 4736 [7364537284958,12,123] 44543.23456)
 </code> </code>
 +
 +
 +===== ✎ =====
 +~~DISCUSSION~~
codesnippets/statemonads.1620081450.txt.gz · Last modified: (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki