Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a utility method for deep conversion from java collections #89

Open
viebel opened this issue Feb 19, 2021 · 4 comments
Open

add a utility method for deep conversion from java collections #89

viebel opened this issue Feb 19, 2021 · 4 comments

Comments

@viebel
Copy link

viebel commented Feb 19, 2021

Thank you for this great library that makes good stuff from Clojure available to Java developers.

In the context of my book Data-Oriented programming, I am considering to use pcollections to illustrate to Java developers the power of data immutability when building a software system.

A fundamental principle of Data-Oriented programming is to represent data with generic immutable data structures instead of instances of classes.

As an example, a book in a library management system (that's the example I use in the book) is represented with a nested map combining maps and lists:

Map watchmen = Map.of(
                      "isbn", "978-1779501127",
                      "title", "Watchmen",
                      "publicationYear", 1987,
                      "authors", List.of("alan-moore", "dave-gibbons"),
                      "bookItems", List.of(
                                           Map.of(
                                                  "id", "book-item-1",
                                                  "rackId", "rack-17",
                                                  "isLent", true
                                                  ),
                                           Map.of (
                                                   "id", "book-item-2",
                                                   "rackId", "rack-17",
                                                   "isLent", false
                                                   )
                                           )
                      );

Now, I'd like to represent the same data as an immutable collection.

The only way I found to recursively convert my nested map into persistent collection is by explicitly converting every map and list.

Map watchmen = HashTreePMap.from(Map.of(
                      "isbn", "978-1779501127",
                      "title", "Watchmen",
                      "publicationYear", 1987,
                      "authors", TreePVector.from(List.of("alan-moore", "dave-gibbons")),
                      "bookItems", TreePVector.from(List.of(
                                           HashTreePMap.from(Map.of(
                                                  "id", "book-item-1",
                                                  "rackId", "rack-17",
                                                  "isLent", true
                                                  )),
                                           HashTreePMap.from(Map.of(
                                                   "id", "book-item-2",
                                                   "rackId", "rack-17",
                                                   "isLent", false
                                                   ))
                                           ))
                      ));

Two questions:

  1. Is there a way to make this conversion less verbose?
  2. Is there a way to recursively converts a map into a pmap?
@hrldcpr
Copy link
Owner

hrldcpr commented Feb 24, 2021

Hey thanks for the question. Currently HashTreePMap and TreePVector only have singleton() convenience methods, nothing for more entries.

I think adding multi-entry methods a la Map.of and List.of is a good idea, so I've opened an issue specifically for that #90 (PRs welcome!)

As for automatically converting nested maps, I'm less certain if that warrants being built in to the library, so I'll close this for now but feel free to argue for it being reopened 😉

Sorry that functionality isn't already in the library, good luck with your project!

@viebel
Copy link
Author

viebel commented Feb 25, 2021

Thank you @hrldcpr.

Regarding converting nested maps, do you think it's feasible to write code that does that? What would it take? Probably kind of dynamic castings to check whether an entry is a collection or not? Am I correct?

On a related note: what would it take to parse a (deeply nested JSON) into a pcollection?

@hrldcpr
Copy link
Owner

hrldcpr commented Mar 3, 2021

Yeah I guess in both cases you'd write a recursive function, something like PCollection deepCopy(Collection c) which checks the type of c to decide what type of PCollection to make, then iterates through the elements of c adding them to the new PCollection—but calling deepCopy first if the element is a Collection, otherwise just adding it directly

Wouldn't be that much code, just a case for each collection type (List, Map, Set). I guess given the popularity of JSON maybe this is a useful thing to have built-in to the library… So I'll rename this issue accordingly. To be honest though I probably won't get to it any time soon, but I'll mark it as "good first issue" and maybe someone else will!

@hrldcpr hrldcpr changed the title Handling nested maps add a utility method for deep conversion from java collections Mar 3, 2021
@viebel
Copy link
Author

viebel commented Mar 4, 2021

That makes sense. Thank you!

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

No branches or pull requests

2 participants