practicalli / practicalli/clojure

nested vector into a nested map

Open
#68 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

Does anyone know how to de-structure a nested vector into a map?
For instance ["uno" ["dos" "tres" "cuatro" "cinco"]] --> {:uno {:dos "tres" :cuatro "cinco"}}
The keys (uno, dos, and cuatro) will be different everytime and are unknown ahead of time.
dpsutton 20:20
destructuring is about creating bindings from a shape. it's not really a way to create new shapes of data
andy.fingerhut 20:23
or another way of phrasing that comment is that the term "destructuring" has a particular technical meaning in the Clojure language, and it would be more clear if perhaps you asked how to transform the nested vector into the map you gave as examples.
Mario C. 20:23
@noisesmith Was able to get past the error using the examples you linked, 👍
Andrew Brock 20:23
Not sure what you mean by shape, but the 'format' of the vector will be the same, where as [key [key value key value.....key value]] if that helps?
Mario C. 20:23
Now just need to actually make it work lol
andy.fingerhut 20:25
Are you looking for Clojure code that would handle not just that example, but cases nested even 3 or more levels deep?
20:26
And it should handle arbitrary even numbers of elements in every vector?
Andrew Brock 20:27
I think for now, just that example... I haven't run into an instance (yet) where it goes more than 2 levels deep. And yes, it will always be an even number of elements (strings) in every vector.
bfabry 20:28

user=> (clojure.walk/postwalk #(if (vector? %) (apply hash-map (map-indexed (fn [i k-or-v] (if (odd? i) k-or-v (keyword k-or-v))) %)) %) ["uno" ["dos" "tres" "cuatro" "cinco"]])
{:uno {:cuatro "cinco", :dos "tres"}}

20:28
for fun!
Andrew Brock 20:29
These are the outputs of the carmine package for Redis streams using XRANGE. Everything is added to the stream as a map, but when you read it back, it's a nested vector.
20:30
You sir, are amazing... thank you! I'd been trying different things with walk for hours!

user=> (clojure.walk/postwalk #(if (vector? %) (apply hash-map (map-indexed (fn [i k-or-v] (if (odd? i) k-or-v (keyword k-or-v))) %)) %) ["uno" ["dos" "tres" "cuatro" "cinco"]])
{:uno {:cuatro "cinco", :dos "tres"}}

Posted in #beginners | Yesterday at 20:28 | View message
bfabry 20:34
you're welcome
bfabry 20:46
this problem made me wonder if there's a simple way to un-interleave, seeing as there is an interleave. is there such a thing?
andy.fingerhut 20:48
It seems hard to imagine (if not impossible) to write a lazy Clojure function that would be the inverse of interleave
20:49
but I may be lacking imagination/coffee right now. I am not aware of a single Clojure built in function that would undo interleave , but certainly one could compose a few that would do it.
andy.fingerhut 21:07
e.g. this might be close to what you are thinking: (defn uninterleave [n coll] (map (fn [i] (take-nth n (drop i coll))) (range n))) (edited)
21:08
It isn't written with optimal efficiency in mind, but brevity. e.g. it traverses coll n times.
manutter51 21:16
If the colls you interleave are not all the same length, then interleave is not reversable, since it drops any elements that don’t have corresponding elements in all the other colls, i.e.

(interleave [1 2 3]
[4 6]
[7 8 9 10])
=> (1 4 7 2 6 8)

alexmiller 21:16
isn't it just partition then collect nth 0, nth 1, nth 2 etc from those?
andy.fingerhut 21:32
more than one way to skin that cat, yes.

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, test, or entry point is named. Start by checking whether this resolved discussion is intended to become a documented or coded repository change; completion cannot be defined from the issue alone.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.