Fork me on GitHub
#clojure
<
2017-10-11
>
xiongtx01:10:45

What’s the easiest way to try out the clj script from brew-install? https://github.com/clojure/brew-install I git cloneed the repo and am not sure where to go from there…

athomasoriginal02:10:07

Are you just trying to get a CLJ environment setup?

xiongtx04:10:43

No. I’m trying to play around w/ @U064X3EF3’s work in brew-install.

Alex Miller (Clojure team)11:10:18

It's in the central tap - just "brew install clojure" for 1.8.0 or "brew install --devel clojure" for 1.9.0-beta1

xiongtx21:10:07

Yeah, found it thanks to @U065JNAN8. Might be useful to link to it in the brew-install README: https://github.com/clojure/brew-install/blob/master/README.md

tbaldridge02:10:04

It’s in brew

tbaldridge02:10:27

So brew install clojure

lovuikeng04:10:11

@lewix being able to partipate in this clojurian channel to witness a vibrant, innovative, cooperative, clojure community, is itself a daily blessing 👍

the2bears05:10:37

At runtime I believe the recommended way is to make calls to 'conform' or 'valid?'

Olical08:10:25

You can use s/fdef to wrap your function up with argument and return value checks.

Olical08:10:44

But these aren't run automatically, you have to call instrement which is in the spec.test namespace I think?

Olical08:10:16

There may be a nice way to enable the s/fdef checks for a single function. https://clojuredocs.org/clojure.spec/fdef

Olical08:10:40

(and you can call s/fdef before you define your function which reads quite nicely)

Olical08:10:19

@blmstrm You may want to try https://github.com/jeaye/orchestra too, it looks pretty great 🙂

blmstrm08:10:16

@olical Thanks! I’ll look into that.

blmstrm08:10:40

@olical What I was hoping to use spec for, even in production, was to avoid alot of validation of incoming data and just define specs for incoming and outgoing values. But the more I read up on it it seems it is not recommended to have it turned on in production.

Olical08:10:36

It's never so much on or off. How I see it: Use fdef all over the place and instrument during dev / tests. If you want to use it a bit like schema and check things at the network boundaries, then you definitely can. Just check things with s/valid?. You can then use s/explain and some of your own code to return helpful error messages.

Olical08:10:24

@blmstrm During development you can call instrument after all of your functions are defined to get checks throughout your system. But I'd recommend only manual checks at the edges in production.

blmstrm08:10:00

Okay. That makes sense. Thanks for taking your time and explaining. What you are saying is exactly what I wanted to do, it just didn’t occur to me that I should create custom validation code with valid?.

Olical08:10:53

Route all of your HTTP in/out through some central namespace, then perform your checks in there. Just a suggestion, it's all very new, so you might find something that works well for you after playing with it 🙂

Bravi09:10:45

oh clojure, why are you so awesome 😄

laujensen10:10:34

If there are any danish clojurians in here who are looking for a job, please send me a message 🙂

mogenslund10:10:34

@laujensen... and is working remotely an option?

laujensen10:10:48

@mogenslund For the right person it probably is yes

xtreak2910:10:02

I have two maps arg and val and two different keys :id in arg and fooid in val that need to be assigned to the var id. Either arg or val can be empty and hence I have used the below code that works but is there a syntax sugar where I can merge the two maps and then destructure them in such a way if one is nil then the other is assigned

(defn query
  [arg val]
  (let [{id :id, track :track} arg
        {id :fooid, :or {id id}} val]
    id))
lacinia-tut.run=> (query {} {:fooid 1}) # :id is nil and hence :fooid is used 
1
lacinia-tut.run=> (query {:id 1} {}) # :fooid is nil and hence :id is used
1
Is there a syntactic sugar to do above in one destructuring call ?

sundarj10:10:19

i would do

(let [id (or (:id arg) (:fooid val))
      {:keys [track]} arg]
  ...)

tomaas11:10:30

