Skip to content

Add window function#98

Merged
weavejester merged 1 commit into
weavejester:masterfrom
tomdl89:window
Jun 19, 2025
Merged

Add window function#98
weavejester merged 1 commit into
weavejester:masterfrom
tomdl89:window

Conversation

@tomdl89

@tomdl89 tomdl89 commented Mar 5, 2025

Copy link
Copy Markdown
Contributor

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

@weavejester weavejester left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. My initial impression is that this is reasonable, but let me give it a some thought first.

Comment thread src/medley/core.cljc Outdated
when no collection is provided. For a sliding window containing each element
and n-1 _following_ elements, use `clojure.core/partition` with a `step` size
of 1."
{:added "1.10.0"}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 1.9.0 I believe.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry I assumed 1.9 had been cut. Will correct.

Comment thread src/medley/core.cljc
@tomdl89

tomdl89 commented May 21, 2025

Copy link
Copy Markdown
Contributor Author

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

@weavejester

Copy link
Copy Markdown
Owner

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.

@tomdl89

tomdl89 commented May 21, 2025

Copy link
Copy Markdown
Contributor Author

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

@weavejester

Copy link
Copy Markdown
Owner

That's the one!

@weavejester

Copy link
Copy Markdown
Owner

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

@tomdl89

tomdl89 commented Jun 18, 2025

Copy link
Copy Markdown
Contributor Author

All squished. lmk if you need anything else. Cheers

@weavejester weavejester merged commit 3399e9b into weavejester:master Jun 19, 2025
1 check passed
@tomdl89 tomdl89 deleted the window branch June 19, 2025 13:17
@tomdl89

tomdl89 commented Aug 24, 2025

Copy link
Copy Markdown
Contributor Author

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

@weavejester

Copy link
Copy Markdown
Owner

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

Released 1.9.0. Sorry for the delay.

@tomdl89

tomdl89 commented Aug 29, 2025

Copy link
Copy Markdown
Contributor Author

No worries, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants