Re: Idiomatic usage of partial

2012-05-17 Thread Tim Visher
On Wed, May 16, 2012 at 1:57 PM, Murtaza Husain
murtaza.hus...@sevenolives.com wrote:
 What is the idiomatic use of partial. I understand it helps create closures,
 however the reader notation also allows to the same. So when should partial
 be use dover other forms of creating functions.

I have no idea if it is idiomatic or not but I've come to use partial
whenever I want to use a function with the leading arguments fixed.
The only times I use the `#` reader macro is when I need to fix an
argument that's after the argument I want to map over or utilize. I
find partial to signal my intent better in the cases where I'm trying
to say give me a new function with leading arguments fixed vs. the
reader macro which doesn't really signal my intent at all other than
that I want an anonymous function that may do anything at all. Also
partial clearly indicates that I'm doing a higher-order operation on
an existing function which could lead to less cognitive overload, I
suppose, when reading the code later on whereas the reader macro sort
of has to be read thoroughly in order to understand what I was trying
to do.

Just my 2 cents.

-- 
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: Idiomatic usage of partial

2012-05-17 Thread Tim Visher
On Wed, May 16, 2012 at 1:57 PM, Murtaza Husain
murtaza.hus...@sevenolives.com wrote:
 What is the idiomatic use of partial. I understand it helps create closures,
 however the reader notation also allows to the same. So when should partial
 be use dover other forms of creating functions.

Also, for the sake of spamming of group (^_^), I find that many of the
times I've elected to use `#` to define a truly anonymous function, as
in a function that isn't building on something else, per se, but is
just being used in the local context to do something specific, I
almost always end up pulling it out later at least into a let-fn form
if not into a top level form. It's a little like leaving the `{}` off
of a one line if statement. It saves a little typing, but over the
long haul it almost always seems to come back to bite you.

-- 
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: Idiomatic usage of partial

2012-05-17 Thread greg r
The reader notation is limited to the arity of the number of arguments 
provided.  partial allows variable arity.
Check out pages 67-68 of Clojure Programming.

Regards,
Greg

On Wednesday, May 16, 2012 1:57:40 PM UTC-4, Murtaza Husain wrote:

 Hi,

 What is the idiomatic use of partial. I understand it helps create 
 closures, however the reader notation also allows to the same. So when 
 should partial be use dover other forms of creating functions.

 Thanks,
 Murtaza



-- 
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: Idiomatic usage of partial

2012-05-17 Thread Alan Malloy
#() syntax can accept as many arguments as you like. For example, you
can define partial using #():

(defn partial [f  args]
  #(apply f (concat args %)))

On May 16, 7:13 pm, greg r gsra...@bellsouth.net wrote:
 The reader notation is limited to the arity of the number of arguments
 provided.  partial allows variable arity.
 Check out pages 67-68 of Clojure Programming.

 Regards,
 Greg







 On Wednesday, May 16, 2012 1:57:40 PM UTC-4, Murtaza Husain wrote:

  Hi,

  What is the idiomatic use of partial. I understand it helps create
  closures, however the reader notation also allows to the same. So when
  should partial be use dover other forms of creating functions.

  Thanks,
  Murtaza

-- 
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


Idiomatic usage of partial

2012-05-16 Thread Murtaza Husain
Hi,

What is the idiomatic use of partial. I understand it helps create 
closures, however the reader notation also allows to the same. So when 
should partial be use dover other forms of creating functions.

Thanks,
Murtaza

-- 
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: Idiomatic usage of partial

2012-05-16 Thread Jim - FooBar();
One of the best examples of partial that I've seen is in the 
debug-repl...look it up on github and see how it is being used there in 
the main function that starts the debug repl...basically every time you 
invoke the debug-repl within the same repl session you're going to have 
different local bindings...so partial takes care of returning a function 
that has all your bindings precollected...I don't know if I'm saying 
this right...if you leave the debug-repl but some time afterwards you 
want to enter it again your locals will be re-bound to your current ones...


Hope that helps...

Jim

On 16/05/12 18:57, Murtaza Husain wrote:

Hi,

What is the idiomatic use of partial. I understand it helps create 
closures, however the reader notation also allows to the same. So when 
should partial be use dover other forms of creating functions.


Thanks,
Murtaza

--
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: Idiomatic usage of partial

2012-05-16 Thread Jay Fields
Someone once mentioned that partials are more performant. i.e. (partial
println foo) is better than #(println foo %). I can't remember why,
something about creating classes under the covers, I believe. Hopefully
someone can chime in.

Personally, I used to use #(... %) due to it being less characters. I
didn't really feel strongly about it, and once someone mentioned that
partial was 'better' I switched. However, 'partial' is a bit long, so I
always (def % partial) - and my code ends up looking like (% println foo)

Cheers, Jay

On Wed, May 16, 2012 at 1:57 PM, Murtaza Husain 
murtaza.hus...@sevenolives.com wrote:

 Hi,

 What is the idiomatic use of partial. I understand it helps create
 closures, however the reader notation also allows to the same. So when
 should partial be use dover other forms of creating functions.

 Thanks,
 Murtaza

  --
 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: Idiomatic usage of partial

2012-05-16 Thread Stuart Sierra
Every literal instance of `fn` or `#()` compiles to a new class definition. 
This is only at compile time: once the code is running, each execution of 
the `fn` expression merely creates an instance of that class.

partial is implemented in terms of `fn`, so every usage of `partial` merely 
creates an instance of a class that already exists. So using `partial` 
instead of `fn` or `#()` saves a small amount of memory.

-S

-- 
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: Idiomatic usage of partial

2012-05-16 Thread Alan Malloy
On May 16, 2:05 pm, Stuart Sierra the.stuart.sie...@gmail.com wrote:
 Every literal instance of `fn` or `#()` compiles to a new class definition.
 This is only at compile time: once the code is running, each execution of
 the `fn` expression merely creates an instance of that class.

 partial is implemented in terms of `fn`, so every usage of `partial` merely
 creates an instance of a class that already exists. So using `partial`
 instead of `fn` or `#()` saves a small amount of memory.

Note also that the tradeoff goes both ways - (partial f x) can't know
how many more args to expect, so it has to create a vararg function,
use apply, and all that jazz - a lambda can just accept exactly the
right number of args and use them directly. So there's some runtime
penalty for using partial.

Of course, in both cases the difference is miniscule - use whatever
makes your intent clearer.

-- 
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