Fork me on GitHub
#beginners
<
2019-01-22
>
N/A00:01:54

How do you save and read sorted maps to a file in ClojureScript? ClojureScript:

=> (binding [*print-dup* true]
  (prn (sorted-map :foo 1)))
{:foo 1}
Clojure:
(binding [*print-dup* true]
  (prn (sorted-map :foo 1)))
; #=(clojure.lang.PersistentTreeMap/create {:foo 1})
https://stackoverflow.com/questions/17347836/savingreading-sorted-maps-to-a-file-in-clojure

v3ga05:01:20

Why does (def blah [range 10]) have a length of 1 and (defn blah (range 10)) gives you 10 values? I understand that range is a lazy seq so it’s only going to give you what’s necessary. Just wondering it wouldn’t give you the full range in a vector…

Alex Miller (Clojure team)06:01:47

[range 10] is a literal vector with the function range and the number 10 (should have a count of 2)

Alex Miller (Clojure team)06:01:38

if you want a vector with the contents of the range, you could do (vec (range 10))

v3ga06:01:13

thanks alex. after I asked i noticed my confusion and yes it should have been 2.

borkdude09:01:32

Inspired by yesterday’s discussion about str/join, I wrote a spec for it: https://github.com/borkdude/speculative/blob/master/src/speculative/string.cljc#L8

dazld09:01:21

is there a clojure equivalent of JSON.stringify(data, null, 4)?

dazld10:01:00

ie, output formatted data as a string

borkdude10:01:13

or do you mean pprint?

borkdude10:01:04

cljs.user=> (require '[clojure.pprint :refer [pprint]])
nil
cljs.user=> (with-out-str (pprint {:a {:b {:c 1}}}))
"{:a {:b {:c 1}}}\n"
cljs.user=> (with-out-str (pprint (range 100)))
"(0\n 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n 10\n 11\n 12\n 13\n 14\n 15\n 16\n 17\n 18\n 19\n 20\n 21\n 22\n 23\n 24\n 25\n 26\n 27\n 28\n 29\n 30\n 31\n 32\n 33\n 34\n 35\n 36\n 37\n 38\n 39\n 40\n 41\n 42\n 43\n 44\n 45\n 46\n 47\n 48\n 49\n 50\n 51\n 52\n 53\n 54\n 55\n 56\n 57\n 58\n 59\n 60\n 61\n 62\n 63\n 64\n 65\n 66\n 67\n 68\n 69\n 70\n 71\n 72\n 73\n 74\n 75\n 76\n 77 \n 78 \n 79 \n 80 \n 81 \n 82 \n 83 \n 84 \n 85 \n 86 \n 87 \n 88 \n 89 \n 90 \n 91 \n 92 \n 93 \n 94 \n 95 \n 96 \n 97 \n 98 \n 99)\n"

dazld10:01:33

ah! that’s how to get it!

dazld10:01:50

thank you 🙂

lepistane11:01:13

Hello! are there any blogs/resources for working with huge files of different types? I am getting byteArrayInputStream and need to convert it to /tmp and i use for that. But when files get bigger that function is stuck until file is processed i tried future and (.start (Thread. #(io/copy..))) but without luck i was also thinking about implementing atom which will hold if io/copy is done. I need anything to just deal and work with files that are not edn/string based cause that's all i am finding on google

noisesmith19:01:45

do you need the whole file to exist on disk? if you use a thread or future you shouldn't end up blocking the parent thread unless you attempt to deref or join

noisesmith19:01:11

you can use realized? to check if a future has completed

lepistane12:01:37

@U051SS2EU sorry maybe i should've explained myself bit more i will receive reference to ByteArrayInputStream and my job is to save it to a disk using as minimal amount of RAM/heap as possible. Those input streams might be few GBs big and they can't be realized or fully in memory. Is there a way to process this stream (or any kind of stream) 'lazily' (for the lack of better word) or in cunks. I want first 50mb > save to disk > second 50mb > save to disk until stream is empty This way i would use 50mb of ram per 'save'

noisesmith17:01:01

a read/write loop would do this, but that is what io/copy should be doing internally

Sy Borg12:01:47

I have some source code examples with a deps.edn file in the root. IJ Idea can't import it. What do I need to do? Convert it to a Leiningen project somehow?

borkdude12:01:32

do you use Cursive? ask in #cursive

Ali Ebadian17:01:00

Hey all, is there an intuitive formatting that you guys use?

Ali Ebadian17:01:02

ps I know SPC m f b in Spacemacs formats the code, but I find the format hard to read

seancorfield17:01:34

@ali.ebadian What do you find hard to read about that default formatting? And what would make it more readable for you?

seancorfield17:01:09

Most people (and most tooling) follows this community style guide https://github.com/bbatsov/clojure-style-guide

defenestrated19:01:17

hi all, quick question: i’m going through Lacinia’s tutorial (for working with graphQL), and I came across this let structure that’s a bit confusing:

(defn resolver-map
  []
  (let [cgg-data (-> (io/resource "cgg-data.edn")
                     slurp
                     edn/read-string)
        games-map (->> cgg-data
                       :games
                       (reduce #(assoc %1 (:id %2) %2) {}))]
    {:query/game-by-id (partial resolve-game-by-id games-map)}))

defenestrated19:01:03

the part i’m confused by is the map that’s returned: i’m 90% sure the goal is to define a function in the :query namespace, but why would that be done inside a map?

defenestrated19:01:30

instead of, say, (defn :query/game-by-id (...))

noisesmith19:01:47

that's an illegal defn

noisesmith19:01:10

The hash map is a key and a value, where the value is a function. If new functions are created / looked up at app runtime, it's appropriate to use a hash for lookup instead of creating new defs at runtime

dpsutton19:01:31

it's not defining a function in a namespace. its returning a map with the key :query/game-by-id whose value is a function

defenestrated19:01:26

so that hash map would be defined “globally” i.e. outside the let, right?

noisesmith19:01:37

it's returned by the resolver-map function, it doesn't create anything at global or even namespace scope

dpsutton19:01:27

(ps, love the username)

defenestrated19:01:54

hah thanks… i’ve been using it since i was like 13 playing counterstrike, which is embarrassing for all kinds of reasons

😂 15