Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add walkthrough file #65

Merged
merged 3 commits into from
Jan 21, 2020
Merged

add walkthrough file #65

merged 3 commits into from
Jan 21, 2020

Conversation

dchelimsky
Copy link
Contributor

Our language about the state monad thus far has been "pay no attention the man behind the curtain", but I think it's actually helpful to explain the mechanics. I've found it especially confusing that we've adopted the term "flow" to talk about individual steps.

This PR includes a walkthrough that introduces the basic mechanics of the state monad and builds on that.

@@ -0,0 +1,262 @@
(require '[state-flow.core :refer [flow run]]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in a .repl file instead of .clj because the intent is to evaluate expressions one at a time, not load up a namespace.

;; primitives. Here's another primitive, which returns a specified
;; value without modifying the internal state:

(run (state/return {:count 37}) {:count 0})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will actually fail on master (as of 851620d), but #63 fixes that.

@dchelimsky dchelimsky force-pushed the dc/walkthrough branch 2 times, most recently from e3d63d7 to 44fcf76 Compare January 20, 2020 16:27
(state/wrap-fn #(fetch-users db))
#{{:name "Philip"}})) ;; <- different spelling
{:db (atom {:users #{}})})
;; => #<Pair [#{{:name "Phillip"}} {:db #atom[{:users #{{:name "Phillip"}}} 0x4c2a27a6]}]>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added #66 to sugest that we include the failure information below in the return value above

Copy link
Contributor

@philomates philomates left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏
I like it and learned a bit about :mfn and such, which I had sorta ignored until now :)

One thing I wonder about is we have a few docs kicking around with examples of how to interact with state-flow (I think I wrote two different ones). Do you think this one could replace those?

;;
;; - A monad is a wrapper around a function.
;; - A state monad is a monad whose function is a function of
;; some mutable state, which is managed for you by a runner.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe is a monad whose function is one over some mutable state? The double occurrence of function is tripping me up a little

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated using of (but removing the redundancy).

;; - :state-context is a reference to mutable state, which will be
;; managed by state-flow.
;;
;; The :mfn of a state monad is a function of state, which returns
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some reason I want to see function over state, though I dunno if that is at all more standard, let alone correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is subjective (incorrect to call it incorrect or non-standard). That said, I think of closures as closing over bindings and functions being of their arguments. Does that make sense to you?


(state/get)
;; => #cats.monad.state.State{:mfn #object[...],
;; :state-context #<State-E>}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there is a better way to represent the state context for printing. LikeState-E is quiet cryptic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Looking into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's on us:

p/Printable
(-repr [_]
"#<State-E>")))

Without that, we get this:

(state/get)
;; => {:mfn           #function[state-flow.state/reify--15501/fn--15509],
;;     :state-context #object[state_flow.state$reify__15501 0x68fabfd8 "state_flow.state$reify__15501@68fabfd8"]}

That might be a better option for now. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @sovelten - got an opinion on this from afar?

;; -------------------------------------------

;; state-flow is implemented using a state monad. If you're already
;; familiar with monads, great! If not, don't worrry. We'll explain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worrry -> worry

@dchelimsky
Copy link
Contributor Author

One thing I wonder about is we have a few docs kicking around with examples of how to interact with state-flow (I think I wrote two different ones). Do you think this one could replace those?

Links? I think this replaces the introductory material, but we still need more advanced material, which might make sense to be where you have examples already with a reference to the walkthrough.

@dchelimsky dchelimsky merged commit 7f607ba into master Jan 21, 2020
@dchelimsky dchelimsky deleted the dc/walkthrough branch January 21, 2020 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants