From 9a8b1f8d8e66e69873601f7e1eb400f53ea136a0 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sun, 29 Sep 2019 07:15:28 -0700 Subject: [PATCH] monads: fix ordering of writer monad logs. --- extra/monads/monads-tests.factor | 19 ++++++++++++++++++- extra/monads/monads.factor | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/extra/monads/monads-tests.factor b/extra/monads/monads-tests.factor index a7b925ca2f..ca02097401 100644 --- a/extra/monads/monads-tests.factor +++ b/extra/monads/monads-tests.factor @@ -1,4 +1,5 @@ -USING: tools.test math kernel sequences lists promises monads ; +USING: tools.test math math.functions kernel sequences lists +promises monads ; FROM: monads => do ; IN: monads.tests @@ -113,6 +114,22 @@ LAZY: nats-from ( n -- list ) run-writer ] unit-test +{ + T{ writer + { value 1.618033988749895 } + { log + "Started with five, took square root, added one, divided by two." + } + } +} [ + { + [ 5 "Started with five, " ] + [ sqrt "took square root, " ] + [ 1 + "added one, " ] + [ 2 / "divided by two." ] + } do +] unit-test + { T{ identity f 7 } } [ 4 identity-monad return diff --git a/extra/monads/monads.factor b/extra/monads/monads.factor index 485e543160..19ac7f8c07 100644 --- a/extra/monads/monads.factor +++ b/extra/monads/monads.factor @@ -188,7 +188,7 @@ M: writer-monad fail "Fail" throw ; : run-writer ( writer -- value log ) [ value>> ] [ log>> ] bi ; -M: writer >>= '[ [ _ run-writer ] dip '[ @ run-writer ] dip append ] ; +M: writer >>= '[ [ _ run-writer ] dip '[ @ run-writer ] dip prepend ] ; : pass ( writer -- writer' ) run-writer [ first2 ] dip swap call( x -- y ) ; : listen ( writer -- writer' ) run-writer [ 2array ] keep ; -- 2.34.1