Skip to content

Commit

Permalink
✨ unitary list is instance monad-fail
Browse files Browse the repository at this point in the history
Problem:
- The unitary list context is a monad but not monad fail.

Solution:
- Extend the unitary list context to monad fail.
  • Loading branch information
sabjohnso committed Nov 29, 2024
1 parent 8c855ec commit 79609e7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/unitary-list.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"Return the monad operators for a unitary list.
A unitary list is a list with 1 or zero elements"
(make-instance 'monad-operators
(make-instance 'monad-fail-operators
:mreturn #'list
:flatmap (lambda (f mx)
(and mx (funcall f (car mx))))))
(and mx (funcall f (car mx))))
:fail (lambda (str)
(declare (ignore str))
nil)))
3 changes: 2 additions & 1 deletion test/unitary-list-test.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
(is (equal '("X") (ctx-run context (fmap #'symbol-name (pure 'x)))))
(is (equal '("X") (ctx-run context (fapply (pure #'symbol-name) (pure 'x)))))
(is (equal '((x y)) (ctx-run context (product (pure 'x) (pure 'y)))))
(is (equal '("X") (ctx-run context (flatmap (lambda (x) (mreturn (symbol-name x))) (pure 'x)))))))
(is (equal '("X") (ctx-run context (flatmap (lambda (x) (mreturn (symbol-name x))) (pure 'x)))))
(is (equal '() (ctx-run context (fail "Yikes!"))))))

(test binding
(let ((context (make-unitary-list-context)))
Expand Down

0 comments on commit 79609e7

Please sign in to comment.