Fork me on GitHub
#beginners
<
2017-03-10
>
yonatanel09:03:02

@adamkowalski It might be enough to implement the few interfaces mentioned here: https://clojure.org/reference/data_structures Until a better, more accurate advice comes along, you can study some of Zach Tellman's implementations: https://github.com/ztellman/clj-radix/blob/master/src/clj_radix.clj https://github.com/ztellman/clj-tuple/blob/master/src/clojure/lang/PersistentUnrolledMap.java Or this example: https://gist.github.com/david-mcneil/1684980

lepistane09:03:28

what do you do when u cant load namespace

lepistane09:03:04

CompilerException java.lang.Exception: namespace 'picture-gallery.routes.auth' not found, compiling:(cider-repl picture-gallery:51:7)

jumar09:03:44

@lepistane when does this happen? If you try to eval buffer in Emacs?

lepistane10:03:59

well i am reading book WebDev with Clj 2nd edition and i am at chapter 8 making picture-gallery we are supposed to make authentication so i made new file auth.clj in routes folder and tried to test register user function i made but i cant it get that error i used cider-browser-ns and i cant see routes.auth namespace why is that?

jumar10:03:25

try to eval auth namespace first

jumar10:03:57

to double check you can always lein run, lein test, or lein repl to make sure that there are no errors

lepistane10:03:52

i did nothing changed, i tried to restart server to save files nothing

lepistane10:03:15

lein repl doesnt recognise the auth NS i created

jumar10:03:58

hm, that's strange. Are you sure that you created the namespace in a proper directory? I'm just looking at picture-gallery-a from book's code examples and the namespace is "picture-gallery.routes.services.auth" (with "service") but it really doesn't matter much

lepistane10:03:16

i didnt get that part

lepistane10:03:30

so i just put auth in routes folder

lepistane10:03:38

because services is a file that has namespace

lepistane10:03:13

not a folder

lepistane10:03:34

yea that's what i thought. it shouldnt matter

jumar10:03:20

just to make it clear: - in the book the file structure is as follows: src/clj/picture-gallery/routes/services/auth.clj -> you should declare namespace picture-gallery.routes.services.auth in this case - if you created file src/clj/picture-gallery/routes/auth.clj then you should declare namespace picture-gallery.routes.auth and be able to use it

lepistane10:03:50

k i am blind

lepistane10:03:11

error in process filter wrong type argument characterp nil getting this error while evaluating NS

urbanslug10:03:05

Hey I want something like "1245" that I want to make "12:45"

urbanslug10:03:11

I'm thinking of using regex

urbanslug10:03:32

Not sure but the point is insert colon as the 3rd last char in string

lepistane10:03:14

@urbanslug not sure about elegance but something like take (let [s "1245"] (apply str (first s) (second s) ":" (rest s))) might work i am sure there are bugs or things u have to fix but that's my idea

urbanslug10:03:31

