2

Say a function returns a large and deeply nested hashmap. By which means do you study it's structure? Printing to the console is confusing, using clojure.pprint/pprint makes it a bit better but still does not give you an overview.

How would you handle it in order to understand it's data structure?

Anton Harald
  • 5,772
  • 4
  • 27
  • 61
  • 3
    Have you tried https://clojure.github.io/clojure/clojure.inspector-api.html#clojure.inspector ? – cfrick Mar 23 '16 at 10:01
  • sounds promising, doesn't work on my machine though. It just creates a new blank window. That might be due to the fact, that I'm using a window manager (?).. A browser alternative could work better.. – Anton Harald Mar 23 '16 at 10:21
  • @AntonHarald are you sure it does not work ? It worked for me on Linux and OSX with Clojure 1.7 and 1.8. It uses Swing under the hood so should work everywhere Swing does. – nha Mar 23 '16 at 10:45
  • yes the window manager is the problem. have a look for the tool `wmname` and look up what a good setting is for your setup (i use LG3D for awesomewm) – cfrick Mar 23 '16 at 10:53
  • I'm using awesomewm as well. After naming my wm via wmname to "LG3D" (inspect "sometest") still just creates an empty new white window /client in my windowmanager. In the window I cannot do anything, no right click etc.) Or would I have to restart the wm? – Anton Harald Mar 23 '16 at 11:00
  • getting OT here, but this is a well known problem. you have to search the web for how to fix it for your window manager version (another "fake" name can help). – cfrick Mar 23 '16 at 11:11
  • I'm always using CIDER's value-inspection https://github.com/clojure-emacs/cider#value-inspection . It's vary useful/convenient for me and/or other CIDER users. – ayato_p Mar 24 '16 at 01:25
  • this looks like it could be the exactly the thing I was looking for. I see the inspected object in a new buffer when hitting the suggested key combination in emacs. However all the further key-combinations for navigating the structure don't have any effect. Do you know, why this could happen? – Anton Harald Mar 24 '16 at 09:19

1 Answers1

2

There are several ways to inspect a map :

  • clojure.inspector (as @cfrick noted)

    (require '[clojure.inspector :as inspector])
    (inspector/inspect {:a 1 :b 2}) ;; #object[javax.swing.JFrame 0x1b730837 "javax.swing.JFrame[frame0,4,23,400x400,invalid,layout=java.awt.BorderLayout,title=Clojure Inspector,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,22,400x378,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]"]
    
  • clojure pretty-printing, note that you can customize it's behaviour. Also, don't forget that it easy to keep only the keys you want with select-keys or dissoc, if you want to remove unwanted values.

Community
  • 1
  • 1
nha
  • 17,623
  • 13
  • 87
  • 133
  • 1
    For large hash map, `inspector/inspect-tree` looks better. – ntalbs Mar 23 '16 at 10:59
  • @ntalbs I just put this example in case in the unlikely event he was calling the functions with bad parameters. Good point, though. – nha Mar 23 '16 at 11:01
  • 1
    comes up in this fine video (https://www.youtube.com/watch?v=QI9Fc5TT87A) along with other useful bits of clojure – BWStearns Mar 23 '16 at 14:15
  • 1
    @BWStearns This is where I saw it first, but I could not find it back ! – nha Mar 23 '16 at 14:27