Fork me on GitHub
#beginners
<
2017-11-17
>
derpocious01:11:30

hey guys, does anyone here use the "lein repl" command?

derpocious01:11:46

For me it starts up with the promt at "user", and I can't figure out how to add a namespace

derpocious01:11:14

I have a file like this (ns jims-bas-ic-cljs-cron.core (:require [cljs-lambda.macros :refer-macros [defgateway]])) (defn derp [] (println "Hi") 3)

Anthony Ciacci01:11:06

I’m not positive I can provide direct help, but perhaps this might be useful to you? https://www.braveclojure.com/getting-started/

seancorfield01:11:44

@derpocious When you say "add a namespace", what do you mean?

seancorfield01:11:19

(assuming you have the dependencies correct in your project, you should be able to just type that code into the REPL)

seancorfield01:11:55

(also, looks like you are trying to do something with ClojureScript code, maybe in a Clojure REPL?)

lovuikeng02:11:11

derpocious probably meant how to require ns in repl

seancorfield02:11:14

In which case... @derpocious If that file is in src/jims_bas_ic_cljs_cron/core.clj (which matches the ns path you showed -- - in Clojure names become _ on the file system), then you would (require '[jims-bas-ic-cljs-cron.core :as jim]) (to load it and provide an alias of jim) and then you can call (jim/derp)

seancorfield02:11:14

(but having that cljs-lambda.macros reference in there with :refer-macros makes me think you're trying to mix Clojure REPL -- from lein repl -- with ClojureScript source code)

derpocious02:11:47

Thanks @seancorfield! Yes, I'm running lein repl. Is there a way to use lein to run a clojurescript repl?

derpocious02:11:11

When I run the code as you typed in the root of the project I get this error:

derpocious02:11:24

FileNotFoundException Could not locate jims_bas_ic_cljs_cron/core__init.class or jims_bas_ic_cljs_cron/core.clj on classpath. Please check that namespaces with dashes use underscores in the Clojure file name. clojure.lang.RT.load (RT.java:456)

derpocious02:11:58

even when I run lein rep l in the src folder I am getting FileNotFoundException 😞

seancorfield03:11:13

You don't want to run lein repl in the src folder. Do you have a project.clj file? That's where you should run lein repl

seancorfield03:11:50

As for ClojureScript, I don't know. I haven't used ClojureScript for about three years and I don't know what the start of the art is in tooling.

seancorfield03:11:22

(I haven't used Leiningen much for nearly two years at this point but I used it with Clojure for years so I can probably still help there)

seancorfield03:11:18

If you're trying to do anything with ClojureScript, I'd recommend finding an up-to-date tutorial and go through it step-by-step.

derpocious12:11:21

@seancorfield I have tried to follow tutorials, but I get errors that don't show up in the tutorials...

derpocious12:11:34

pls check the leiningen room

seancorfield15:11:39

I'm not in the #leiningen room because I no longer use Leiningen.

solf15:11:36

Anyone knows why this returns a function:

(-> '(1 2 3) (partial map #(* % 3)))
=> #function[clojure.core/partial/fn--4761]
But the same code, extracting the partial in a variable, returns a list multiplied by 3 (the result I was expecting):
(def map-mult-3 (partial map #(* % 3)))
(-> '(1 2 3) map-mult-3)
=> (3 6 9)

solf15:11:58

Oh.. is it because -> is a macro, so it interleaves '(1 2 3) before calling partial, resulting in a call to (partial '(1 2 3) map #(* % 3)) ?

rkwofford16:11:22

Maybe you don't need "partial":

rkwofford16:11:50

(map #(* % 3) '(1 2 3)) returns the result you expect

rkwofford16:11:17

(partial map #(* % 3) '(1 2 3)) returns a function

rkwofford16:11:16

Wrap it in parens and it returns what you expected: ((partial map #(* % 3) '(1 2 3)))

solf16:11:46

Oh yeah, it's the -> macro I was trying to understand 🙂

leontalbot03:11:35

(->> '(1 2 3) (map #(* % 3))) or (->> '(1 2 3) (map (partial * 3))) :-)

avfonarev20:11:36

What is the most idiomatic way to create a map from a sequence of keys providing the default value?

phronmophobic20:11:41

do you have an example?

phronmophobic20:11:16

i’m not sure exactly what you mean by your description

phronmophobic20:11:17

are you talking about something like

(into {}
      (for [k my-keys]
        [k default-value]))

avfonarev20:11:09

Say, I have a sequence (1 2 3) and I want to create a map {1 0, 2 0, 3 0}

phronmophobic20:11:06

(let [my-keys [:a :b :c]
      default-value 1]
  (into {}
        (for [k my-keys]
          [k default-value])))

leontalbot03:11:24

You might not need my-keys and use directly the vector in the for loop. Also for accepts a :let social key where you could assign default-value

phronmophobic20:11:35

not sure if it’s the most idiomatic

avfonarev20:11:15

Yes, I was wondering if this code gets optimised by the clojure compiler, for instance

ghadi20:11:07

clojure compiler does not do optimizations, only bytecode translation

avfonarev20:11:45

Thank you, I will stick with it for now

rkwofford20:11:09

How about (zipmap '(1 2 3) (repeat 0))

byron-woodfork20:11:40

^what I was gonna suggest

avfonarev20:11:15

@rkwofford perfect! Thank you

mlombardo21:11:31

How would I read a file line by line, and store values into particular keys of a map, each value in the line is seperated by a semi-colon? (;) So my data would look something like this

semester;title;crn;code;levels;credits;campus;section;capacity

seancorfield21:11:30

@michael.lombardo And the keys would be... what part of that data?

mlombardo21:11:54

Let's say for simplicity my keys in the map would be semester, title etc.

mlombardo21:11:25

I want to make all of these values keys

seancorfield21:11:58

I'm confused... which parts would be keys and which parts would be values?

seancorfield21:11:29

Can you give an actual example of the data (with actual values, so we aren't confused by the names of the values)?

seancorfield21:11:13

OK, so the first line is keys, the rest of the file is values (clarified by DM), so slurp the file, and line-seq to turn it all into lines, then map a function to split the lines (`(str/split line #";")` and then for each line in the rest of that file data, you could zipmap the first line (the headers -> keys) against the data.

gdeer8122:11:00

@michael.lombardo does this look like what you're trying to do? (let [header [:semester :title :day] rows [[1 "cs101" "monday"] [1 "cs301" "tuesday"]]] (for [row rows] (zipmap header row))) in this example the first row has already been parsed as the header and the rest of the file has been parsed into a collection of vectors since that is pretty straight forward. this returns ({:semester 1, :title "cs101", :day "monday"} {:semester 1, :title "cs301", :day "tuesday"})

seancorfield22:11:39

(yeah, we took nearly all of this to DM as there were a lot of terminology things to get straightened out first)

seancorfield22:11:01

We ended up with something like this

(defn read-data
  "Given a filename, read ;-separated data from it and return a sequence of
  hash maps, using the first line of the file as keys."
  [filename]
  (with-open [r (io/reader filename)]
    (let [[headers & lines] (->> r line-seq (map #(str/split % #";")))]
      (doall (map #(zipmap (map keyword headers) %)
                  lines)))))

seancorfield22:11:17

for is probably nicer than map there but the problem is that looks like a for-loop in C/Python so it's confusing 🙂

gdeer8123:11:55

when I first learned the for comprehension it really clicked when I created a deck of cards by creating suite/value tuples so now when I see a problem that takes that shape I go "for" it

seancorfield23:11:20

Yup, once folks stop seeing for as a C/Python-style for loop, and understand it's a "comprehension" instead, it can be a really elegant way to express generation of values.

seancorfield23:11:36

Especially given :when and :let qualifiers...

gdeer8123:11:26

oh yeah that reminds me that I need to make a gist of that code we figured out for the "find-the-pairs" problem. It's so beautiful