codesnippets:statemonads
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| codesnippets:statemonads [2021/05/04 00:37] – [A decoder] f2b216 | codesnippets: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:// | * inspired by [[http:// | ||
| Line 332: | Line 331: | ||
| -- / | -- / | ||
| -- v v v v v v | -- v v v v v v | ||
| - | print ((parse " | + | print ((decode |
| type DecodeState = [Char] -- the remaining chars | type DecodeState = [Char] -- the remaining chars | ||
| class Decoder tValue where | class Decoder tValue where | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| do | do | ||
| nCount <- takeRepetitionCountS | nCount <- takeRepetitionCountS | ||
| - | liValues <- parseNListS | + | liValues <- decodeNListS |
| return liValues | return liValues | ||
| - | | + | |
| - | | + | |
| | lCount > 0 = | | lCount > 0 = | ||
| do | do | ||
| - | mx <- parseMaybeS | + | mx <- decodeMaybeS |
| Sfr.ifJust mx | Sfr.ifJust mx | ||
| {- then -} (\x -> | {- then -} (\x -> | ||
| do | do | ||
| - | liValues <- parseNListS | + | liValues <- decodeNListS |
| return (x : liValues)) | return (x : liValues)) | ||
| {- else -} (return []) | {- else -} (return []) | ||
| Line 363: | Line 362: | ||
| instance Decoder MyData where | instance Decoder MyData where | ||
| - | | + | |
| 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 | ||
| - | | + | |
| do | do | ||
| nLength <- takeRepetitionCountS | nLength <- takeRepetitionCountS | ||
| Line 387: | Line 386: | ||
| instance Decoder Double where | instance Decoder Double where | ||
| - | | + | |
| do | do | ||
| nLength <- takeRepetitionCountS | nLength <- takeRepetitionCountS | ||
| Line 486: | Line 485: | ||
| Just (MyData 4736 [7364537284958, | Just (MyData 4736 [7364537284958, | ||
| </ | </ | ||
| + | |||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/statemonads.1620081450.txt.gz · Last modified: (external edit)
