Re: optional first map argument

2013-06-07 Thread Alice
I've modified clojure.core/destructure to support this new binding
syntax.

https://gist.github.com/doffltmiw/5730721

Here are some tests:

(let2 [[(a map?) b] [{:p 1} 2]]
  [a b])
;= [{:p1 1} 2]

(let2 [[(a map?) b] [2]]
  [a b])
;= [nil 2]

(let2 [[(a map? {:p default}) b] [2]]
  [a b])
;= [{:p default} 2]

(let2 [[a (b map?) (c String) d] [abc def ghi]]
  [a b c d])
;= [abc nil def ghi]


What do you think? Considering that everyone is writing their own
macros or helper functions, isn't it worth including in the core?



On Jun 4, 6:48 pm, Alice dofflt...@gmail.com wrote:
 What about adding this new binding syntax?

 [(attrs map?)  contents]       ; optional first map argument
 [(attrs map? {})  contents]    ; with default value

 [(s String)]

 ; is shorthand for

 [(s #(instance? String %))]

 ; a function taking optional map, vector, string arguments.
 [(m map?) (v vector?) (s String) n]

 On Jun 4, 2:22 am, Alice dofflt...@gmail.com wrote:







  I often need to do this when writing hiccup helper functions:

  (defn my-widget
    [ args]
    (let [attrs    (if (map? (first args)) (first args) {})
          contents (if (map? (first args)) (next args) args)]
      ...

  I found this post, but considering that it is 4 years old, is there
  any new library developed to help with this situation?.

 https://groups.google.com/group/clojure/browse_frm/thread/125a8af6881...

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: optional first map argument

2013-06-04 Thread Alice
What about adding this new binding syntax?

[(attrs map?)  contents]       ; optional first map argument
[(attrs map? {})  contents]    ; with default value

[(s String)]

; is shorthand for

[(s #(instance? String %))]

; a function taking optional map, vector, string arguments.
[(m map?) (v vector?) (s String) n]


On Jun 4, 2:22 am, Alice dofflt...@gmail.com wrote:
 I often need to do this when writing hiccup helper functions:

 (defn my-widget
   [ args]
   (let [attrs    (if (map? (first args)) (first args) {})
         contents (if (map? (first args)) (next args) args)]
     ...

 I found this post, but considering that it is 4 years old, is there
 any new library developed to help with this situation?.

 https://groups.google.com/group/clojure/browse_frm/thread/125a8af6881...

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




optional first map argument

2013-06-03 Thread Alice
I often need to do this when writing hiccup helper functions:

(defn my-widget
  [ args]
  (let [attrs(if (map? (first args)) (first args) {})
contents (if (map? (first args)) (next args) args)]
...


I found this post, but considering that it is 4 years old, is there
any new library developed to help with this situation?.

https://groups.google.com/group/clojure/browse_frm/thread/125a8af68813cd98

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: optional first map argument

2013-06-03 Thread gaz jones
You could look at the impl:

https://github.com/weavejester/hiccup/blob/master/src/hiccup/def.clj


On Mon, Jun 3, 2013 at 12:22 PM, Alice dofflt...@gmail.com wrote:

 I often need to do this when writing hiccup helper functions:

 (defn my-widget
   [ args]
   (let [attrs(if (map? (first args)) (first args) {})
 contents (if (map? (first args)) (next args) args)]
 ...


 I found this post, but considering that it is 4 years old, is there
 any new library developed to help with this situation?.

 https://groups.google.com/group/clojure/browse_frm/thread/125a8af68813cd98

 --
 --
 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 unsubscribe from this group and stop receiving emails from it, send an
 email to clojure+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.




-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: optional first map argument

2013-06-03 Thread Jim - FooBar();

On 03/06/13 18:22, Alice wrote:

I often need to do this when writing hiccup helper functions:

(defn my-widget
   [ args]
   (let [attrs(if (map? (first args)) (first args) {})
 contents (if (map? (first args)) (next args) args)]
 ...


I found this post, but considering that it is 4 years old, is there
any new library developed to help with this situation?.

https://groups.google.com/group/clojure/browse_frm/thread/125a8af68813cd98



why are you not using named arguments? Is there somethign in hiccup that 
prevents you from doing so? It seems to me they would save you quite a 
bit of hassle and make the code more evident...


(defn my-widget
  [ {:keys [attrs contents]}]
  ...

or if you don't know exactly what keys will come in you can relax it a 
bit with  (defn my-widget  [ {:as opts]}] ...


HTH,

Jim

ps: assuming there is no constrain I'd go for this one:

(defn my-widget
  ([attrs contents] ...)
  ([contents] (my-widget {} contents))
  ([] (my-widget {} []))

--
--
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: optional first map argument

2013-06-03 Thread Alice
I sometimes need to inspect or modify the map in the function, but
defelem only merges the map.

On Jun 4, 2:30 am, gaz jones gareth.e.jo...@gmail.com wrote:
 You could look at the impl:

 https://github.com/weavejester/hiccup/blob/master/src/hiccup/def.clj







 On Mon, Jun 3, 2013 at 12:22 PM, Alice dofflt...@gmail.com wrote:
  I often need to do this when writing hiccup helper functions:

  (defn my-widget
    [ args]
    (let [attrs    (if (map? (first args)) (first args) {})
          contents (if (map? (first args)) (next args) args)]
      ...

  I found this post, but considering that it is 4 years old, is there
  any new library developed to help with this situation?.

 https://groups.google.com/group/clojure/browse_frm/thread/125a8af6881...

  --
  --
  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 unsubscribe from this group and stop receiving emails from it, send an
  email to clojure+unsubscr...@googlegroups.com.
  For more options, visithttps://groups.google.com/groups/opt_out.

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: optional first map argument

2013-06-03 Thread Alice
The same reason that hiccup tag vector is taking optional attribute
map.
It's more readable.

(my-widget {:id id1} hello world)

vs.

(my-widget {:attr {:id id1} :contents '(hello world)})

On Jun 4, 2:38 am, Jim - FooBar(); jimpil1...@gmail.com wrote:
 On 03/06/13 18:22, Alice wrote:

  I often need to do this when writing hiccup helper functions:

  (defn my-widget
     [ args]
     (let [attrs    (if (map? (first args)) (first args) {})
           contents (if (map? (first args)) (next args) args)]
       ...

  I found this post, but considering that it is 4 years old, is there
  any new library developed to help with this situation?.

 https://groups.google.com/group/clojure/browse_frm/thread/125a8af6881...

 why are you not using named arguments? Is there somethign in hiccup that
 prevents you from doing so? It seems to me they would save you quite a
 bit of hassle and make the code more evident...

 (defn my-widget
    [ {:keys [attrs contents]}]
    ...

 or if you don't know exactly what keys will come in you can relax it a
 bit with  (defn my-widget  [ {:as opts]}] ...

 HTH,

 Jim

 ps: assuming there is no constrain I'd go for this one:

 (defn my-widget
    ([attrs contents] ...)
    ([contents] (my-widget {} contents))
    ([] (my-widget {} []))

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: optional first map argument

2013-06-03 Thread Jim - FooBar();

On 03/06/13 19:04, Alice wrote:

It's more readable.

(my-widget {:id id1} hello world)


you can get to almost exactly that with the last variant I posted.

(defn my-widget
   ([attrs contents] (apply widget-creator attrs contents)) ;;I have no idea 
what widget-creator might be
   ([contents] (my-widget {} contents))
   ([] (my-widget {} []))



 Basically, it becomes:

(my-widget {:id id1} [hello world]) ;;notice the extra vector

of course, then you need to use 'apply' on the contents if the fn you're 
eventually calling expects variadic args (from hiccup I guess).

 (apply hiccup/widget* attrs contents)

Jim


--
--
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: optional first map argument

2013-06-03 Thread Alice
 (defn my-widget
     ([attrs contents] (apply widget-creator attrs contents)) ;;I have no idea 
 what widget-creator might be
     ([contents] (my-widget {} contents))
     ([] (my-widget {} []))

contents of one arity version can be a map. so the code should be

(defn my-widget
([attrs contents] ...
([arg] (if (map? arg) (my-widget arg nil) (my-widget {}
contents)) )
([] (my-widget {} []))

but this doesn't look simpler than

(defn my-widget
  [ args]
  (let [attrs(if (map? (first args)) (first args) {})
contents (if (map? (first args)) (next args) args)]
...

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: optional first map argument

2013-06-03 Thread Meikel Brandmeyer (kotarak)
Hi,

there is always the possibility of a macro.

(defmacro defwidget
  [name  body]
  (let [[docstring [attrs _ contents] body]
(if (string? (first body))
  [(first body) (second body) (nthnext body 2)]
  [nil (first body) (next body)])]
`(defn ~name
   ~@(when docstring [docstring])
   [ args#]
   (let [[~attrs ~contents] (if (map? (first args#))
  [(first args#) (next args#)]
  [{} args#])]
 ~@body

Usage:

(defwidget my-widget
  Some widget.
  [attrs  contents]
  [:some [:hiccup attrs contents]])

Kind regards
Meikel

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.