Fork me on GitHub
#beginners
<
2018-02-11
>
opsb11:02:40

I'm having a bit of a negative reaction to list comprehensions 🙂 Are they idiomatic (in which case I should just fight through and embrace) or could I safely favour the use of map/`filter` etc?

bronsa11:02:23

nothing unidiomatic about for

bronsa11:02:55

it can often lead to more concise and clear code than the equivalent map/mapcat/filter version

opsb11:02:49

ok, I guess I should at least give them a fair go

Sabbatical201715:02:37

What's the idiomatic way to look up a key in a map, but if the key is not present I would like it to crash rather than return nil? If you think I should not be interested in doing this, because the question indicates that I am doing it wrong somehow, I would also be interested in hearing that.

Sabbatical201715:02:26

The reason I am asking is that when I wrote an app in Clojure I kept looking up keys in the wrong place (e.g., look up :foo in a map when I should have looked up :bar to get a map in which there is actually a :foo). I am assuming that there is a spec-related full answer to this question, but I am hoping that there is also quick way to just get a crash right where the problem is, instead of somewhere far away where the nil I got ends up propagating and blowing up stuff.

joelsanchez15:02:12

don't think there is an idiomatic way to make get throw you can use spec to validate your data before trying to call get

noisesmith15:02:51

the simple version is (fn [m k] (if-not (contains? m k) (throw (ex-info "key not found" {:m m :k k})) (get m k)))

bronsa16:02:09

I usually do (if-let [[_ v] (find m k)] v (throw ..)) in those cases

ghadi16:02:18

another idiom: (or (get m :important-key) (throw (Exception...)))

ghadi16:02:28

(when validating stuff inline)

bronsa16:02:20

that doesn't support nil or false tho

noisesmith16:02:24

I keep forgetting to use find

derpocious18:02:12

hey all, is there a "weaker" version of assoc?

derpocious18:02:48

For example, If I have a big object db and just want to update one value some layers down in nested maps

derpocious18:02:53

(assoc db :items { :muffin {:quantity -1}})

derpocious18:02:15

But this will wipe out any other children of the object

noisesmith18:02:28

there's assoc-in for nested updates, I don't know if that's what you mean by "weaker"

derpocious18:02:18

I just meant updating the object without wiping out all the other keys of the maps

noisesmith18:02:36

user=> (assoc-in {:muffin {:quantity -1 :flavor 'blueberry}} [:muffin :quantity] 3)
{:muffin {:quantity 3, :flavor blueberry}}

noisesmith18:02:29

see also get-in / update-in for related functionality

Steve Upstill21:02:11

Hey, thrashing beginner here. I've been working through the Web Development book using the command line, and I'm trying to switch over to IntelliJ IDEA and Cursive to run my project. Got IntelliJ installed, with Cursive within, and opened the 'guestbook' project. But when I try to run the REPL,

Steve Upstill21:02:21

all I get is "Error running 'REPL for guestbook': Unknown error. My environment is as follows:

Steve Upstill21:02:31

IntelliJ IDEA 2017.3.3 (Community Edition) Build #IC-173.4301.25, built on January 16, 2018 JRE: 1.8.0_152-release-1024-b11 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Mac OS X 10.12.6

Steve Upstill21:02:10

Apparently it's using IntelliJ's JDK, which according to earlier reports is the best way to work around Java issues.

Steve Upstill21:02:07

"Stuck" doesn't begin to cover where I am, which is completely lost at sea. Any suggestions for an utter noob?

tstelzer21:02:39

@steve171 man, having to deal with tooling problems, when you really just want to learn a language sucks

Steve Upstill21:02:03

True that, brother!

tstelzer21:02:32

hope you find relief, i don't use any of those things unfortunately 🙂

Drew Verlee21:02:31

@steve171 is the book web development clojure 2?

Steve Upstill21:02:00

Yes, Web Development with Clojure, 2nd Edition

Drew Verlee21:02:29

how are you running the repl?

Steve Upstill21:02:15

There's a 'REPL for guestbook' choice in the Run menu, and I've created a vanilla configuration and run that. Same result.

Drew Verlee21:02:39

hmm i want to try this. your the second person to ask me

Drew Verlee21:02:14

to be clear you can start the server right? with lein run?

Drew Verlee21:02:36

also, imo cursive is probably the safest editor choice out there for clojure.

Steve Upstill21:02:39

I'm noticing that the Leiningen window lists the following tasks: pom, help, upgrade, install, jar, deps, uberjar, test, clean, compile and version. Is it suspicious that there's no 'run' option?

Drew Verlee21:02:45

so your fine with your setup.

Steve Upstill21:02:07

Yes, I can run lein from the shell...though it does report the following:

Steve Upstill21:02:11

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.xnio.nio.NioXnio$2 (file:/Users/upstill/.m2/repository/org/jboss/xnio/xnio-nio/3.4.0.Beta1/xnio-nio-3.4.0.Beta1.jar) to constructor sun.nio.ch.KQueueSelectorProvider() WARNING: Please consider reporting this to the maintainers of org.xnio.nio.NioXnio$2 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release

seancorfield22:02:24

Sounds like you're running on Java 9? These are normal (and harmless) warnings caused by stuff in Java 9 that not everyone has caught up with yet.

Steve Upstill21:02:26

Could this be causing IntelliJ to lose it's shit?

Drew Verlee21:02:45

i have no idea what any of that means

Steve Upstill21:02:52

You and me both.

Steve Upstill21:02:33

I've ignored it previously because it didn't interfere with working with the lein REPL

Drew Verlee21:02:02

my first guess, is you need to connect to the repl that starts when you run lein run -p 8000:

$ lein run -p 8000
...
luminus.repl-server - starting nREPL server on port 7000

Steve Upstill21:02:03

Sorry, I can't interpret that first line.

Steve Upstill21:02:27

Specifically, what does 'âś—' denote?

Drew Verlee21:02:44

some random stuff from my terminal.

Drew Verlee21:02:27

run > clojure repl > remote > host: 127.0.01 port: 7000 Is what i was hoping would allow me to connect to the repl. but it seems to just stall.

Steve Upstill21:02:52

Well, the luminus.repl-server is specified in my src/clj/guestbook/core.clj file. It's not clear how I would get the IDE to run it, since there is only a limited selection of choices for the REPL: 'Use nREPL in normal JVM Process', 'Run nREPL with Leiningen' and 'Use clojure.main in normal JVM process'

Drew Verlee21:02:44

there is a remote repl option. which i would assume we want, but i tried to connect to the luminus repl (localhost:7000) and it just stalls.

Steve Upstill21:02:59

Now that's interesting. I picked 'Use clojure.main...' (getting warnings about limited functionality) and ran it, and it apparently started up.

Drew Verlee22:02:18

hmm. ok. now im guessing we dont want to run lein run -p 8000 from the command line. we want to use intelliji to start it

Steve Upstill22:02:25

That sounds like a challenge. BTW, I'm using Colin's instructions for getting started with Cursive, and he suggests that "if you already have an nREPL server running somewhere, you can connect to it using a Remote configuration. There are two options here - either you can configure it to connect to a host and port, or you can configure it to connect to 127.0.0.1 and get the port from the port file written by Leiningen."

Steve Upstill22:02:45

Now the trick is to get the nREPL server running...

Drew Verlee22:02:46

Yea. i dont see why i can’t connect to it using the remote option. But it just doesn’t when i punch in localhost and the port. so humpf

Steve Upstill22:02:41

Well, what do you know? When I just 'lein repl', I get an nREPL server on port 60109. Now let's negotiate with IntelliJ...

Steve Upstill22:02:52

Hey! I created a Remote configuration, gave it the IP address and port, and it seems to run! No prompt, oddly, but I seem to be talking to the REPL!

Steve Upstill22:02:22

@seancorfield yeah, I assumed it was noise. (I just installed Java, like a week ago, so I'm also assuming it's the latest version)

Drew Verlee22:02:13

@steve171 There is more then one way to do this. This way also works and is more intelliji compact: 1) run > clojure repl > local: run with lein paramaters: -p 8000 i believe this to be working as i can send parts of my application to the repl now.

Steve Upstill02:02:56

That just gets me the same Unknown Error when I try to run.

Steve Upstill02:02:49

However, I did set up a Remote configuration with "Use Leiningen REPL port" and it worked. But now, having restarted IntelliJ, Running the config (green triangle next to the Configuration selection) does nothing. No errors, no feedback, nothing. GAH!

Steve Upstill22:02:21

@drewverlee is there supposed to be a 'clojure repl' choice on the IntelliJ Run menu?

Drew Verlee22:02:39

@steve171 run > edit configurations >create (+ in upper left corner) > clojure repl > local:

seancorfield22:02:29

BTW, there's a #cursive channel if you need to dig deeper on IntelliJ and Clojure.

seancorfield22:02:17

(I personally find IntelliJ to be horrible -- I've tried it so many times and just given up... but then I really don't like IDEs with menus and panels all over the place)

seancorfield22:02:38

When I started with Clojure, I used TextMate with a simple REPL plugin. It was good to be learning Clojure without having to also learn a new editor. Then I switched to Emacs for several years which was its own giant learning curve. Then I switched to Atom/ProtoREPL and I've been very happy with the simplicity of that setup.

seancorfield22:02:30

But editors are very subjective so my general advice is: if there's a Clojure plugin for the editor you already use, go that route; if there isn't, pick the combination that's closest to what you're used to.

Drew Verlee22:02:08

I had assumed cursive to be the most structured and guided approach. But maybe thats not the case.

Drew Verlee22:02:26

I’m happy with spacemacs and cider so far. I only get in trouble trying to be on the edge.

Drew Verlee22:02:10

@steve171 i would go with #luminus and #cursive for more help. i dont feel like i understand cursive well enough to help. I too wasn’t starting the luminous server with my setup. I’m guessing you do what i said put the profile as “dev” (which might be the default) and then pass the -p 8000 to something. but i can’t figure out what. Maybe its supposed to be a string?

seancorfield22:02:23

I'm seeing a lot of people working through that Web Dev book and getting into all sorts of difficulties. Web dev in Clojure is very different to most other languages, because you have to assemble a whole bunch of libraries to get the "full stack framework" look'n'feel that is common in other languages -- and I personally think there's too much "magic" going on if you start with a "batteries included" combination of libraries. I recommend beginners start with just a bare bones Compojure / Ring app and then look at Hiccup or Selmer for generating HTML and get a solid handle on all the basics, before attempting to understand something like Luminus.

seancorfield22:02:57

As Arachne was being developed, I felt it suffered from a lot of the same problems: so much machinery and "magic" in the name of flexibility and elegance...

gklijs22:02:18

@seancorfield I really liked Luminus. I think it was the second let project I did, adding some back and stuff to a snake game. It's nice to get all the pieces working, even if you don't really get all the profiles and stuff.

gonewest81823:02:50

@steve171 by any chance are you the author of “The Renderman Companion”? On my bookshelf 5 feet from where I sit.