@lepistane Yeah not too elegant (clojure.string/replace "1234" #"..$" #(str %1)) I have this which is close

urbanslug10:03:38

I just need to find a way to add the colon

urbanslug10:03:56

(clojure.string/replace "1234" #"..$" #(str ":" %1))

urbanslug10:03:02

@lepistane LOL phantom js says "undefined is not a constructor" even thought I've used the function just above this use. 🙂 will try your way. clj to cljs fails

not-raspberry11:03:57

@urbanslug No need to use replace when substing is enough.

not-raspberry11:03:00

Yes, substring. Typo.

lepistane11:03:12

i made it work!! how? well bottom up approach function was making error so it couldnt load NS tnx for help everyone

kauko13:03:43

(macro-expand-1 '(defn-with-defaults foobar {:inc 1} [deps b] (+ (:inc deps) b))
-->
(defn foobar
  ([b] (foobar {:inc 1} b))
  ([deps b] (let [deps (merge {:inc 1} deps)] (+ (:inc deps) b))))

kauko13:03:53

I'm trying to write that kind of macro

kauko13:03:29

I'm new to writing macros, but I think I'd need a macro that captures a variable on purpose (so it can add that overwriting let expression), but I also need splicing-unquote, which I think is only available when syntax quoting

kauko13:03:43

but syntax quoting doesn't let me capture variables

kauko13:03:27

(defmacro defn-with-defaults
  [name defaults params & body]
  `(defn ~name
    (~(vec (rest params)) (~name ~defaults ~@(rest params)))
    (~params (let [deps (merge ~defaults ~(first params))]
               ~@body))))

kauko13:03:32

So this wouldn't work..

schmee13:03:35

kauko ~‘foo allows variable capture

kauko13:03:17

So keep that as it is, but (let [~'deps ..?

schmee13:03:53

actually, I think what you want here is gensym

schmee13:03:08

~‘deps should work, but I don’t think it is a good idea to write macros with magic variables like this

schmee13:03:14

gimmie a sec

kauko13:03:11

(defn-with-defaults increase {:inc 1} [deps a] (+ (:inc deps) a))
=> #'clojure.core/increase
(increase 1)
=> 2
(increase {:inc 5} 1)
=> 6

kauko13:03:48

Nevermind the clojure.core namespace, I accidentally switched to that namespace instead of my own core ns when trying this out in the REPL 😄

kauko13:03:38

But that is what I want. Just imagine the function actually does something sensible, and gets for example a db connection in the deps parameter

kauko13:03:11

I don't think gensym helps me here, because the way I understand this is I need to have that let block in the function body

kauko13:03:26

and it wouldn't do much if it was deps# instead of deps

schmee13:03:43

here’s the problem:

kleinheit.extractor=> (macroexpand-1 '(defn-with-defaults foobar {:inc 1} [something b] (+ (:inc something) b)))
(clojure.core/defn foobar ([b] (foobar {:inc 1} b)) ([something b] (clojure.core/let [deps (clojure.core/merge {:inc 1} something)] (+ (:inc something) b))))

schmee13:03:32

[something b] looks like a parameter list, but it doesn’t function the same way

schmee13:03:45

which will be very confusing a couple of weeks later

kauko13:03:01

Yeah, I actually realised that. I'd be fine with it. Do you have any ideas how to avoid that though?

kauko13:03:26

(I deleted some messages). I guess it would be better if the let-binding bound to the same symbol as the first parameter

joshkh13:03:31

quick repl question!. why can't i see the gen namespace in this scenario? if i lein repl then (use 'ms.specs) and then try to call gen i get Unable to resolve symbol: gen in this context:

(ns ms.specs
  (:require [clojure.spec :as s]
            [clojure.spec.gen :as gen]))

schmee13:03:12

kauko yeah

schmee13:03:41

but IMO macros like these tend to be more trouble than they’re worth

schmee13:03:01

I’d just type it out 🙂

kauko13:03:05

Haha could be, I'm just learning 🙂

kauko13:03:24

It's a personal project, I'm allowed to be cute dangit! 😄

schmee13:03:24

for that purpose it’s completely fine 👍

schmee13:03:04

first you should know how to to it, and then you should know when not to do it 😄

kauko13:03:30

I wonder if it's even possible to get this to work with destructuring though

kauko13:03:36

I guess I can access the :as definition like from a map.. and make the macro fail if there is none? I dunno

grounded_sage15:03:35

I don't seem to be doing this right. Does anyone know how to invoke the playbackRate method?

grounded_sage15:03:36

#?(:cljs (.playbackRate (. js/document (getElementById "my-video")) 0.6))

kauko15:03:00

Are you using React (Reagent, Rum...)? If yes, getElementById probably doesn't work.

kauko15:03:46

Have you made sure the code is actually finding and returning the element?

grounded_sage15:03:16

I am using Rum

grounded_sage15:03:33

If I get the component how to do the function on it

kauko15:03:28

Don't know off the top of my head. That looks pretty close. You probably need to use a built-in function in Rum to access the React component, then call .playbackRate on it 🙂

kauko15:03:47

I'm sure you can get help at #rum. I see you've already asked other questions there :thumbsup:

grounded_sage15:03:07

Was just trying to not flood it with nooby questions haha

kauko15:03:12

Hehe 🙂

grounded_sage15:03:50

@kauko is there a standard way to set a property in Rum/Reagent etc?

not-raspberry17:03:53

@joshkh: because namespaces are not seen as objects. So gen will not work but gen/vector will.

user=> (require '[clojure.spec.gen :as gen])
nil
user=> gen

             java.lang.RuntimeException: Unable to resolve symbol: gen in this context
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: gen in this context, compiling:(/tmp/form-init7595209781886275963.clj:1:8637)
user=> gen/any
#<Fn@10f353c7 clojure.spec.gen/eval31156[any]>

joshkh17:03:26

is anyone here good with spec? 🙂 i've used gen/generate to create bunch of maps with UUIDs and i'd like to generate (using spec) vectors containing those UUIDs.

joshkh17:03:52

ha, thanks again

alexis18:03:44

Does anyone know if there is an equivalent to python's newspaper library in clojure? It looks like enlive might be the right place for me to look (https://github.com/swannodette/enlive-tutorial). Are there others?