mirror of
https://git.in.rschanz.org/ryan77627/guix.git
synced 2024-11-07 07:26:13 -05:00
monads: Fix 'mapm' so that effects happen from left to right.
* guix/monads.scm (mapm): Don't reverse LST, so that items are processed from left to right. Bind the result of 'foldm' and reverse it. * tests/monads.scm ("sequence"): Change 'frob' so it performs its side effect within an 'mlet' body. Adjust call accordingly.
This commit is contained in:
parent
c2150d9ace
commit
f62435e286
2 changed files with 17 additions and 13 deletions
|
@ -209,13 +209,15 @@ (define (foldm monad mproc init lst)
|
|||
|
||||
(define (mapm monad mproc lst)
|
||||
"Map MPROC over LST, a list of monadic values in MONAD, and return a monadic
|
||||
list."
|
||||
(foldm monad
|
||||
list. LST items are bound from left to right, so effects in MONAD are known
|
||||
to happen in that order."
|
||||
(mlet monad ((result (foldm monad
|
||||
(lambda (item result)
|
||||
(mlet monad ((item (mproc item)))
|
||||
(return (cons item result))))
|
||||
'()
|
||||
(reverse lst)))
|
||||
lst)))
|
||||
(return (reverse result))))
|
||||
|
||||
(define-inlinable (sequence monad lst)
|
||||
"Turn the list of monadic values LST into a monadic list of values, by
|
||||
|
|
|
@ -166,14 +166,16 @@ (define derivation-expression
|
|||
(let* ((input (iota 100))
|
||||
(order '()))
|
||||
(define (frob i)
|
||||
(mlet monad ((foo (return 'foo)))
|
||||
;; The side effect here is used to keep track of the order in
|
||||
;; which monadic values are bound.
|
||||
;; which monadic values are bound. Perform the side effect
|
||||
;; within a '>>=' so that it is performed when the return
|
||||
;; value is actually bound.
|
||||
(set! order (cons i order))
|
||||
i)
|
||||
(return i)))
|
||||
|
||||
(and (equal? input
|
||||
(run (sequence monad
|
||||
(map (lift1 frob monad) input))))
|
||||
(run (sequence monad (map frob input))))
|
||||
|
||||
;; Make sure this is from left to right.
|
||||
(equal? order (reverse input)))))
|
||||
|
|
Loading…
Reference in a new issue