Fork me on GitHub
#beginners
<
2019-10-02
>
iha202:10:07

I trying to get the values of a protocol definition from another namespace and there is no success.

iha202:10:12

Can someone help me with this?

iha202:10:37

How does one accomplish this?

iha202:10:10

I am using clojurescript by the way.

seancorfield03:10:22

@iha2 Not sure what you mean by "the values of a protocol definition" -- perhaps you can share (some of) your code in a Gist or Pastebin link?

iha203:10:39

(defrecord Nav [text link nav])

(def articles (map-indexed #(Nav. (->> % second :title) '(::article {:id (->> % first)}) nil)
                           (:articles @app-state)))

iha203:10:00

The other namespace has …

iha203:10:18

(js/console.log (:text (first nav-tree)))

seancorfield03:10:44

What's nav-tree?

seancorfield03:10:16

Also, since you are defing articles, it will be assigned a value when that namespace loads and it will not change after that.

iha203:10:47

Thats fine.

iha203:10:00

Basically I need to able to do this (:text value) where value is the (Nav. "root" ::root nil) in another namespace.

seancorfield03:10:45

OK... So I'm not sure what you're asking... what are you having problems with?

seancorfield03:10:55

I don't see any protocols in that code.

seancorfield04:10:33

It would probably help you to use the auto-generated constructor function ->Nav instead of trying to use the type constructor Nav.

seancorfield04:10:31

When you require the namespace that contains the defrecord, it will give you access to ->Nav and map->Nav etc but in order to get at the type Nav, you need to import it, just like any other native types.

seancorfield04:10:02

I suspect you're getting an error (you didn't say what error you're seeing) because you're not importing the record type?

seancorfield04:10:16

Like this

user=> (ns first.one)
nil
first.one=> (defrecord Nav [text link nav])
first.one.Nav
first.one=> (ns second.thing (:require [first.one :as one]))
nil
second.thing=> (one/->Nav "root" ::root nil)
#first.one.Nav{:text "root", :link :second.thing/root, :nav nil}
second.thing=> 
@iha2

seancorfield04:10:08

Otherwise you'd need this:

first.one=> (ns second.thing (:import (first.one Nav)))
nil
second.thing=> (Nav. "root" ::root nil)
#first.one.Nav{:text "root", :link :second.thing/root, :nav nil}
second.thing=> 

seancorfield04:10:41

