Fork me on GitHub
#beginners
<
2018-04-13
>
seancorfield00:04:48

@mfikes Interesting. Clojure's reverse is just (reduce1 conj () coll) -- I wonder whether Clojure/core would accept a patch to make that use rseq on reversible? collections like ClojureScript does?

meguru00:04:03

Hello, I tried to create lwjgl's video player with opencv. So, I learned some example codes for opencv. Next, I wanted to use lwjgl's library with them. But I got some error messages if I add lwjgl's library into project.clj. Here is the project's path (If you want to see error message, please look at project.clj ) https://github.com/MokkeMeguru/clj-lwjgl-vplayer Environment: Manjaro Linux/Leiningen 2.8.1 on Java 1.8.0_162 OpenJDK 64-Bit Server VM Thank you.

meguru00:04:12

How can I get rid of this error?

noisesmith00:04:01

I don't specifically know how to do this with opencv, but the root of the issue is that the java process doesn't see the relevant opencv native library on its native path

noisesmith00:04:41

but if you are using a maven lib that interacts with opencv, that should already be set up for you

meguru00:04:36

That's mean... Is there a package in clojars etc? And should I use it?

noisesmith01:04:45

I wouldn't expect opencv to be on clojars, there's probably something on maven central or nexus though?

meguru01:04:10

I can find some opencv in maven and clojars, so I will try them. Thanks your help!

troglotit13:04:33

is there something like javascript’s object-shorthand let obj = {a, b, c} which desugars to let obj = {a: a, b: b, c: c}?

troglotit13:04:29

I’m passing around a lot of maps which have shape like {:phone-number phone-number}

troglotit13:04:17

it’s somewhat dual to destructuring

moxaj13:04:11

@troglotit (let [{:keys [a b c]} {:a 1 :b 2 :c 3}] ..)

moxaj13:04:40

oh wait you mean the other way?

moxaj13:04:27

no core function which does that, but easy to implement as a macro

troglotit13:04:20

oh, yeah, fair enough. Thanks!

sundarj13:04:21

in fact, in Clojure you can go one step further: you can generate a map from all the locals

noisesmith16:04:05

hey that's my gist 😄

noisesmith16:04:21

I came here to share the same link

sundarj16:04:17

haha sorry for robbing you of the satisfaction 😛

john13:04:12

