Fork me on GitHub
#clojurescript
<
2016-05-11
>
lwhorton00:05:42

does anyone know of a cljs graph datastructure library?

lwhorton00:05:17

i would prefer not to roll my own .. it’s been too long since working with all the graphing algos and such to trust my self

jacob00:05:58

Hi everyone! I was trying to use Dexie.js for indexedDB queries the other day, and I couldn’t figure out how to use it from CLJS, because in order to retrieve the database, you have to call a function on an object that changes its internal state, but does not return itself, like so:

// This is no problem: 
var db = new Dexie('MyDatabase');

// But now the .version method changes the internal state, but does not return itself – how can I hook into this in CLJS?
db.version(1).stores({friends: 'name, age'});
The example is from http://dexie.org. Thanks for any help! Cheers.

ag01:05:00

can someone explain. I’m still struggling to find right way of using figwheel. So I’ve creater script/figwheel.clj using these instructions: https://github.com/bhauman/lein-figwheel/wiki/SASS-watcher now when I call it from cmd

$ rlwrap lein run -m clojure.main --init script/figwheel.clj  -r
it works, but how do I use that script from cider repl? Just evaluating scriptfile’s buffer in repl doesn’t seem to be able to run sass watcher

ag01:05:15

what am I doing wrong

bhauman01:05:34

@ag you can put it in a user.clj that is on you dev classpath

ag01:05:07

the whole content of my figwheel.clj?

bhauman01:05:50

or you can put it on your dev classpath and then require it in your user.clj

bhauman01:05:22

and then call start

bhauman01:05:35

those instructions are for starting from the command line, but it's clojure so put it on your classpath and call it

ag01:05:01

the watcher still doesn’t work 😞

ag01:05:18

maybe it has something to do with java.lang.Runtime.exec

ag01:05:58

yes, watcher doesn’t work if you jack-in. Repl has to be started outside of Emacs and then CIDER needs to be connected to it, otherwise watcher doesn’t work

bostonaholic02:05:11

@jacob: not tested, but something like this should do the trick

(def db (Dexie. "MyDatabase"))

(doto db
  (.version 1)
  (.stores (clj->js {:friends "name, age"})))

jimmy08:05:27

hi guys where can I the Instant write handler of transit in clojurescript ? I couldn't find it in transit-cljs. I would like to write a handler to support Moment object ( to return just date time in javascript then transit date time ).

danielcompton09:05:45

@nxqd: here’s ours for goog UtcDateTime’s

;; 
(def transit-readers
  {:handlers
   {"m" (transit/read-handler (fn [s] (UtcDateTime.fromTimestamp s)))
    "u" uuid}})

(def transit-writers
  {:handlers
   {UtcDateTime (transit/write-handler
                  (constantly "m")
                  (fn [v] (.getTime v))
                  (fn [v] (str (.getTime v))))}})

dimiter13:05:25

Hi, given a Vector of String, what is the easiest way to convert all of those to numbers,

