On Thu, 2 Sep 2010 05:54:26 -0700 (PDT)
Glen Rubin <rubing...@gmail.com> wrote:

> I defined a fn whose execution depends on the number of parameters
> passed to it.  It works fine!  I am just concerned that I am doing
> this in a way that is not as concise as it could or should be.  Here
> is my fn:
> 
> (defn make-target
> 
> "Parses text file for single sweep specified by sweepidx.  If size is
> not specified will parse till end of sweep.  If startidx and size are
> not spefified will parse from beginning to end"
> 
> ([file channel sweepidx]
>   (let [collated-target1 (graph-part file channel sweepidx (+ 1
> sweepidx))
>       ;break open double parens
>       collated-target2 (apply identity collated-target1)]
>     collated-target2))
> 
> ([file channel sweepidx startidx]
>   (let [collated-target1 (graph-part file channel sweepidx (+ 1
> sweepidx))
>       collated-target2 (apply identity collated-target1)
>       ;;apply starting point
>       collated-target3 (drop startidx collated-target2)]
>     collated-target3))
> 
> ([file channel sweepidx startidx size]
>   (let [collated-target1 (graph-part file channel sweepidx (+ 1
> sweepidx))
>       collated-target2 (apply identity collated-target1)
>       ;apply starting point and size parameters
>       collated-target3 (take size (drop startidx collated-target2))]
>     collated-target3)))
> 

The variants already posted - using the shorter argument list variants
to provide default arguments to the longer ones - is the idiomatic
approach. I missed those and saw this alternative, and figured it was
worth posting for comparison, if nothing else:

(defn make-target

"Parses text file for single sweep specified by sweepidx.  If size is
not specified will parse till end of sweep.  If startidx and size are
not spefified will parse from beginning to end"

([file channel sweepidx]
  (let [collated-target1 (graph-part file channel sweepidx (+ 1 sweepidx))
        ;break open double parens
        collated-target2 (apply identity collated-target1)]
    collated-target2))

([file channel sweepidx startidx]
        (drop startidx (make-target file channel sweepidx)))

([file channel sweepidx startidx size]
       (take size (make-target file channel sweepidx startidx))))

-- 
Mike Meyer <m...@mired.org>             http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

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

Reply via email to