medley icon indicating copy to clipboard operation
medley copied to clipboard

Add window function

Open tomdl89 opened this issue 8 months ago • 6 comments

I've had the lazy-only version of this function in quite a few codebases over the years, and recently took the time to write the transducer version. The cljs support is particularly annoying to write because cljs's array-list type doesn't implement a remove method.

This is like a simple alternative to https://github.com/cgrand/xforms/ 's window transducer, which needs f and invf functions to construct the windows. Plus this has a standard lazy-arity.

An example use case:

(def months [{:month "Jan" :income 500}
             {:month "Feb" :income 450}
             {:month "Mar" :income 580}
             {:month "Apr" :income 420}
             {:month "May" :income 680}])

(->> (window 3 months)
     (map (fn [months]
            {:month (:month (last months))
             :last-3-avg (/ (reduce (fn [acc x] (+ (:income x) acc)) 0 months)
                            (count months))})))
;; => ({:month "Jan", :last-3-avg 500}
;;     {:month "Feb", :last-3-avg 475}
;;     {:month "Mar", :last-3-avg 510}
;;     {:month "Apr", :last-3-avg 483}
;;     {:month "May", :last-3-avg 560})

tomdl89 avatar Mar 05 '25 00:03 tomdl89

Sorry for the delay @weavejester - I've corrected the added metadata, and assume the benchmarking is satisfactory? Cheers

tomdl89 avatar May 21 '25 09:05 tomdl89

I probably won't be able to look at this in depth for a couple of weeks due to conference organizing. I wanted to let you know that this is on my radar though.

weavejester avatar May 21 '25 11:05 weavejester

No worries. reClojure by any chance? If so, I'll see you there!

tomdl89 avatar May 21 '25 12:05 tomdl89

That's the one!

weavejester avatar May 21 '25 12:05 weavejester

I think all looks good. Can you squish down the commits?

weavejester avatar Jun 17 '25 20:06 weavejester

All squished. lmk if you need anything else. Cheers

tomdl89 avatar Jun 18 '25 13:06 tomdl89

Hi @weavejester can you cut a release when you get a chance please?

tomdl89 avatar Aug 24 '25 23:08 tomdl89

Hi @weavejester can you cut a release when you get a chance please?

Released 1.9.0. Sorry for the delay.

weavejester avatar Aug 29 '25 21:08 weavejester

No worries, thank you!

tomdl89 avatar Aug 29 '25 21:08 tomdl89