Thread Macro For Inserting In Arbitrary Positions

2011-07-05 Thread Asim Jalis
Frequently I want to use the thread macros (- or -) except I need
to thread the arguments into positions other than the first and the
last. Or sometimes I have to go back and forth between first and last
positions. Instead of alternating between - and - and creating
nesting, I've been using this thread macro which replaces underscore
with the value of the previous expression.

(defmacro --
  Threads the expr through the forms. Replaces all underscores (_)
  with x in the form, making a list of it if it is not a list already.
  If there are more forms, inserts the first form as the last item in
  second form, etc.
  ([x form] (if (seq? form)
  (with-meta `(~(first form) ~@(replace {'_ x} (next
form))) (meta form))
  (list form x)))
  ([x form  more] `(-- (-- ~x ~form) ~@more)))

Is there a reason something like this does not exist in clojure.core?
Is this an oversight or is there a reason this is not there?

Asim

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Thread Macro For Inserting In Arbitrary Positions

2011-07-05 Thread Asim Jalis
Here is an example of using this:

(-- hello world (.toUpperCase _) (.toLowerCase _) (.indexOf _ w))

On Tue, Jul 5, 2011 at 3:50 PM, Asim Jalis asimja...@gmail.com wrote:
 Frequently I want to use the thread macros (- or -) except I need
 to thread the arguments into positions other than the first and the
 last. Or sometimes I have to go back and forth between first and last
 positions. Instead of alternating between - and - and creating
 nesting, I've been using this thread macro which replaces underscore
 with the value of the previous expression.

 (defmacro --
  Threads the expr through the forms. Replaces all underscores (_)
  with x in the form, making a list of it if it is not a list already.
  If there are more forms, inserts the first form as the last item in
  second form, etc.
  ([x form] (if (seq? form)
              (with-meta `(~(first form) ~@(replace {'_ x} (next
 form))) (meta form))
              (list form x)))
  ([x form  more] `(-- (-- ~x ~form) ~@more)))

 Is there a reason something like this does not exist in clojure.core?
 Is this an oversight or is there a reason this is not there?

 Asim


-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Thread Macro For Inserting In Arbitrary Positions

2011-07-05 Thread Mark Rathwell
 Is there a reason something like this does not exist in clojure.core?
 Is this an oversight or is there a reason this is not there?

Previous discussions on this subject:

1.
http://groups.google.com/group/clojure/browse_thread/thread/e826fc303e440b7c/0e7bdba707b7982d

  (in particular Rich's response on the second page:
http://groups.google.com/group/clojure/msg/1eb5cf8fa3248049 )


2.
http://groups.google.com/group/clojure/browse_thread/thread/66ff0b89229be894/c3d4a6dae45d4852



On Tue, Jul 5, 2011 at 7:14 PM, Asim Jalis asimja...@gmail.com wrote:

 Here is an example of using this:

 (-- hello world (.toUpperCase _) (.toLowerCase _) (.indexOf _ w))

 On Tue, Jul 5, 2011 at 3:50 PM, Asim Jalis asimja...@gmail.com wrote:
  Frequently I want to use the thread macros (- or -) except I need
  to thread the arguments into positions other than the first and the
  last. Or sometimes I have to go back and forth between first and last
  positions. Instead of alternating between - and - and creating
  nesting, I've been using this thread macro which replaces underscore
  with the value of the previous expression.
 
  (defmacro --
   Threads the expr through the forms. Replaces all underscores (_)
   with x in the form, making a list of it if it is not a list already.
   If there are more forms, inserts the first form as the last item in
   second form, etc.
   ([x form] (if (seq? form)
   (with-meta `(~(first form) ~@(replace {'_ x} (next
  form))) (meta form))
   (list form x)))
   ([x form  more] `(-- (-- ~x ~form) ~@more)))
 
  Is there a reason something like this does not exist in clojure.core?
  Is this an oversight or is there a reason this is not there?
 
  Asim
 

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en


-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Thread Macro For Inserting In Arbitrary Positions

2011-07-05 Thread Sean Corfield
On Tue, Jul 5, 2011 at 4:14 PM, Asim Jalis asimja...@gmail.com wrote:
 Here is an example of using this:

 (-- hello world (.toUpperCase _) (.toLowerCase _) (.indexOf _ w))

BTW, that's not a very compelling example since you can already do:

(- hello world! .toUpperCase .toLowerCase (.indexOf w))
-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

Perfection is the enemy of the good.
-- Gustave Flaubert, French realist novelist (1821-1880)

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en