Skip to content

Instantly share code, notes, and snippets.

@devn
Created February 20, 2011 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devn/835627 to your computer and use it in GitHub Desktop.
Save devn/835627 to your computer and use it in GitHub Desktop.
Factor out condp and keyed functions into updatable structures
;; I have two primary goals:
;; 1.) ...{:foo (fn ...)}... should be defined outside of the
;; function-map in a way that allows you to update :foo's (fn ...)
;; (perhaps a ref, an atom?)
;; 2.) The cases for condp: "fred", "ethel" should live in a structure
;; that can be updated as well. For instance, if I want to add "lucy"
;; (handle-lucy a b c d) to the condp I'd like to be able to do that
;; by using a separate structure to hold all of the cases for the
;; condp. The trouble is that placing them in a map or a vector, for
;; instance, means the variables a b message and d are not available
;; within the context of that structure.
;; Summary: Help me refactor this to make :foo and :bar their own
;; functions which live in #'function-map that can be updated while
;; the program is running. In addition, help me find a way to take
;; the cases for the condp and put them into their own updatable
;; container.
;; Summary 2: If you think what I'm asking for is crazy, take a whack
;; at refactoring this however you see fit.
;; This is a shortened version of a function-map that I am struggling with refactoring
(def function-map
{:foo (fn [{:keys [a b message d]}]
(let [[command & args] (.split message " ")]
(condp = command
"fred" (handle-fred a b @d)
"ethel" (handle-ethel a b (first args) @d)
nil)))
:bar (fn [{:keys [a b c]}]
(do-something a b c))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment