Re: Like if, but it composes functions

2013-02-20 Thread Laurent PETIT
Hello,

Why the names fix / to-fix ?

2013/2/20 Alan Malloy a...@malloys.org:
 Useful has functions that do this and more: fix or to-fix, according to
 taste. Your iffn is just the three-argument case of to-fix: (def magnify
 (to-fix pos? inc dec)). But fix and to-fix accept more or fewer arguments as
 well, so that (fix x pos? inc) is like (if (pos? x) (inc x) x), and (to-fix
 tall? shorten thin? fatten) is (fn [x] (cond (tall? x) (shorten x) (thin? x)
 (fatten x) :else x)).

 Basically both of these functions look through their clause pairs and apply
 the first transform whose test matches. fix takes its focus argument
 immediately, while to-fix returns a lambda that performs the requested
 operation.


 On Tuesday, February 19, 2013 9:53:57 PM UTC-8, James MacAulay wrote:

 Sometimes I find myself writing code like this:

 (defn magnify [n] (if (pos? n) (inc n) (dec n)))

 ...and I want to get rid of all those ns. I've looked for a macro like
 this, but couldn't find it, so I wrote it:

 https://gist.github.com/jamesmacaulay/4993062

 Using that, I could re-write the above like this:

 (def magnify (iffn pos? inc dec))

 I can imagine a condfn macro, too:

 (def magnify2 (condfn pos? inc
   neg? dec
   :else identity)

 Has this kind of conditional function composition been explored much? I
 couldn't find anything like it in the standard library, but maybe I wasn't
 looking hard enough.

 Cheers,
 James

 --
 --
 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: Like if, but it composes functions

2013-02-20 Thread Alan Malloy
You can use fix to take some data that might not be right (say, an integer 
that might actually be a string) and fix it by applying read-string: (fix 
10 string? read-string). to-fix returns a function you can use to fix 
things.

On Wednesday, February 20, 2013 12:06:36 AM UTC-8, Laurent PETIT wrote:

 Hello, 

 Why the names fix / to-fix ? 

 2013/2/20 Alan Malloy al...@malloys.org javascript:: 
  Useful has functions that do this and more: fix or to-fix, according to 
  taste. Your iffn is just the three-argument case of to-fix: (def magnify 
  (to-fix pos? inc dec)). But fix and to-fix accept more or fewer 
 arguments as 
  well, so that (fix x pos? inc) is like (if (pos? x) (inc x) x), and 
 (to-fix 
  tall? shorten thin? fatten) is (fn [x] (cond (tall? x) (shorten x) 
 (thin? x) 
  (fatten x) :else x)). 
  
  Basically both of these functions look through their clause pairs and 
 apply 
  the first transform whose test matches. fix takes its focus argument 
  immediately, while to-fix returns a lambda that performs the requested 
  operation. 
  
  
  On Tuesday, February 19, 2013 9:53:57 PM UTC-8, James MacAulay wrote: 
  
  Sometimes I find myself writing code like this: 
  
  (defn magnify [n] (if (pos? n) (inc n) (dec n))) 
  
  ...and I want to get rid of all those ns. I've looked for a macro 
 like 
  this, but couldn't find it, so I wrote it: 
  
  https://gist.github.com/jamesmacaulay/4993062 
  
  Using that, I could re-write the above like this: 
  
  (def magnify (iffn pos? inc dec)) 
  
  I can imagine a condfn macro, too: 
  
  (def magnify2 (condfn pos? inc 
neg? dec 
:else identity) 
  
  Has this kind of conditional function composition been explored much? I 
  couldn't find anything like it in the standard library, but maybe I 
 wasn't 
  looking hard enough. 
  
  Cheers, 
  James 
  
  -- 
  -- 
  You received this message because you are subscribed to the Google 
  Groups Clojure group. 
  To post to this group, send email to clo...@googlegroups.comjavascript: 
  Note that posts from new members are moderated - please be patient with 
 your 
  first post. 
  To unsubscribe from this group, send email to 
  clojure+u...@googlegroups.com javascript: 
  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+u...@googlegroups.com javascript:. 
  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: Like if, but it composes functions

2013-02-20 Thread Laurent PETIT
2013/2/20 Alan Malloy a...@malloys.org:
 You can use fix to take some data that might not be right (say, an integer
 that might actually be a string) and fix it by applying read-string: (fix
 10 string? read-string). to-fix returns a function you can use to fix
 things.

OK, I thought there was some more generic meaning to it (for the OP's
initial need, I'm not sure the name fix would convey appropriate
semantics, for instance)

Cheers



 On Wednesday, February 20, 2013 12:06:36 AM UTC-8, Laurent PETIT wrote:

 Hello,

 Why the names fix / to-fix ?

 2013/2/20 Alan Malloy al...@malloys.org:
  Useful has functions that do this and more: fix or to-fix, according to
  taste. Your iffn is just the three-argument case of to-fix: (def magnify
  (to-fix pos? inc dec)). But fix and to-fix accept more or fewer
  arguments as
  well, so that (fix x pos? inc) is like (if (pos? x) (inc x) x), and
  (to-fix
  tall? shorten thin? fatten) is (fn [x] (cond (tall? x) (shorten x)
  (thin? x)
  (fatten x) :else x)).
 
  Basically both of these functions look through their clause pairs and
  apply
  the first transform whose test matches. fix takes its focus argument
  immediately, while to-fix returns a lambda that performs the requested
  operation.
 
 
  On Tuesday, February 19, 2013 9:53:57 PM UTC-8, James MacAulay wrote:
 
  Sometimes I find myself writing code like this:
 
  (defn magnify [n] (if (pos? n) (inc n) (dec n)))
 
  ...and I want to get rid of all those ns. I've looked for a macro
  like
  this, but couldn't find it, so I wrote it:
 
  https://gist.github.com/jamesmacaulay/4993062
 
  Using that, I could re-write the above like this:
 
  (def magnify (iffn pos? inc dec))
 
  I can imagine a condfn macro, too:
 
  (def magnify2 (condfn pos? inc
neg? dec
:else identity)
 
  Has this kind of conditional function composition been explored much? I
  couldn't find anything like it in the standard library, but maybe I
  wasn't
  looking hard enough.
 
  Cheers,
  James
 
  --
  --
  You received this message because you are subscribed to the Google
  Groups Clojure group.
  To post to this group, send email to clo...@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+u...@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+u...@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.



-- 
-- 
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: Like if, but it composes functions

2013-02-20 Thread James MacAulay
Ben: of course, haha...making it a macro seems rather silly now :P

Alan: I didn't know about useful before, thanks for the pointer! fix and 
to-fix look great.

-- 
-- 
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: Like if, but it composes functions

2013-02-20 Thread Jonathan Fischer Friberg
Function composition similar to that has been explored a lot in the haskell
world. See:

http://www.haskell.org/haskellwiki/Arrow

I also made a small library to implement some of the operators:

https://github.com/odyssomay/clj-arrow

I think the reason arrows are so interesting in haskell is because they
generalize monads.
However, in clojure I have found them to make code harder to write/read
rather than easier,
so I kind of gave up the concept after a while (and haven't updated the
library). Although
it's possible that they are actually highly useful and I've just missed
something.

Jonathan


On Wed, Feb 20, 2013 at 3:55 PM, James MacAulay jmacau...@gmail.com wrote:

 Ben: of course, haha...making it a macro seems rather silly now :P

 Alan: I didn't know about useful before, thanks for the pointer! fix and
 to-fix look great.

 --
 --
 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: Like if, but it composes functions

2013-02-19 Thread Ben Wolfson
You don't need a macro for this:

user (defn conditionalize [pred then else]
(fn [ args] (if (apply pred args)
 (apply then args)
 (apply else args
#'user/conditionalize
user ((conditionalize pos? inc dec) 3)
4
user ((conditionalize pos? inc dec) -3)
-4
user (def magnify (conditionalize pos? inc dec))
#'user/magnify
user (magnify 3)
4

On Tue, Feb 19, 2013 at 9:53 PM, James MacAulay jmacau...@gmail.com wrote:
 Sometimes I find myself writing code like this:

 (defn magnify [n] (if (pos? n) (inc n) (dec n)))

 ...and I want to get rid of all those ns. I've looked for a macro like
 this, but couldn't find it, so I wrote it:

 https://gist.github.com/jamesmacaulay/4993062

 Using that, I could re-write the above like this:

 (def magnify (iffn pos? inc dec))

 I can imagine a condfn macro, too:

 (def magnify2 (condfn pos? inc
   neg? dec
   :else identity)

 Has this kind of conditional function composition been explored much? I
 couldn't find anything like it in the standard library, but maybe I wasn't
 looking hard enough.

 Cheers,
 James

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





-- 
Ben Wolfson
Human kind has used its intelligence to vary the flavour of drinks,
which may be sweet, aromatic, fermented or spirit-based. ... Family
and social life also offer numerous other occasions to consume drinks
for pleasure. [Larousse, Drink entry]

-- 
-- 
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: Like if, but it composes functions

2013-02-19 Thread Alan Malloy
Useful has functions that do this and more: fix or to-fix, according to 
taste. Your iffn is just the three-argument case of to-fix: (def magnify 
(to-fix pos? inc dec)). But fix and to-fix accept more or fewer arguments 
as well, so that (fix x pos? inc) is like (if (pos? x) (inc x) x), and 
(to-fix tall? shorten thin? fatten) is (fn [x] (cond (tall? x) (shorten x) 
(thin? x) (fatten x) :else x)).

Basically both of these functions look through their clause pairs and apply 
the first transform whose test matches. fix takes its focus argument 
immediately, while to-fix returns a lambda that performs the requested 
operation.

On Tuesday, February 19, 2013 9:53:57 PM UTC-8, James MacAulay wrote:

 Sometimes I find myself writing code like this:

 (defn magnify [n] (if (pos? n) (inc n) (dec n)))

 ...and I want to get rid of all those ns. I've looked for a macro like 
 this, but couldn't find it, so I wrote it:

 https://gist.github.com/jamesmacaulay/4993062

 Using that, I could re-write the above like this:

 (def magnify (iffn pos? inc dec))

 I can imagine a condfn macro, too:

 (def magnify2 (condfn pos? inc
   neg? dec
   :else identity)

 Has this kind of conditional function composition been explored much? I 
 couldn't find anything like it in the standard library, but maybe I wasn't 
 looking hard enough.

 Cheers,
 James


-- 
-- 
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: Like if, but it composes functions

2013-02-19 Thread Alan Malloy
Sorry, forgot to link to 
useful: 
https://github.com/flatland/useful/blob/develop/src/flatland/useful/fn.clj#L30

On Tuesday, February 19, 2013 9:53:57 PM UTC-8, James MacAulay wrote:

 Sometimes I find myself writing code like this:

 (defn magnify [n] (if (pos? n) (inc n) (dec n)))

 ...and I want to get rid of all those ns. I've looked for a macro like 
 this, but couldn't find it, so I wrote it:

 https://gist.github.com/jamesmacaulay/4993062

 Using that, I could re-write the above like this:

 (def magnify (iffn pos? inc dec))

 I can imagine a condfn macro, too:

 (def magnify2 (condfn pos? inc
   neg? dec
   :else identity)

 Has this kind of conditional function composition been explored much? I 
 couldn't find anything like it in the standard library, but maybe I wasn't 
 looking hard enough.

 Cheers,
 James


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