I am using vim-sexp and vim-sexp-mappings-for-regular-people plugins for editing Clojure files. I don't quite understand what slurp and barf commands do exactly.
I tried playing with them, and it seems that they insert/remove forms at the beginning/end of an adjacent form. Is that correct? If not, what is the proper definition for slurp and barf ?
slurping and barfing are the essential operations/concepts to use one of the modern . 的基本操作/概念。 after getting used to them I'm completely incapable of editing code without these. Of the ~20 people that sit with me writing clojure all day all of them use these all the time. so saying that they are "helpful for lisp coders" is a very tactful and polite understatement.
slurp: (verb)
"to include the item to one side of the expression surrounding the point into the expression"
barf: (verb)
"to exclude either the left most or right most item in the expression surrounding the point from the expression"
and some examples.
1 2 (3 4) 5 6
slurp right:
1 2 (3 4 5) 6
barf right:
1 2 (3 4) 5 6
slurp left:
1 (2 3 4) 5 6
barf left:
1 2 (3 4) 5 6
and we're back where we started.
When I give talks/presentations introducing paredit I generally leave students/attendees with just these two concepts because I feel they are enough to start getting the benefit of structural editing without being overwhelming. Once you are comfortable with these by learning to move forward/backward and up/down by expression rather than by character. 通过学习按表达式而不是按字符向前/向后和向上/向下移动来继续 。
even though it list emacs keybindings I still highly recommend the animated guide to paredit that Peter Rincker mentions in his answer.
I am not an expert on: lisps, emacs, paredit, vim-sexp, or vim-sexp-mappings-for-regular-people. (Why am I posting right?!)
However I know that slurp and barf come form Emac's paredit mode. This Emacs mode is supposedly very helpful for lisp coders. I am sure you can find a nice helpful article on these subjects if you search for paredit. As a matter a fact I found a nice article for you: The Animated Guide to Paredit . From what I can tell you are right in your guesses about slurp and barf.
it may seem gross, but i visualise barf-ing like vomiting (they are synonyms after all), where you are expelling something out.
slurping, i visualise having a drink through a straw and drawing in the drink.
The pipe symbol is the cursor in these illustrations.
so barf-ing to the right (pushing out the 4
)
1 2 (3 |4) 5 6 -> 1 2 (3|) 4 5 6
and slurping to the right gets you back the 4 (gross as it may be to re-ingest what you previously threw up)
1 2 (3|) 4 5 6 -> 1 2 (3 4) 5 6
The backward version do the same things but with items before the current s-exp.
I find I use the forward/right versions much more than the left, as I'm usually adding something in front, like a let
binding, so a session might be:
(some-fn1 (count some-map))
(some-fn2 (count some-map))
aha, a let could come in here to refactor the (count some-map)
:
(let [c (count some-map)]|)
(some-fn1 c)
(some-fn2 c)
But the let isn't wrapping the 2 calls, so we want to pull in (slurp) the next 2 forms inside the let s-exp, so now at the cursor position, slurp twice which will give after first:
(let [c (count some-map)]|
(some-fn1 c))
(some-fn2 c)
and then on second:
(let [c (count some-map)]|
(some-fn1 c)
(some-fn2 c))
and any decent editor with paredit/structural editing will also do the indentation at the same time for you.
It's also important to note that the barf/slurp will happen within the current set of brackets (ie slurping (let [a (count x)])
will do different things depending on where the cursor is, as there are 3 sets of brackets), hence why I was careful where to put the cursor in the let binding above, else you're push in/out the wrong bracket (which is another way of thinking of barf/slurping - manipulating the position of the bracket rather than pull/pushing items into/out of the s-exp).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.