(let [x ['a 'b 'c]] (zipmap x x))

john13:04:31

;=>{a a, b b, c c}

john13:04:57

or (let [x ['a 'b 'c]] (zipmap (map keyword x) x)) if you want them keywords

🙏 4
rich 8
athomasoriginal13:04:40

oops, the above should be a smiling face...

troglotit13:04:19

you could add and remove emojis clicking on them 😄

soulflyer14:04:43

Got a problem using a clojurescript cider repl, possibly due to me not understanding exactly how require works in the repl. I want to be able to require some of the namespaces of my project rather than type them out in full for every function call. Up until now I have never needed to do this, switching the repl namespace instead. Now I need functions from several of the project namespaces, and require is just not working for me. To illustrate, here I'm calling a constant defined in anh-front.treeusing (def test-string "hello"):

cljs.user> anh-front.tree/test-string
"hello"
cljs.user> (require 'anh-front.tree)
----  Exception    ----

  anh-front.tree does not exist

Why does the fully qualified call to anh-front/test-string work fine, but require doesn't? Should it, or am I doing something stupid here?

john14:04:44

So that can be quite puzzling, yeah

john14:04:20

My theory is that the browser-side compiled artifacts do correctly have the necessary fns. But, for some reason, your local compiler env has 'forgotten' the local context.

soulflyer14:04:42

Any idea how to remind it?

john14:04:57

I sometimes experience a bug (not in cider, but with the figwheel repl in general) where after an initial load, my repl will complain when issuing certain forms. But after I edit my ns and save again, it does a recompile and the local state finally matches the browser's state.

john14:04:21

Some errors you get are coming from your local env

john14:04:31

some are coming from the remote browser env

john14:04:36

so this can confuse you

john14:04:05

Your exception above - I believe that's your local env complaining that it doesn't know those things exist, when your remote end clearly does

john14:04:25

When issuing a fully qualified fn from the repl to the browser, the local repl compiles that down and assumes there's nothing syntactically wrong. If you pass a non-fully qualified name, the local repl env will attempt to resolve it first and things will break

soulflyer14:04:10

I get a subtly different error if i try to load the namespace instead of require it:

cljs.user> (load-namespace 'anh-front.tree)
clojure.lang.ExceptionInfo: anh-front.tree does not exist {:cljs.repl/error :invalid-ns}

john14:04:34

Assuming figwheel is still watching and compiling in the background, if you edit and save the file, do you see indications of a recompile in the browser? And does the loading then work?

john14:04:41

good chance your configuration is tainted somehow

soulflyer15:04:05

looks like the recompile is happening as normal whenever I change a source file

soulflyer15:04:13

But the load never works

soulflyer15:04:13

Woah, stranger and stranger. The original string I defined was (def test-var "hello") and I just called it by the old name by mistake and it gave me an error message and the correct previously defined value!

cljs.user> anh-front.tree/test-var
----  Compiler Warning on   <cljs form>   line:1  column:1  ----

  Use of undeclared Var anh-front.tree/test-var

  1  anh-front.tree/test-var
     ^--- 

----  Compiler Warning  ----
"hello"

john15:04:51

right, so your local env 'forgot' about test-var but it still got passed to the browser and the browser returned "hello", is what I think is happening there

john15:04:43

You've managed to get your environment into a state where the browser and your local env aren't synchronized.

john15:04:18

Sometimes this is the user's fault, sometimes it's bugs in our tools

john15:04:59

Trying to keep those two states - local and remote - synchronized in all possible web development workflows is a difficult task

Will15:04:10

I had a problem where I wanted to query all rows from a table in a database, then generate insert statements to write to a file that I could then take and run on a different database with the same table from the sql file. I am a beginner to clojure and was wondering if anybody could review the way I solved this problem and tell me how to improve it? Or tell me anything weird I’m doing that I could accomplish a much better way

soulflyer15:04:53

@john I think I have seen this before. Sometimes the first thing I do is call re-frame.db/app-db and get back an error message AND the contents of the app-db. The error message usually goes away once I start editing things. Nothing I do is making the require work though. Maybe I should fire up a different repl, no browser, for this part of the job. Thanks for the assistance, I'm one step closer to having a clue 🙂

👍 4
soulflyer17:04:40

@john turned out it just needed the :source-paths filling out correctly. Works fine now.

soulflyer15:04:22

@josmith2016 not sure I fully understand your code, but first thought is wouldn't interpose do a lot of the work for you?

Will15:04:59

What does interpose do?

soulflyer15:04:48

puts a separator between entries of a list I think

soulflyer15:04:16

cljs.core/interpose
 [sep]
 [sep coll]
  Returns a lazy seq of the elements of coll separated by sep.
  Returns a stateful transducer when no collection is provided.

Will15:04:16

Thanks I'll check it out

soulflyer15:04:06

There's also a last fn you might find useful. I think I heard its pretty much reverse and first under the hood but would make the code more readable.

sundarj15:04:00

there's also butlast

dpsutton16:04:31

@josmith2016 (clojure.string/join "," coll) should do exactly what you want

dpsutton16:04:28

(let [statement (str "INSERT " (clojure.string/join ", " ["user_id" "user_name"])
                     " into USERS")]
  (prn statement))
"INSERT user_id, user_name into USERS"

Will16:04:59

Thanks guys! I’ll try all those out

Casey17:04:23

given a hash-map of vectors { :foo [] :bar []} how can I apply map to the vector keyed by :bar only and get back the whole hash-map with an updated :bar?

donaldball17:04:16

(update m :bar (fn [v] (map v f))

Alex Miller (Clojure team)17:04:28

shouldn’t that be (map f v) ?

Casey17:04:30

thanks @donaldball @alexmiller just learned about update that's damn handy

Casey17:04:18

i was trying to do it with assoc but was having trouble 😕

noisesmith17:04:28

@ramblurr see also update-in, assoc-in

Casey17:04:39

hm, after that update/map the vector is now a list

Casey17:04:11

wrapping map in vec would make it a vector again, but is there a better way to do that?

dadair17:04:44

I have a leiningen project with a subproject, how do I add the subproject as a dependency to the parent one, so I can use it as a lein <sub-pro task>?

dadair17:04:16

basically, I’ve followed https://github.com/weavejester/lein-generate to create a new generator (which lives as a leiningen project within the parent project), but I’m not sure how to add the subproject to the parent project in order to be able to do the lein generate <thing>

dadair18:04:57

Whoops, I figured it out, I was adding to :dependencies instead of :generators

hmaurer20:04:32

Is it common practice to have a :type key in clojure maps, with a keyword indicating the “type” of the entity that the map is representing?

hmaurer21:04:41

e.g.

{:type :my-app/person
 :name "John Smith"}

hmaurer21:04:03

I am not just talking about entities that might end up in a database

hmaurer21:04:06

but also internal stuff

hmaurer21:04:11

data flowing through an application

joelsanchez21:04:38

also, I think you'll find that many multimethod definitions just dispatch on :type

hmaurer21:04:07

@joelsanchez yep, I have been doing that a lot and I was wondering if it was “good design” or if I was doing something wrong

Alex Miller (Clojure team)21:04:29

in some cases this is a sign that you could instead use defrecord and dispatch on the type or use protocols

hmaurer21:04:02

@alexmiller mmh, I see. Do you think it would be frowned upon to have a :type key on most maps in the application then? Or it would depend on the use-case?

Alex Miller (Clojure team)21:04:31

there are a lot of tradeoffs in maps vs records

hmaurer21:04:32

I find maps to be nicer to work with than records, but that might be my lack of experience with Clojure

hmaurer21:04:43

As in, I find them more explicit

Alex Miller (Clojure team)21:04:05

I consider both to be idiomatic

hmaurer21:04:06

@alexmiller thank you 🙂 follow-up question: would it be idiomatic to use the derive function to construct a hierarchy of types?

Alex Miller (Clojure team)21:04:33

yes, although fyi most people aren’t even aware that this exists

hmaurer21:04:53

are they not aware that it exists because it’s frowned upon to use it? or just because it’s not well known?

Alex Miller (Clojure team)21:04:15

most people don’t really do “hierarchy” type things

hmaurer21:04:15

The use-case I have is very simple. I want to represent “failures” in my application as maps. A lot of functions can either return some result, or return a “failure” map. There are many types of “failures”, and so I was thinking of deriving them all from a keyword, e.g. :myapp/failure

hmaurer00:04:51

@U61HA86AG oh wow, thank you! it’s funny; the basics of what you just link are almost exactly what I implemented in my project, but it contains a few extra nice functions

hmaurer21:04:34

(I am working on a theorem prover, if that puts some context)

justinlee22:04:11

hey does anybody actually use schema for type checking? I’m kind of confused that there appear to be terminal code in the output. It suggests that there is some way to get better formatting, but I can’t figure out how.