# ClojureDocs(beta)

## Clojure Contrib

### Namespaces

• clojure.contrib
• http
• probabilities

1.2.0

# maybe-t

• (maybe-t m)
• (maybe-t m nothing)
• (maybe-t m nothing which-m-plus)
the base values can be invalid (represented by nothing, which defaults
to nil). The third argument chooses if m-zero and m-plus are inherited
behaviour (use :m-plus-from-transformer). The default is :m-plus-from-base
if the base monad m has a definition for m-plus, and
:m-plus-from-transformer otherwise.

### 1 Example top

• ```(use 'clojure.contrib.monads)

;; We want to combine failing computations and nondeterminism
;; The sequence monad is not sufficient
user=> (domonad sequence-m [x [2 nil 3] y [2 3]] (* x y))
NullPointerException   clojure.lang.Numbers.ops (Numbers.java:942)

user=> (def maybe-seq-m (maybe-t sequence-m))
#'user/maybe-seq-m

;; The combined monad can handle failing computations
user=> (domonad maybe-seq-m [x [2 nil 3] y [2 3]] (* x y))
(4 6 nil 6 9)
```

```(defn maybe-t
the base values can be invalid (represented by nothing, which defaults
to nil). The third argument chooses if m-zero and m-plus are inherited
behaviour (use :m-plus-from-transformer). The default is :m-plus-from-base
if the base monad m has a definition for m-plus, and
:m-plus-from-transformer otherwise."
([m] (maybe-t m nil :m-plus-default))
([m nothing] (maybe-t m nothing :m-plus-default))
([m nothing which-m-plus]
(fn m-bind-maybe-t [mv f]
(m-bind mv
(fn [x]
(if (identical? x nothing)
(m-result nothing)
(f x))))))
(fn m-plus-maybe-t [& mvs]
(if (empty? mvs)
(m-result nothing)
(m-bind (first mvs)
(fn [v]
(if (= v nothing)
(apply m-plus-maybe-t (rest mvs))
(m-result v)))))))
])))```