hi, i rewrite new FirebaseOptions.Builder() to (new (.-Builder FirebaseOptions)), but get the error Unable to resolve classname: (.-Builder FirebaseOptions). Tried FirebaseOptions/Builder, as well as FirebaseOptions$Builder suggested in #clojurescript channel. What am I doing wrong here?

xtreak2911:10:41

@sundarj It's better compared to using :or . Thanks

tomaas11:10:23

I needed to import (:import com.google.firebase.FirebaseOptions$Builder) additionally

amoe12:10:35

Hello, I'm trying to run this example of annotation interop from "Clojure Programming". But this won't compile for me. It seems that gen-class doesn't create the class, so I get ClassNotFoundException. https://github.com/clojurebook/ClojureProgramming/blob/master/ch09-annotations/src/main/clojure/com/clojurebook/annotations/junit.clj Has something changed since the example was created?

amoe12:10:06

oh, ignore.

amoe12:10:16

I needed to have :aot :all in project.clj.

amoe12:10:29

Or at least for that namespace...

bbss13:10:14

I just made a video on how to use Clojure to interact with the StarCraft II AI API https://www.youtube.com/watch?v=hcYneRzdWV0

alex-dixon13:10:38

I’m still having trouble adding a custom pretty print writer that has all the features of pprint just for IDeref I want it to print #[object …] instead of #<Atom …>. I’m running code through pprint so checking instances on the fly and calling “print” instead of pprint isn’t an option. This may sound superfluous but it’s for a dev tool and I’ve been stuck on it for a couple days 🙂

dominicm13:10:17

@alex-dixon is pprint a multi-method, and you can just override the method for IDeref?

Chris Dewar-English16:10:57

Hi. Any Specter experts here? I'm hitting a few brick walls and need some assistance.

Chris Dewar-English16:10:20

Hi @nathanmarz - very kind of you to help! I have a couple of minor problems. Firstly I'm trying to add a new element to the end of structure but I'm just getting the data added as a value.

Chris Dewar-English16:10:35

e.g. (s/setval [s/ALL :fields :summary s/END] [:zzzz] subResponse)

Chris Dewar-English16:10:08

