Fork me on GitHub
#beginners
<
2023-01-18
>
mister_m01:01:04

I have a https://docs.oracle.com/javase/8/docs/api/java/nio/file/DirectoryStream.html, but I am not sure how to work with this java Iterable. I'd like to reduce over it and produce a sequence of objects read from the files returned by the iterator. How can I use this with reduce?

hiredman01:01:40

If you call the iterator method you should get an iterator and reduce may work on that out of the box these days

hiredman01:01:56

There is also iterator-seq

mister_m01:01:12

ah I see - I just found that. m

ghadi01:01:10

Did you try reduce directly? That class has Iterable as a parent, which are natively reducible

kostamo13:01:04

Hi, how to enable namespace form auto generation when creating a new file? For vscode/calva I found this: https://calva.io/namespace-form-auto-creation/ but it does not work out of the box on my machine, nor can I find a setting where to enable that. Adding "calva.newFile.createNamespaceForm": true did not work either.

skylize13:01:00

Should work out of the box in Calva. Are you creating files with .clj, .clcs, .cljc extension?

kostamo13:01:55

Tried with .clj and .cljs extensions, neither worked.

seancorfield16:01:52

I think it's LSP under the hood that generates the new ns form so maybe your LSP setup isn't working right for your project. I'd suggest asking in #CBE668G4R with details about your project setup...

pez17:01:20

A way to check if it is the project configuration or something else, is to try it with a minimal project. For Clojure CLI that would be a folder with.`./deps.edn` containing an empty map {}. Then open this folder in VS Code. Then create a file ./src/hello/core.clj it should get a namespace form (ns hello.core).

pez17:01:22

(If you create the deps.edn file in VS Code clojure.lsp will complain loudly. Just add the empty map and reload the VS Code window, Developer: Reload Window.)

kostamo09:01:13

Thanks for the tips! Looks like it’s a workspace issue, ns creation works as expected on another project.

pez11:01:29

You are welcome! The clojure-lsp status item pops up a menu with three options that could prove useful. • Show server info. A map with clojure-lsp info about the project and the lsp server status. • Open the clojure-lsp server log. You might find messages in there that can help pinpoint what's wrong. • Open trace level settings. The communication between Calva's clojure-lsp client and the server can be traced in the Output channel Clojure Language Client. This is disabled by default. Mostly useful for attaching to questions and asks for help. You'll find people who knows how to read those logs in the #CPABC1H61 channel.

Guild Navigator19:01:11

How do I import an external library into the REPL? I want to do this:

(ns user (:require [feedparser-clj.core] [clojure.string :as string]))
but of course I don't have that library installed yet. I don't have a project set up yet - just looking to play around in the REPL first. I tried this:
$ clj -X:deps find-versions :lib feedparser-clj.core
Downloading: org/clojure/tools.deps.cli/0.9.10/tools.deps.cli-0.9.10.pom from central
Downloading: org/clojure/tools.deps/0.16.1260/tools.deps-0.16.1260.pom from central
Downloading: org/clojure/tools.deps.cli/0.9.10/tools.deps.cli-0.9.10.jar from central
Downloading: org/clojure/tools.deps/0.16.1260/tools.deps-0.16.1260.jar from central
Execution error (NullPointerException) at java.util.regex.Matcher/getTextLength (Matcher.java:1769).
Cannot invoke "java.lang.CharSequence.length()" because "this.text" is null

Full report at:
/var/folders/rp/_1s4ggdd65sf_y4y5nzp6w2s2s874j/T/clojure-7897925417238007337.edn

Alex Miller (Clojure team)20:01:42

that looks like a Clojure namespace, not a library name (groupId/artifactId)

Alex Miller (Clojure team)20:01:11

% clj -X:deps find-versions :lib org.clojars.gnzh/feedparser-clj
Downloading: org/clojars/gnzh/feedparser-clj/maven-metadata.xml from clojars
{:mvn/version "0.6.0"}
{:mvn/version "0.6.1"}

% clj -Sdeps '{:deps {org.clojars.gnzh/feedparser-clj {:mvn/version "0.6.1"}}}'
Downloading: org/clojars/gnzh/feedparser-clj/0.6.1/feedparser-clj-0.6.1.pom from clojars
Downloading: net/java/dev/rome/rome/1.0.0/rome-1.0.0.pom from central
Downloading: net/java/dev/rome/rome/1.0.0/rome-1.0.0.jar from central
Downloading: org/clojars/gnzh/feedparser-clj/0.6.1/feedparser-clj-0.6.1.jar from clojars
Clojure 1.11.1

