Fork me on GitHub
#beginners
<
2018-03-15
>
josmith201600:03:18

I have a vector maps called my-map like this [{:guid 123, :amount 100} {:guid 124, :amount 200}] I want to get a vector that looks like this [123 124], I tried this (map my-map [:guid]) but I’m getting the error IllegalArgumentException Key must be integer. Is there a different way to accomplish this when the value is a string rather than a number?

noisesmith00:03:18

@josmith2016 "my-map" isn't a map, it's a vector

noisesmith00:03:27

(map :guid my-map)

noisesmith00:03:05

or if you have a string instead of a keyword, (map #(get % s) v)

josmith201600:03:24

Duh that's right, thanks @noisesmith

lee.justin.m04:03:14

it is equivalent to (deref response)

lee.justin.m04:03:33

it means response is an atom and you want the value of the atom

lee.justin.m04:03:29

actually i lied. this is something from http-kit? it’s doing something I’m not familiar with

lee.justin.m04:03:11

okay so it’s a clojure promise, not an atom: https://clojuredocs.org/clojure.core/promise

suryapjr08:03:43

Scripting is fun with clojure !

suryapjr09:03:08

s Market)                                    4                                                5 (println "Preparing your basket")              6                                                7 (println "What do you want to add in your bask 8                                                9 (def basket [])                               10                                               11 (def item1 (read-line))                       12
13 (def c1 (conj basket item1))
14
15 (println "Anything else ?")
16
17 (println "Press q to quit ")
18
19 (def yo (read-line))
20
21 (if (= yo "q") System/exit 0) 

suryapjr09:03:06

My code ..is giving error : Unable to find static field exit

christian76709:03:11

It’s a function

christian76709:03:15

(System/exit 0)

christian76709:03:32

(if (= yo "q") (System/exit 0))

christian76709:03:59

for readability I would suggest using when instead of if when there is no else clause: (when (= yo "q") (System/exit 0))

suryapjr09:03:41

@christian767 thank you !!

jumar09:03:42

@suryapjr you usually shouldn't use System/exit - this will kill your REPL. It's almost always better to just return and let the program finish.

suryapjr10:03:52

@jumar coool !!

suryapjr10:03:09

Guys..why should we write concurrent code ?

suryapjr10:03:20

Practical examples..

suryapjr10:03:51

Trying to understand how to implement future

mbjarland11:03:21

in a leiningen project, how would I go about printing the dependency tree that will be visible to the user of my lib once I deploy it to clojars? Doing lein with-profiles -dev deps :tree still seems to add things like clojure-complete and org.clojure/tools.nrepl etc. I realize this probably should be in #leiningen, but that channel seems a bit stale

orestis11:03:02

@suryapjr That’s an extremely broad questions, you could write books about it 🙂 Mainly you write concurrent code because computers nowadays have more than 1 CPUs/cores, so you could be using them to get faster execution of your program. Not all problems can be solved this way, and even for those who can, historically it has been hard to write concurrent code without bugs, because the primitives exposed e.g. Threads and Locks are very hard to reason about once your program grows in size. Clojure provides higher-level primitives like atoms, promises, futures, agents etc. to help write correct concurrent code.

val_waeselynck12:03:07

@ the way I see it, what you're describing is parallelism, not concurrency. Concurrency is when several logical processes access common resources, which has more to do with the purpose of the program. A single threaded program can be concurrent!

orestis14:03:32

Heh, the old concurrency vs parallelism thingy 🙂 Very true, and I like the idea of logical process & common resources. Stealing this for some future explanation 😉

suryapjr14:03:33

Thank you gentlemen.

djwelch66612:03:56

Hello, quick question which I can't seem to find the answer to. I am slurping a body stream in my ring handler so I can convert it to a string, but I would like to first check if it is a stream to prevent failure, because sometimes the body can be string and sometimes it's an input stream. How would I do that?

djwelch66612:03:18

currently my code looks like

(def handler (wrap-apigw-lambda-proxy api))

(deflambdafn api.lambda.Handler
  [in out ctx]
  (with-open [in (io/reader in :encoding "UTF-8") out (io/writer out)]
    (-> (parse-stream in true)
        (handler)
        (update :body slurp)
        (generate-stream out))))

russ76713:03:56

If you can deal with the logic as "if this is string do this otherwise assume it's a stream" you can also use the string? function, which in my mind is just a little tidier. And it's probably complete overkill here but you could also create a multimethod. Yes, that would be utter overkill.

manutter5112:03:05

Try (if (instance? java.io.InputStream in) ,,,)

alexmiller14:03:17

you can’t spec defmulti or defmethod

alexmiller14:03:48

you can spec the dispatch function in defmulti if you extract it to a function in a var

elena.caraba14:03:54

@alexmiller I updated the first code snipped to reflect what :oneoption is.

alexmiller14:03:35

sorry, I assumed you meant creating an s/fspec for it

alexmiller14:03:22

the syntax you’re using for preconditions there isn’t right btw

elena.caraba14:03:55

@alexmiller I might not have written here correctly trying to anonymize my code

alexmiller14:03:57

:pre takes a collection of expressions

alexmiller14:03:17

so usually is like {:pre [(s/valid? int? var1)] … }

elena.caraba14:03:56

Does your statement for defmulti still stand, though?

alexmiller14:03:38

no, I thought you meant something else

alexmiller14:03:09

off the top of my head, I’m not sure if preconditions are checked in both defmulti and defmethod

elena.caraba14:03:53

So either one or the other suffices?

alexmiller14:03:57

well defmulti really takes the inputs to the multimethod dispatch function itself

alexmiller14:03:11

defmethod is really the function called as a result of the defmulti dispatch method

elena.caraba14:03:52

@alexmiller thank you for your help! It has clarified some of the spec issues I've had.

roelof16:03:10

what is the best way to learn clojure for a complete beginner

mfiano17:03:18

For those using Emacs, is cider-jack-in supposed to open the REPL also, or just connect to it? I'm Reading "Clojure for the Brave and True", and it mentions that it should also open a REPL, but it does not for me. I must also run cider-switch-to-repl-buffer.

jason13617:03:24

cider-jack-in opens another window for me, along with starting a REPL.

mfiano17:03:47

Interesting. It just prints that it connects in the minibuffer, but does not open a buffer.

jason13617:03:52

At least when opening from the context of a lein project

mfiano17:03:01

The buffer is buried and I can switch to it manually, though.

mfiano17:03:10

Yes, from a lein project.

mfiano17:03:34

@roelof Hello again. Give up on Haskell?

roelof17:03:41

nope, but always curious about others languages

roelof17:03:28

@mfiano you are also everywhere

mfiano17:03:30

True, I've been big into the functional programming community for over a decade 😅

roelof17:03:10

oke, why did you change clojure to haskell then ?

mfiano17:03:29

I'll always be at home with Lisp. Treating data as code and code as data allows for expressive code, easy refactoring, and meta-programming that just isn't possible with other languages.

roelof17:03:54

oke, I did a some ruby, little scala, little c# and some haskell

roelof17:03:08

but I find nowhere really my home

drewverlee20:03:45

If you have a strong background already i think… start with “living clojure” then read: “joy of clojure” and “elements of clojure” and “clojure applied” all at once. Mixin what ever other stuff you want!

drewverlee20:03:09

I think Onyx might embody the heart of the language in a lot of ways. So its worth looking into.

drewverlee20:03:08

also Michael Drogalis talk is fantastic. Maybe watch that before doing anything else: https://youtu.be/kP8wImz-x4w

roelof20:03:17

do not think that is something for a complete beginner in clojure but interressting

mfiano03:03:04

I think that was directed at me, coming from many years of Lisp, but new to Clojure this week.

mfiano03:03:10

So thanks 🙂

mfiano15:03:40

That was one of the best talks I ever saw

drewverlee21:03:11

@mfiano np. That talk changed my perspective on a lot of things

roelof17:03:04

For me I cannot find a language which it totally good for me. So I can settle

gonewest81817:03:52

@mfiano you say the repl window is buried, but is it actually connected and working? Is there anything written to *nrepl-messages* or *Messages* that suggests a failure? I’m about to go afk for an hour but that’s where I would start. You can take this discussion to #cider where others can surely pitch in

mfiano17:03:33

@gonewest818 Yes, it is connected. I can switch to the repl buffer and use it just fine. It's just that it is not opened when I call cider-jack-in from a lein project.

mfiano17:03:47

(It only connects to it when I call that)

seancorfield18:03:37

@mfiano Sounds like an Emacs setting needs updating to auto-switch to the REPL buffer when it is opened. It sounds familiar but it's been a few years since I used Emacs... There is an #emacs channel if you want to try their or #cider perhaps?

seancorfield18:03:02

(ah, I see @gonewest818 already suggested the latter)

dpsutton18:03:25

Michael did you follow the setup guide for emacs from brave and true?

mfiano18:03:56

@seancorfield's solution worked. Thanks all.

drewverlee19:03:26

I wonder if there would be interest in a remote meet up and discussion around the Elements of Clojure book which is having a full release this week (crosses fingers)

roelof19:03:31

no more recommendations for learning clojure as a beginner ?

sundarj19:03:36

heard good things about Living Clojure, and can recommend Programming Clojure

sundarj19:03:05

also this series of blog posts is really good: https://aphyr.com/tags/Clojure-from-the-ground-up

seancorfield21:03:18

A +1 for Living Clojure.

roelof21:03:05

thanks all

me.lucas.sa21:03:54

hey guys, just a noob question… how do I remove a map from my list based in another list?

(def bl '({:lemma the, :pos DT} {:lemma dog, :pos NNS} {:lemma be, :pos VBD} {:lemma dog, :pos VBG} {:lemma around, :pos RB} {:lemma ., :pos .})
(def st '(the be around))

expected output: ({:lemma dog, :pos NNS} {:lemma dog, :pos VBG}  {:lemma ., :pos .}  )

me.lucas.sa21:03:20

I tried with remove and filter but didn’t work the way I was expecting.

steve47621:03:23

you can use filter. You'll want to make st a set

dpsutton21:03:49

(filter (comp #{"the" "be" "around"} :lemma)
        '({:lemma "the" :pos "DT"} {:lemma "dog" :pos "NNS"} {:lemma "be" :pos "VBD"}
          {:lemma "dog" :pos "VBG"} {:lemma "around" :pos "RB"} {:lemma ".," :pos "."}))
({:lemma "the", :pos "DT"}
 {:lemma "be", :pos "VBD"}
 {:lemma "around", :pos "RB"})

dpsutton21:03:29

comp is compose. so filter by first calling :lemma and then the set with what you are looking for in the lemmas.

austin02123:03:58

I have been told there is a way to inspect the source of an existing function in-repl. Is this Truth? How do?

seancorfield23:03:38

@austin021 usually just (source my-func)

seancorfield23:03:11

(! 688)-> clj
Clojure 1.9.0
user=> (source inc)
(defn inc
  "Returns a number one greater than num. Does not auto-promote
  longs, will throw on overflow. See also: inc'"
  {:inline (fn [x] `(. clojure.lang.Numbers (~(if *unchecked-math* 'unchecked_inc 'inc) ~x)))
   :added "1.2"}
  [x] (. clojure.lang.Numbers (inc x)))
nil
user=> 

austin02123:03:21

Just what I was thinking of, thanks!

seancorfield23:03:27

The function has to have been loaded in from a source file -- it doesn't work with functions just typed in at the REPL.

mfikes23:03:01

^ FWIW, we added that to Lumo and Planck... it is a great convenience, that could perhaps be in all REPLs 🙂

seancorfield23:03:28

Is it much work to add function sources to a repl @mfikes?

mfikes23:03:13

@seancorfield It wasn't that difficult in ClojureScript, as we just stashed the source in the AST, and fetched it from there. https://github.com/mfikes/planck/commit/f16e6187f09a0201b3b2cefbfd8e0c1d75fa053d?w=1

seancorfield23:03:57

Hmm, source-fn looks for :file metadata and then reads the source file... 👀

mfikes23:03:45

I revised that URL to only show the non-whitespace change to show how simple it really was

mfikes23:03:34

It is probably worth an hour spiking it in the Clojure REPL to see if it pans out there 🙂

seancorfield23:03:57

Hah, yeah, in one of my many "spare" hours... 🙂

seancorfield23:03:12

But maybe I'll try to scratch that itch at some point...

mfikes23:03:19

You'll gain them back when typing source for REPL entered forms 🙂