(I'm assuming it works the same way in cljs 🙂 )

iha204:10:40

I really apologize. I meant defrecord.

iha204:10:46

So sorry for the confusion.

iha204:10:47

the namespace thing is apparently not necessary in clojurescript.

Ilayda Bakare14:10:09

Hello, I need some help! At the minute it returns a vector and I'd like it to return a string separated by a ,

andy.fingerhut14:10:19

It looks like it is returning a string. Note that it starts with ", and all of the other occurrences of " you show are escaped by preceding them with a \

andy.fingerhut14:10:48

What does (load-csv-file ...) return?

andy.fingerhut15:10:40

That is a list whose first (and only) element is a vector of strings.

andy.fingerhut15:10:23

To get what you want, something like (str/join "," (first (load-csv ...))) would return what you want.

Ilayda Bakare15:10:37

Yup, I cut it but its a list of vectors in a csv. Essentially I need it to return just a string so I can pass that to other functions

andy.fingerhut15:10:12

What functions do you want to pass it to, that require strings?

Ilayda Bakare15:10:19

what I'm trying to do is replace the (slurp filepath) part

andy.fingerhut15:10:09

It looks like perhaps load-csv returns a similar kind of value that csv/read-csv might? Or do they return different kinds of data structures?

andy.fingerhut15:10:38

Turning all of your data into a string in memory, just to parse it again, seems odd

Ilayda Bakare15:10:45

I'm integrating code someone else has written into my project, and it needs to use the load-csv format to be consistent with the rest of my project essentially

Ilayda Bakare15:10:30

If I can get the load-csv-file function to do what I want I can move on with the rest of my task!

andy.fingerhut15:10:16

Understood. Sorry, I am not aware of the differences in detail between load-csv and read-csv. Someone else might know.

Ilayda Bakare15:10:49

no worries, thanks for getting back to me anyway!

andy.fingerhut15:10:37

It seems that if you do really want to create a string of your CSV data, and reparse it, perhaps you want to do (str/join "\n" (map #(str/join "," %) (load-csv ...))). The map will create comma-separate strings of each element of your list (where each of those elements is a vector), then the outer str/join will concatenate those together, with newlines between them.

mzavarella14:10:34

@ilayda It seems like your load-csv-file function is loading the csv as a vector of things and then applying the str function to that vector. If you read the csv file in as a vector and do not coerce it, then you can call (clojure.string/join "," my-vec) and get the desired `"username,password,..." output

mzavarella14:10:00

something like this should do it for all of the rows. you can adapt this to do it for only a specific number of rows or whatever you'd like

(with-open [reader ( "my-csv-file.csv")]
  (->> (clojure.data.csv/read-csv reader)
          ;; (take n) ;; uncomment this to take 'n' number of rows, you'll have to define 'n' somewhere
          (map #(clojure.string/join "," %))))

Ramon Rios16:10:07

Hello guys. How are u doing?

Ramon Rios16:10:02

I'm a beginner at clojurescript with re-frame. I'm having a few issues using bidi to route my views

Ramon Rios16:10:42

core.cljs:3901 re-frame: no subscription handler registered for: :active-panel. Returning a nil subscription.

Ramon Rios16:10:52

Error rendering component (in poc_cljs.views.main_panel)

dpsutton16:10:03

(rf/reg-sub
  :active-panel
  (fn [db _]
    (:active-panel db)))
you probably want something like this

Ramon Rios16:10:42

This piece of code gets the variable and put it on a db?

lepistane18:10:57

hi guys i am using figwheel-main template for simple site i am hosting it on github pages which require me to put compiled version there github pages require index.html to be on top lvl but in my case it's in Is there a way to move this to top level for development and also github pages?

practicalli10:10:57

I created an deps.edn alias to generate a single minified .js file in /docs/js directory and manually copied any html, css and other static resources to /doc. The static assests dont change much, so can just run Clojure CLI tools alias to create a new version of the ClojureScript application. See the end of this article for examples: http://jr0cket.co.uk/2019/08/development-workflow-with-clojure-cli-tools.html

dpsutton18:10:48

just copy it? the html is most likely dead simple and doesn't change? it's just a call to app.js or whatever, right?

lepistane18:10:11

but paths need to be changed in css and also the script

lepistane18:10:04

guess not ... oki this works @dpsutton thanks

smp18:10:06

Hi everyone If afrit is a macro, how do I get it to expand within the let definition? (defn cfrit [ks] (let [ (afrit ks int32) ])())

smp18:10:51

I'm trying to write a macro to expand to name (get-type handle) so the example above would expand to let [ ks (get-int32 handle) ] (...)

smp18:10:07

but macroexpansion seems to see (afrit ks int32) as a single form and let requires an even number of forms within the [] or it throws an error

smp18:10:10

thanks for any help

noisesmith18:10:13

`(let [[email protected](afrit ks int32)] ...)
- something like this

noisesmith18:10:56

[email protected] when inside ` unwraps a sequence as individual children of the parent

smp18:10:43

No luck I'm afraid with that Syntax error macroexpanding clojure.core/let at (core.clj:1273:5). [(clojure.core/unquote-splicing (afrit ks int32))] - failed: even-number-of-forms? at: [:bindings] spec: :clojure.core.specs.alpha/bindings

smp18:10:08

1271 (defn cfrit 1272 [ks] 1273 (let [ [email protected](afrit ks int32) ])())

noisesmith18:10:11

what does afrit actually return?

noisesmith18:10:15

no, that's wrong

noisesmith18:10:26

you can't use ~ outside `

noisesmith18:10:51

and you can't do this in a defn correctly, this needs to be a defmacro

smp18:10:25

oh, this is looking promising, let me see if I can rework this.

smp18:10:46

@noisesmith thanks for your help, that's given me a pointer I needed, I appreciate it.

smp18:10:54

for reference 1255 (defn gass 1256 [ty] 1257 (symbol (str "bb-" ty))) 1258 ... 1266 1267 (defmacro afrit 1268 [ks ts] 1269 `(symbol ks) ((gass ts)))

hiredman19:10:37

that will never work

hiredman19:10:32

1. macros expand in to a single form (just like functions have a single result), and you are trying to have your macro expand in to two 2. you cannot use a macro in the binding forms of let like that

hiredman19:10:26

#2 is partially because of #1 and partially due to macros expanding top down