user=> (ns user (:require [feedparser-clj.core] [clojure.string :as string]))
nil

Alex Miller (Clojure team)20:01:56

most libraries are a little more forthcoming with their lib name (here org.clojars.gnzh/feedparser-clj) - I found it in the project.clj in the github repo, but really should be in the readme

Guild Navigator20:01:36

Thanks! Very helpful. I really need to better understand the various CLI switches and when to use them

Guild Navigator20:01:46

Clojure Day 3 for me

Guild Navigator20:01:51

What did I do wrong here? Should ns have returned nil?

❯ clj -X:deps find-versions :lib org.clojars.gnzh/feedparser-clj
Downloading: org/clojars/gnzh/feedparser-clj/maven-metadata.xml from clojars
{:mvn/version "0.6.0"}
{:mvn/version "0.6.1"}
❯ clj -Sdeps '{:deps {org.clojars.gnzh/feedparser-clj {:mvn/version "0.6.1"}}}'
Downloading: org/clojars/gnzh/feedparser-clj/0.6.1/feedparser-clj-0.6.1.pom from clojars
Downloading: net/java/dev/rome/rome/1.0.0/rome-1.0.0.pom from central
Downloading: jdom/jdom/1.0/jdom-1.0.pom from central
Downloading: jdom/jdom/1.0/jdom-1.0.jar from central
Downloading: net/java/dev/rome/rome/1.0.0/rome-1.0.0.jar from central
Downloading: org/clojars/gnzh/feedparser-clj/0.6.1/feedparser-clj-0.6.1.jar from clojars
Clojure 1.11.1
user=> (ns user (:require [feedparser-clj.core] [clojure.string :as string]))
nil
user=> (def f (parse-feed ""))
Syntax error compiling at (REPL:1:8).
Unable to resolve symbol: parse-feed in this context
user=>

Alex Miller (Clojure team)21:01:53

> Should ns have returned nil? yes

Guild Navigator21:01:20

Ok cool. Just not sure what that REPL:1:8 error is

Alex Miller (Clojure team)21:01:05

require causes the namespace to be loaded and all of it's symbols are then available as fully qualified symbols feedparse-clj.core/parse-feed

Guild Navigator21:01:27

(ns user (:require [feedparser-clj.core :as fp] [clojure.string :as string]))

(def f (fp/parse-feed ""))

Alex Miller (Clojure team)21:01:40

if you want to use it as a shorter name you can either: 1. Alias the namespace 2. "Refer" the symbol so that it can be used unqualified in your current namespace

Guild Navigator21:01:01

i did the alias - not sure what refer is

Alex Miller (Clojure team)21:01:27

so because it's aliased, you can use the aliased form like you did

Alex Miller (Clojure team)21:01:05

or you could:

(ns user (:require [feedparser-clj.core :refer [parse-feed]] [clojure.string :as string]))
(def f (parse-feed ""))

Alex Miller (Clojure team)21:01:27

generally, most of the time, it's better to alias

Guild Navigator21:01:53

ah i see - yes i can see where that can get confusing

Alex Miller (Clojure team)21:01:17

it's useful for cases where you have tightly coupled namespaces, or namespaces that provide things that look like "syntax"

Guild Navigator21:01:42

ok another dumb newbie question:

(ns main
  (:gen-class)
  (:require (feedparser-clj.core :as fp)))

(defn parse-feed []
  (def f (fp/parse-feed ""))
  )

(parse-feed)

Guild Navigator21:01:52

Execution error (IllegalArgumentException) at main/eval138$loading (main.clj:1).
Don't know how to create ISeq from: clojure.lang.Keyword

Guild Navigator21:01:02

i suspect i'm doing ns wrong

andy.fingerhut21:01:41

I believe you need square brackets instead of parentheses around [feedparser-clj.core :as fp]

Guild Navigator21:01:13

I absolutely hate IntelliJ for this. It literally won't let me type ) or ].

andy.fingerhut21:01:27

That might be IntelliJ with some Clojure editing mode/package that does something called paredit? Not sure, as I haven't used paredit myself.

Guild Navigator21:01:47

I'm having better luck with VS Code + Calva

Guild Navigator21:01:27

its working nicely now:

(ns main
  (:gen-class)
  (:require [feedparser-clj.core :as fp]))

(defn parse-feed []
  (def f (fp/parse-feed ""))
  (println f)
  )

(parse-feed)

👍 2
Stuart09:01:49

You really want to get used to structural editing on. It might seem annoying now, but will make your life easier / editing much faster once you get used to it.

☝️ 4
simongray13:01:32

Just use parinfer.