Re: a convenience idea for test functions

2012-04-18 Thread Dmitri
That is an excellent point, and the macro is actually a very nice
approach, thanks for the help.


On Apr 18, 1:07 am, Sean Corfield seancorfi...@gmail.com wrote:
 On Tue, Apr 17, 2012 at 9:16 PM, Dmitri dmitri.sotni...@gmail.com wrote:
  (map? foo bar baz) would return bar if foo is a map and baz otherwise.

 To elaborate on Alan's response, consider:

 (if (map? foo) (/ bar 0) baz)

 If map? were 'merely' a variadic function, (map? foo (/ bar 0) baz)
 would fail because (/ bar 0) would be evaluated and then passed as an
 argument, along with foo and baz.

 So, no, the simple answer is that you can't just make the test
 functions variadic and get the same behavior as an if form (hence
 Alan's suggestion of a macro-generating macro to create new macros for
 the forms you want).

 (Apologies if I'm laboring the point here)
 --
 Sean A Corfield -- (904) 302-SEAN
 An Architect's View --http://corfield.org/
 World Singles, LLC. --http://worldsingles.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


a convenience idea for test functions

2012-04-17 Thread Dmitri
Often times I find myself writing code like the following

(if (map? foo) bar baz)

would it make sense to make test functions variadic, so if only passed
a single argument it would return true/false, but could also act as an
if when passed 3 arguments, eg:

(map? foo bar baz) would return bar if foo is a map and baz otherwise.

-- 
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: a convenience idea for test functions

2012-04-17 Thread Alan Malloy
IMO this is fairly absurd for a language default, but you can easily
do this yourself in a number of ways. For example:

(defmacro define-preds [ preds]
  (cons `do
(for [pred preds]
  `(defmacro ~(symbol (str pred +)) ~'[obj then else]
 (list '~'if (list '~pred ~'obj)
   ~'then ~'else)

(define-preds map? string?)

(map?+ {} 1 2) ;; 1

This would be quite a lot easier if we didn't care about the short-
circuiting behavior of 'if, since then a simple higher-order function
would do it, something like (defn as-switch [pred] (fn [obj then else]
(if (pred obj) then else))).

On Apr 17, 9:16 pm, Dmitri dmitri.sotni...@gmail.com wrote:
 Often times I find myself writing code like the following

 (if (map? foo) bar baz)

 would it make sense to make test functions variadic, so if only passed
 a single argument it would return true/false, but could also act as an
 if when passed 3 arguments, eg:

 (map? foo bar baz) would return bar if foo is a map and baz otherwise.

-- 
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: a convenience idea for test functions

2012-04-17 Thread Sean Corfield
On Tue, Apr 17, 2012 at 9:16 PM, Dmitri dmitri.sotni...@gmail.com wrote:
 (map? foo bar baz) would return bar if foo is a map and baz otherwise.

To elaborate on Alan's response, consider:

(if (map? foo) (/ bar 0) baz)

If map? were 'merely' a variadic function, (map? foo (/ bar 0) baz)
would fail because (/ bar 0) would be evaluated and then passed as an
argument, along with foo and baz.

So, no, the simple answer is that you can't just make the test
functions variadic and get the same behavior as an if form (hence
Alan's suggestion of a macro-generating macro to create new macros for
the forms you want).

(Apologies if I'm laboring the point here)
-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.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