results in -----> :fields {:summary Setting up the code for blah blah [:zzzz]

Chris Dewar-English16:10:39

When I'd like it to be ----> :fields {:summary Setting up the code for ADA & AOA on Local machine, :zzzz etc.

nathanmarz16:10:52

what's the input structure?

nathanmarz16:10:22

use backticks in slack to get it to render code clearly, e.g. (setval ALL :a data)

Chris Dewar-English16:10:33

It's JSON parsed with Cheshire

nathanmarz16:10:35

can you paste an example?

nathanmarz16:10:01

I can't tell the type of the value for :summary

Chris Dewar-English16:10:14

I want to add another element next to :summary

nathanmarz16:10:01

ah I see, it's a string

nathanmarz16:10:19

when you transform END on a string, you need to provide a string

nathanmarz16:10:33

by providing a vector, it just calls str on that and appends it to the target

nathanmarz16:10:19

you can do ", :zzzz" to get the result you want

Chris Dewar-English16:10:10

Ah ok. And that will create a key :zzzz that I can then select later?

nathanmarz16:10:40

what do you mean? it just gets added to that string at that subvalue

Chris Dewar-English16:10:26

I really just want to add a new element after :summary, not append to the existing value.

Chris Dewar-English16:10:42

A new key and val insert into the structure after :summary

nathanmarz16:10:11

so a new key/value in the "fields" map?

nathanmarz16:10:24

there's no concept of "after" for :summary as its a key in an unordered map

nathanmarz16:10:01

(setval [ALL :fields :zzzz] :some-val data)

Chris Dewar-English16:10:37

Ok, that makes sense!

nathanmarz16:10:42

actually in this case it would be (setval [ALL (keypath "fields") :zzzz] :some-val data) since the keys in your example are strings, not keywords

nathanmarz16:10:08

feel free to drop by the #specter channel if you have more questions

Chris Dewar-English16:10:23

Thank you. I'll switch over now. I have one more question

phreed19:10:07

I am considering writing dynamic versions of import and require based on https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Reflector.java . The idea is to be able to pass the name of the class/method in variables; something like (import-dyn (class-name :as foo)) and static methods as (foo / method-name 5 4 3) or instance methods as (let [x (foo . 5)] (x . method-name 4 3)) where class-name and method-name are strings [require-dyn would be similar]. Has anyone already done something like this?

noisesmith19:10:08

are you sure you need to do anything with require? require is already a function that can take a symbol

phreed20:10:25

@noisesmith you are correct, the following works fine: (let [ans (symbol "clojure.core")] (require [ans :as 'foo]) (foo/+ 5 6))

noisesmith20:10:13

the trick is the part where you call it - since if you do the require inside a function, the compiler will reject a form because the ns is not loaded at the time it is compiled, you can use resolve to work around this though, but it’s clumsy

noisesmith20:10:08

the same issue comes up with import (or just using a dynamically loaded class and method not known at compile time in general) - you have to use clumsier constructs because the most convenient interop forms need the symbols to be known during compilation

noisesmith20:10:59

really, the method being dynamic is harder than the class being dynamic (clojure already does implicit type reflection when it is needed for method calls)

phreed20:10:44

@noisesmith I was thinking a macro. Exemplar (new-dyn ["Lexer'] "lexer" nil) producing something like (let [class-loader (DynamicClassLoader.) lexer-class ^Lexer (.loadClass class-loader "lexer")] (Reflector/invokeConstructor lexer-class (into-array CharStream [nil]))

phreed21:10:52

I had read the documentation for clojure.reflect. It exposes information about the properties of a class but it is not clear how to use it to invoke methods short of invoking eval. It was this https://groups.google.com/d/msg/clojure/YJNRnGXLr2I/jfRCjJOLVHMJ thread that lead me to clojure.lang.Reflector in the first place.

noisesmith22:10:49

Oh, interesting

qqq22:10:39

is there a nicer way to write ((fn [x y z] [x z]) ...) // I feel like there should be a nicer way o say "drop middle elem"

taylor22:10:33

user=> ((juxt first last) [1 2 3])
[1 3]
user=> ((juxt first last) [1 2 3 4])
[1 4]

taylor22:10:50

not sure if that’s any nicer 😆

qqq22:10:24

I guess I'm after a builtin named take101 where the 101 binary encodes which elems I want 🙂

noisesmith22:10:29

also, it’s too bad last is O(n), while peek is O(1) but only gets the last item if the arg is a vector

dpsutton22:10:19

i think (fn [[a _ b]] [a b]) is the nicest so far

dpsutton22:10:28

ie, your original version

noisesmith22:10:31

=> (reduce (fn [v [n el]] (if (zero? (bit-and 5 (bit-shift-left 1 n))) v (conj v el))) [] (map-indexed vector [:a :b :c :d :e]))
[:a :c]

noisesmith22:10:02

that uses 5 as a bit mask to build its collection

noisesmith22:10:39

should probably use (take 64 coll) since that’s your max with a reasonable bit mask

noisesmith22:10:13

(I guess you could use a byte-array as a bit mask for larger inputs, that makes the logic a bit messier)

qqq22:10:52

(defn mt [mask lst] (keep (fn [[x y]] (if (= x 1) y)) (map vector mask lst))) (mt [1 0 1] [:a 😛 :c])

qqq22:10:08

mt stands for "masked take"

noisesmith23:10:01

user=> (mapcat #(when-not (zero? %) [%2]) [1 0 1] [:a :b :c])
(:a :c)

qqq23:10:05

in sider, is there a cmd for "eval last sexp AND INSERT result as text in current buffer" ?

qqq23:10:07

that is neat, usikng [] vs [%2] to encode selection of element

noisesmith23:10:59

yeah, mapcat is more general than typically assumed - it’s really “zero or more outputs for each input item, in order”