]> gitweb.factorcode.org Git - factor.git/commitdiff
monads: fix ordering of writer monad logs.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 29 Sep 2019 14:15:28 +0000 (07:15 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 29 Sep 2019 14:16:03 +0000 (07:16 -0700)
extra/monads/monads-tests.factor
extra/monads/monads.factor

index a7b925ca2fc0cf09619c6ae20b0a43f4b957e933..ca02097401434e528b8b63a0f2713d3cee1cefaa 100644 (file)
@@ -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, " <writer> ]
+        [ sqrt "took square root, " <writer> ]
+        [ 1 + "added one, " <writer> ]
+        [ 2 / "divided by two." <writer> ]
+    } do
+] unit-test
+
 { T{ identity f 7 } }
 [
     4 identity-monad return
index 485e5431605af1b6ab7c9724c969eed5972cbe1c..19ac7f8c076eafd12105172c1e020aa300a79d88 100644 (file)
@@ -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 <writer> ] ;
+M: writer >>= '[ [ _ run-writer ] dip '[ @ run-writer ] dip prepend <writer> ] ;
 
 : pass ( writer -- writer' ) run-writer [ first2 ] dip swap call( x -- y ) <writer> ;
 : listen ( writer -- writer' ) run-writer [ 2array ] keep <writer> ;