cljs.user=> c
["1" "2”]

dimiter13:05:18

This is the most obvious to m:

(vec (map #(js/parseInt %) c))

rauh14:05:19

@dimiter: I'd probably do (mapv js/parseInt ["1"])

bahulneel14:05:38

@dimiter don't forget the radix parameter or the runtime will "guess" it from the string.

bahulneel14:05:00

also read-string works well

jacob14:05:25

@bostonaholic: Ah, doto, so it was a case of RTFM. Thank you kindly!

risto14:05:40

Does loop-recur do tail call optimization in ClojureScript?

risto15:05:23

I know TCO was removed temporarily in Babel, but that was primarily due to the need to support it globally I think

fasiha15:05:22

@dimiter: watch out for NaN when parseInt tries to parse non-number.

dimiter15:05:42

Yeah, just ran into that when my vector is empty.

bojan.matic15:05:38

@risto: i might be way off here, but I thought loop-recur actually expands to a trampoline?

bojan.matic15:05:01

since it solves recursion in non-TCO hosts...

bahulneel15:05:52

@bojan.matic I believe you're correct.

urbanslug15:05:19

How to write this from JS to clojurescript for (var i = 0; i < localStorage.length; i++){ localStorage.key(i) }

lewix15:05:32

@urbanslug:

(defn local-storage [storage]
  (loop [cljs-storage (js->clj storage)
         i 0]
    (let [length (count cljs-storage)]
     (if (seq cljs-storage) 
       nil
       (if (< i length)
         (.key (clj->js storage) i)
         (recur (rest storage) (inc i)))))))
;;take my untested solution with a grain of salt, I actually never wrote clojurescript before; I'm new to it

urbanslug15:05:19

6ewis: Yo! waddup 😛

bojan.matic15:05:25

(for [i (range 0 (count (js->clj storage))] (.key (clj->js storage) i))

bojan.matic15:05:37

would that not work?

risto15:05:24

@bojan.matic: I thought the trampoline was specific to JVM

risto15:05:32

In any case, wondering if anyone knows for sure

bojan.matic15:05:53

i guess @dnolen knows for sure 😄

bojan.matic15:05:13

but I would be surprised if trampolines weren’t implemented for js host as well

risto15:05:35

likewise, but it might be a little bit harder because you aren't working with bytecode

lewix15:05:58

@urbanslug: I'm good. you?

jr15:05:34

loop/recur compiles to a while loop in javascript

lewix15:05:45

can someone point errors on my code above?

urbanslug15:05:09

6ewis: It takes an arg storage

jr15:05:14

you need to recur with both loop args

urbanslug15:05:16

Where would I get that?

lewix15:05:25

@bojan.matic: the for macro seems so much better though.

jr15:05:27

(recur (inc i) (rest storage))

lewix15:05:41

@jr: good catch

risto15:05:39

@jr Nice snippet, thanks for the confirm

ddellacosta15:05:04

6ewis: didn’t look too carefully but also def -> defn

ddellacosta15:05:19

I also don’t know why folks are using clj->js , I would default to (.-length localStorage) and (.key localStorage …) but maybe I’m old school and missing something

jr15:05:52

do you just want to invoke key on local storage or accumulate the key results?

jr15:05:40

this is equivalent to your first js snippet

(doseq [i (range (.-length localStorage))]
  (.key localStorage i))

lewix15:05:58

@jr I was just answering to a question, I'm not sure what the exact intent was.

ddellacosta15:05:18

yeah, if it’s mutating localStorage something like jr’s solution makes the most sense to me

ddellacosta15:05:30

we don’t care about the return value here as far as I can tell

lewix15:05:08

@jr what about the for macro above

bojan.matic15:05:23

doseq is better, since for is lazy

ddellacosta16:05:05

also, we don’t care about the return value, so for is pointless there too

dnolen16:05:22

@bojan.matic: trampoline exists in ClojureScript

dnolen16:05:41

loop/recur compiles to primitive iteration on JVM & JavaScript

dnolen16:05:13

@urbanslug: (dotimes [i (.-length js/localStorage)] (.key js/localStorage i))

urbanslug16:05:31

6ewis: You still didn’t tell me where to get the storage arg.

urbanslug16:05:38

I guess I should just learn JS

urbanslug16:05:14

Ok dotimes is new. Checks the clojuredocs

dnolen16:05:26

that will compile to JS primitive loop, obviously only for side effects and avoids the needless allocations of for and doseq etc.

lewix16:05:56

@urbanslug: (local-storage js/localStorage)

bojan.matic16:05:57

oh, my bad, i thought trampolines were made magically by loop/recur…using trampoline means you have to write functions that return thunks, right?

dnolen16:05:02

@bojan.matic: not the case, trampoline is for writing mutually recursive code w/o blowing the stack as long as you’re ok with the perf tradeoffs

dnolen16:05:13

not gonna come close to primitive iteration

bojan.matic16:05:41

how would loop/recur handle non-primitive recursion then? or is this a non-issue? 😄

urbanslug16:05:10

I probably should read a JS book and stop guessing at this point but:

(defn get-secret-things
  [name]
  ;; check if logged out if logged-out:
  #?(:cljs
     (.-log (dotimes [i (.-length js/localStorage)] (.key js/localStorage i)))
     (.removeItem (.-localStorage js/window) "secret-things")
     (.-log (dotimes [i (.-length js/localStorage)] (.key js/localStorage i)))

     )
  (fetch-secret-things))

urbanslug16:05:24

Point is I can’t print that

lewix16:05:53

@urbanslug: (dotimes [i (.-length js/localStorage)] (.log js/console (.key js/localStorage i)))

urbanslug16:05:25

Thanks 6ewis

urbanslug16:05:13

it fails. I’ll just read a book or some docs.

lewix16:05:10

@urbanslug: What's the error message?

urbanslug16:05:39

None, just doesn’t give any output.

dnolen16:05:28

@urbanslug: you’re not printing anything in your snippet

dnolen16:05:54

not sure if that was intentional or not

urbanslug16:05:05

My intention was to print.

urbanslug16:05:14

But it has so much guesswork I don’t like it.

dnolen16:05:19

@urbanslug: then you need to print

dnolen16:05:27

(.-log foo) isn’t going to do anything

dnolen16:05:45

it’ll access a property log which probably doesn’t exist and discard it

dnolen16:05:21

@urbanslug: you probably want (println foo)

lewix16:05:02

@urbanslug (dotimes [i (.-length (.-localStorage js/window)] (.log js/console (.key (.-localStorage js/window) i)))

urbanslug16:05:30

Well println refused to work because (enable-console-print!) didn’t work in a cljc file even when I used #?(:cljs (enable-console-print!))

urbanslug16:05:05

Maybe I missed something. Honestly I’m not comfortable with that level of not know what it is that I’m doing.

lewix16:05:50

@urbanslug: try to console print in the repl and see how it goes

urbanslug16:05:43

dnolen: thanks

urbanslug17:05:22

What difference is there between the self hosting clojurescript repl and the non-self hosting one? Does the non-selfhosting one work like online ides or something? I wonder why the other needs to make external server requests. “so no external server requests are necessary."

urbanslug17:05:17

I wonder which is better.

anmonteiro17:05:36

@urbanslug: self-hosted CLJS is exactly what it says, ClojureScript self-hosted in a JavaScript environment. Non-bootstrap needs access to a JVM for compilation

anmonteiro17:05:47

hence the external server requests

urbanslug17:05:07

@anmonteiro: I think I want the JVM calls one because it mirrors what I’m actually doing, don’t you think?

anmonteiro17:05:46

@urbanslug: I can’t assess what you’re doing without knowing simple_smile

urbanslug17:05:29

@anmonteiro: Well compiling clojurescript to js via clojure then to closure simple_smile

urbanslug17:05:43

I assumed that’s what we are all doing.

anmonteiro17:05:01

then you might want the external requests

anmonteiro17:05:24

Bootstrap CLJS has a few shortcomings, limited macro support being one of those

anmonteiro17:05:35

But I don’t really know every exact detail

underplank18:05:15

Hi all. Im building a React Native app with re-natal. an issue i’ve come across is that for some reason it wants to require the .DS_Store as a javascript module? Any idea how to remove that depenency?

underplank18:05:10

nvm. worked it out. The image scanning code in re-natal just grabs anything in the images directory. might have to open a PR for that.

dnolen18:05:40

@urbanslug: self-hosting is really only for people who for some reason can’t leverage the JVM and who can afford to not care about code size

dnolen18:05:49

if these don’t apply to you then I would ignore self-hosting

sbmitchell18:05:15

@dnolen: Do you have a good reference for working with different markup engines within om.next like hiccup?

dnolen18:05:10

@sbmitchell: no I don’t use hiccup or similar libraries, someone else might know though

dnolen18:05:24

@sbmitchell: I would ask in the #C06DT2YSY channel for more opinions

sbmitchell18:05:42

thanks ill check the channel out!

abdullahibra21:05:28

how can share state between different clients?

abdullahibra21:05:17

like (reagent/atom {}), how can i make all clients see the changes on it?, maybe i can use this for simple multi player game

abdullahibra21:05:26

or interaction between clients

nkraft21:05:24

Anyone worked with Clojurescript and the Google Maps API? I've seen Contour, but I need something a bit more full featured, if it's out there. Just looking to avoid writing stuff in JS if I can help it.

darwin21:05:34

@abdullahibra: last time I tried something like this Firebase worked pretty well for me, I used this wrapper: https://github.com/crisptrutski/matchbox

abdullahibra21:05:53

darwin: can you explain how can i hard-code this, the idea i mean

abdullahibra21:05:54

how can i create on universal atom to be shared between all clients, i can use backend like redis to store the state, or even clojure atom, but when i change reagent/atom to clojure atom i don't get any effect

abdullahibra21:05:45

maybe i can use Ajax to make both interact together

abdullahibra21:05:20

client1 do ajax call to the server, and server update state, and then client2 do the same

abdullahibra21:05:45

i don't know if this are going to be effective and efficient

darwin21:05:11

well, firebase does solve this piece

darwin21:05:09

it synchronizes data across clients, but it is up to you to write the glue code which will publish/merge changes into your ratoms or what have you

darwin21:05:41

firebase is not the only option, there are multiple libraries doing this service of real-time synchronization between multiple js clients, some are fully self-hosted (unlike firebase), but I don’t remember their names from top of my head

darwin21:05:49

maybe http://gun.js.org? (a quick google search for self-hosted firebase alternative)

abdullahibra21:05:34

darwin: thanks man