Skip to content

Code examples in the documentation don't return what's expected #240

@piotr-yuxuan

Description

@piotr-yuxuan

Versions used:

  • [org.clojure/clojure "1.10.1"]
  • [funcool/cats "2.3.6"]
  • [funcool/promesa "5.1.0"]

ns form:

(ns user
  (:require
    [cats.builtin]
    [cats.core :refer [bind return alet mlet fmap fapply]]
    [cats.monad.maybe :refer [just]]
    [cats.labs.promise]
    [promesa.core :as p]))

When evaluating the examples provided in https://funcool.github.io/cats/latest/#alet, some results don't match what is shown in the doc:

(macroexpand '(alet [a (just 1)
                     b (just 41)]
                (+ a b)))
;; =>
(fapply
  (fmap
    (fn [a]
      (fn [b]
        (do (+ a b))))
    (just 1))
  (just 41))

;; the documentation suggests it returns:
(fapply (fn [a]
          (fn [b]
            (do
              (+ a b))))
        (just 1)
        (just 41))

The asynchronous code raises an exception:

(defn sleep-promise
  "A simple function that emulates an
  asynchronous operation."
  [wait]
  (p/promise (fn [resolve reject]
               (future
                 (Thread/sleep wait)
                 (resolve wait)))))

(time
  @(mlet [x (sleep-promise 42)
          y (sleep-promise 41)]
         (return (+ x y))))
;; Execution error (ClassCastException) at user/eval34213$fn$fn (user.clj:4).
;; class user$sleep_promise$fn__34206 cannot be cast to class java.lang.Number (user$sleep_promise$fn__34206 is in unnamed module of loader clojure.lang.DynamicClassLoader @571ca823; java.lang.Number is in module java.base of loader 'bootstrap')

Anyway thanks for the work already done on this library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions