practicalli / practicalli/clojure

sum of repeating keywords in a nested map

Open
#76 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Makefile
Stars
117
Forks
36
PR merge metrics
No merged PRs in 30d

Description

how to get sums of repeating keywords in a nested map.
For instance if i have a map:
{:a 1 :c { :a 2}} how do write a function
(f map :a) -> 3
chaow 22 hours ago
imagine the nested map has multiple levels and i am only interested in summing the values of a certain keyword that might be repeating at different nested level of the map, could also be in a vector
chaow 22 hours ago
i tried postwalk but it doesnt seem to be fit for the task
hindol 22 hours ago
I take back my statement. Typed without thinking.
noisesmith 22 hours ago

user=> (->> (tree-seq coll? seq {:a 1 :c { :a 2}}) (filter map-entry?) (filter (comp #{:a} key)) (map val) (apply +))
3

noisesmith 22 hours ago
I think this one is better, does the same thing

user=> (->> (tree-seq coll? seq {:a 1 :c { :a 2}}) (filter map?) (keep :a) (apply +))
3

(edited)
hindol 22 hours ago
Thanks, I was actually thinking about tree-seq. Forgot the name.
chaow 22 hours ago
wow thanks, that works great! my postwalk involved atoms to track a counter but now i see that theres a much simpler way
noisesmith 22 hours ago
tree-seq is great for when I want to process my tree as if it were a graph :D
noisesmith 22 hours ago
that is, if only data types / keys are significant, and not the relative layer in the nest

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start with the nested-map example and the mentioned tree-seq approach; done means documenting or demonstrating a function that sums matching keyword values through nested maps and vectors.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.