Fork me on GitHub
#beginners
<
2018-02-16
>
lilactown01:02:20

does anyone know why ( "") would work in CIDER, but fail in an uberjar/running lein repl with a 403 error from the server?

oliver08907:02:55

Could somebody explain what the ^ is doing here

(def giants
  (with-meta 'Giants
    {:league "National League"}))

(meditations
 "Or more succinctly with a reader macro"
 (= __ (meta '^{:division "West"} Giants))

oliver08907:02:14

(tough one to google 🙂 )

gonewest81807:02:23

It’s setting that hash-map as metadata on Giants

oliver08907:02:52

yeah, gotcha

oliver08907:02:04

I feel like clojure is turning my mind into a parser 😉

serge07:02:59

(no it-s turning your mind into-god-mode)

oliver08907:02:34

haha, haven't written any macros yet but I can see that if you can just grab any piece of code, traverse it and modify it then yeah, god-mode definitely applies!

oliver08907:02:04

Would it be outrageous to use macros to add side effects? i.e. keep your program pure and then mix in the side effects afterwards

oliver08907:02:17

Are there any clojure implementations of katas that have been written in an idiomatic style? Would be great to read some simple code that's written idiomatically.

serge07:02:24

So I don't use macros in my hobby projects. It's so difficult to me )

serge07:02:40

I use only functions, maps, vectors and so on 🙂

serge07:02:47

Keep it simple!

oliver08907:02:07

ha, yeah, don't worry I'll use them sparingly 🙂 I'm curious how side effects are handled in clojure though, are there some idioms for how to separate them from your pure code?

gklijs07:02:10

@oliver089 in the core are some nice examples, most functions like merge, are either macro’s or functions calling other functions

(defn merge
  "Returns a map that consists of the rest of the maps conj-ed onto
  the first.  If a key occurs in more than one map, the mapping from
  the latter (left-to-right) will be the mapping in the result."
  [& maps]
  (when (some identity maps)
    (reduce #(conj (or %1 {}) %2) maps)))

oliver08907:02:01

ah that's a good call, I'll have a read

oliver08907:02:41

I take it that once code has been evaluated it's harder to grab the code? i.e. I can't do

`merge
and get the original source

oliver08907:02:49

well (source merge) works for the repl

andy.fingerhut09:02:35

For characters in Clojure that are difficult to Google, one reference is this page: https://clojure.org/guides/weird_characters

andy.fingerhut09:02:47

Once you mostly know what they are, there is a section of the Clojure Cheatsheet in the bottom left that has them in a more compact form: https://clojure.org/api/cheatsheet or I like this form of it a bit better: http://jafingerhut.github.io/cheatsheet/clojuredocs/cheatsheet-tiptip-no-cdocs-summary.html

oliver08911:02:23

@ thanks, that's very useful!

timok10:02:10

hi, I just read in the cljs-ajax repo this sentence: "More generally, it's time to move away from EDN on the browser..." why? I wanted to use edn for my webapp but now I don't know. basically I struggle with keywords, that need constant parsing, because in json-format they are always stringified...

sundarj10:02:13

the better thing to use for sending data to the browser is Transit, which supports all the types and extensibility EDN does, but encodes to and from JSON: http://blog.cognitect.com/blog/2014/7/22/transit

timok10:02:57

ok, thanks

abdullahibra12:02:29

if i have list of records like this: [{:x 10 :y 0} {:x 4 :y 7} {:x 0 :y 10}], what i would like to achieve is: get all records which not satisfy x != y, i have done it linear by 2 loops but this isn't efficient if i have much records, is there any suggestion in this

delaguardo12:02:42

Sound like you need to use filter for that

(filter (fn [{:keys [x y]}] (not= x y)) [{:x 10 :y 0} {:x 4 :y 7} {:x 0 :y 10}])

abdullahibra12:02:32

i want comparing x and y from the same and other records

abdullahibra12:02:07

[{:x 10 :y 0} {:x 4 :y 7} {:x 0 :y 10}] for this example: {:x 4 :y 7} should be the result

abdullahibra12:02:33

because first and last are satisfy condition

abdullahibra12:02:49

but imagine we have 50k records for example

abdullahibra12:02:45

so the worst solution is: have 2 filters which iterate for each record and compare with the whole list

abdullahibra12:02:55

but that's very slow

delaguardo12:02:00

(let [data (repeatedly 50000 (fn [] {:x (rand-int 40000) :y (rand-int 40000)}))
      xs (set (map :x data))
      ys (set (map :y data))]
  (filter (fn [{:keys [x y]}]
            (and (not (contains? xs y))
                 (not (contains? ys x))))
          data))

rcustodio15:02:48

Does anyone uses rabbitmq with clojure?

pcbalodi15:02:43

@rcustodio yeah I have used langohr for rabbitmq integration

pcbalodi19:02:58

No, I had a requirement of using mutiple channels actually, so I didnt know about this issue at all 😄

rcustodio20:02:55

What do you mean? Each consumer with its channel or 1 consumer have several channels?

teachtyler15:02:31

is there a loop in clojure similiar to [1,2,3].map(x => x) in js in terms of simplicity

chris15:02:40

(map #(+ % %) [1 2 3])

ben.borders18:02:28

are there any clojure protobuf libraries that support protobuf 3.0 and higher?

josmith201619:02:01

Is there a way to convert a clojure map to json in clojure?

noisesmith19:02:07

there’s libraries - cheshire and clojure.data.json are both popular

tjtolton19:02:10

it would look something like that @josmith2016 via https://github.com/dakrone/cheshire

xhve4lf3w719:02:26

I just did (ns my.ns (require [c.c.matrix] [c.c.matrix.linear])) and it works fine. But when I also require [c.c.matrix.select] I get java.io.FileNotFoundException: Could not locate clojure/core/matrix/select__init.class or clojure/core/matrix/select.clj on classpath. I am using leiningen and vectorz-clj. Does anyone know how I might fix this?

seancorfield19:02:24

@xhve4lf3w7 Looks like it should be [clojure.core.matrix.selection], not select.

xhve4lf3w719:02:25

@seancorfield Indeed. I went by the API docs (https://mikera.github.io/core.matrix/doc/index.html) but didnt pay attention to the version (0.44.0). Well thank you!

tkjone21:02:24

If I have a multi arity function like this

(defn messenger
  ([]        (do-something 1)  (do-something 2))
  ([blah]  (do-something 1) (do-something 3)))
You will notice I am calling the same function with the same argument in case 1 and 2 (do-something 1). Is there a way to just call that function no matter what without manually specifying it in each arity list?

sundarj22:02:36

with a macro? 😛

brandon.ringe22:02:36

You could consolidate the multiple arities into one function with one optional parameter, then call (do-something 1), check if blah is nil?, call (do-something 2) if it is, else call (do-something 3)

brandon.ringe22:02:34

The parameter list would look like [& [blah]]

tkjone13:02:42

I saw that was an option, but I read through this https://stuartsierra.com/2015/06/01/clojure-donts-optional-arguments-with-varargs and it seems to go against the above.

manutter5122:02:32

It’s a common convention in Clojure to have something like

(defn foo-fn
  ([] (foo-fn nil))
  ([bar] (do-something 1) (do-something 2)))
where the lower-arity function calls the higher-arity function with some reasonable defaults

manutter5122:02:50

So that avoids the problem you’re asking about

manutter5122:02:45

But in the specific case you mention, I think that’s kind of a code smell — if it’s doing different things, it arguably should be different functions

wushee22:02:22

looks like pattern matching, no?

noisesmith22:02:07

arity matching is just about the only kind of pattern matching we have natively

brandon.ringe22:02:13

Hello all, I'm new here. I decided instead of always bugging my only friend who knows clojure, I'd try asking community members. I need to create a function that accepts multiple optional arguments. For a caller to be able to just pass values for some parameters but not others, would it be best to use a hash-map parameter and take the values from it? If so, is there a way in the function declaration to destructure the values from the hash-map into named keys, especially so that a caller could see the optional parameter names in intellisense. Here's is the signature I started to write for two optional parameters, but realized this was a code smell because they are ordered, optional params.

(defn get-orders
  [& [statuses product-id]]
  ( ))

andy.fingerhut22:02:13

The "Associative Destructuring" section of this Clojure guide shows how to do destructuring of maps. This can be done in function argument definitions, as well as let/loop/etc.: https://clojure.org/guides/destructuring

sundarj22:02:57

@brandon.ringe there are two main ways of doing this, either by passing an actual hash-map to the function, or by using 'named' arguments syntax:

user=> (defn foo [{:keys [a b]}] (+ a b))
#'user/foo
user=> (foo {:a 2 :b 3})
5
user=> (defn bar [& {:keys [a b]}] (+ a b))
#'user/bar
user=> (bar :a 2 :b 3)
5

noisesmith22:02:42

named arguments are somewhat unpopular because they are harder to compose

brandon.ringe23:02:48

Thanks for the help :+1: