Try this (you need to wrap the return val of helper in lazy-seq also):

(defn pair-sequences-by
  ([seq-1 seq-2 f1 f2]
     "s1 and s2 are guaranteed to be strictly monotonically increasing
whith respect to f1 and f2 as keys respectively.
 The return value is pairs of elements e1 from s1 and e2 from s2 such
that (= (f1 e1) (f2 e2)) is true"
     (let [helper (fn helper [s1 s2]
                    (loop [[x & xs :as wx] s1 [y & ys :as wy] s2]
                      (when (and x y)
                       (let [[xk yk] (map #(%1 %2) [f1 f2] [x y])]
                         (let [cval (compare xk yk)]
                           (cond
                            (< cval 0) (recur xs wy)
                            (= cval 0) (cons [x y] (lazy-seq (helper xs ys)))
                            (> cval 0) (recur wx ys)))))))]
       (lazy-seq (helper seq-1 seq-2))))
  ([seq-1 seq-2 f] (pair-sequences-by seq-1 seq-2 f f)))

On Fri, Feb 24, 2012 at 11:36 PM, Sunil S Nandihalli
<sunil.nandiha...@gmail.com> wrote:
> Hi Everybody,
>  Can somebody comment if the following piece of code is truely lazy?
>
>
>
>
> (defn pair-sequences-by
>
>
>
>   ([seq-1 seq-2 f1 f2]
>
>
>
>      "s1 and s2 are guaranteed to be strictly monotonically increasing whith
> respect to f1 and f2 as keys respectively.
>
>
>
>  The return value is pairs of elements e1 from s1 and e2 from s2 such that
> (= (f1 e1) (f2 e2)) is true"
>
>
>
>      (let [helper (fn helper [s1 s2]
>
>
>
>                     (loop [[x & xs :as wx] s1 [y & ys :as wy] s2]
>
>
>
>                       (when (and x y)
>
>
>
>                        (let [[xk yk] (map #(%1 %2) [f1 f2] [x y])]
>
>
>
>                          (let [cval (compare xk yk)]
>
>
>
>                            (cond
>
>
>
>                             (< cval 0) (recur xs wy)
>
>
>
>                             (= cval 0) (cons [x y] (helper xs ys))
>
>
>
>                             (> cval 0) (recur wx ys)))))))]
>
>
>
>        (lazy-seq (helper seq-1 seq-2))))
>
>
>
>   ([seq-1 seq-2 f] (pair-sequences-by seq-1 seq-2 f f)))
>
>
>
>
>
>
> https://gist.github.com/e9f6c0ad9bbe535d2d54
>
>
>
>
>
>
> I am running out of ram when I use this.. so any help would be greatly
> appreciated.
>
>
>
> Thanks,
> Sunil.
>
> --
> 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: lazyness Mark Rathwell

Reply via email to