This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-04-03
Channels
- # announcements (4)
- # aws (13)
- # babashka (35)
- # beginners (162)
- # boot (8)
- # calva (5)
- # chlorine-clover (15)
- # cider (64)
- # clj-kondo (20)
- # cljs-dev (29)
- # clojars (6)
- # clojure (166)
- # clojure-europe (3)
- # clojure-finland (6)
- # clojure-france (8)
- # clojure-germany (3)
- # clojure-italy (3)
- # clojure-nl (7)
- # clojure-spec (49)
- # clojure-uk (83)
- # clojurescript (39)
- # clojurex (5)
- # core-typed (2)
- # cursive (3)
- # data-science (17)
- # datascript (3)
- # datomic (22)
- # exercism (5)
- # fulcro (3)
- # jobs-discuss (2)
- # joker (2)
- # kaocha (3)
- # malli (26)
- # off-topic (89)
- # pathom (10)
- # pedestal (14)
- # protorepl (14)
- # re-frame (23)
- # reitit (2)
- # shadow-cljs (27)
- # slack-help (10)
- # spacemacs (14)
- # tools-deps (10)
- # tree-sitter (3)
- # xtdb (19)
- # yada (2)
i'm mostly concerned about divide by zero exceptions, which it looks like ratios do not prevent. maybe i should use doubles - you can divide a double by zero, looks like.
@michael740 is this what you're trying to do?:
(/ (+ 20 2) (+ 5 2))
=> 22/7
that would do it! i do think ratios are not the right tool for me, after all.
Also see quot
which is regular division (not a ratio).
It rounds to the nearest integer.
@michael740, it sounds like you’re trying to do algebraic manipulations. maybe something like https://github.com/clojure-numerics/expresso ?
wow, thanks for pointing me at this!
i didn't know this existed
I have a cyclical data structure built with Refs that I would like to print. At some points I'd like the print the hex part of ref.toString() without printing the contents. What's the best to access this part Ref?
For example the 0x1662e925
part of #object[clojure.lang.Ref 0x1662e925 {:status :ready, :val true}]
Nevermind, got it (format "%x" (System/identityHashCode my-ref))
Quot truncates, does not round up, only rounds down.
hey folks. I’m trying to parse the XML from a wordpress export to move into blog using cryogen or perun, some nice clojure static site generators. I think I need some pointers in parsing the xml though, as i’m able to follow examples on clojure doc and clojure’s own test to see how to get a single map from a post using the queries in the gist below. However, I’m unable to figure out how to iterate through an xml data structure to do this for the other 150-odd posts available to me - when I try to get the next I get the whole blob of XML, which is no good fo me. I’ve spent a good few hours reading up on how clojure zippers work, and trying various kinds of recursion, loops, and other macros but alas - no dice. I’m defeated by something I could do in pretty quickly in other languages :( You can see my code in the gist below, along with the source xml - how can I work with each post in the xml, to get a map back to write to a text files, or dump in a database? https://gist.github.com/mrchrisadams/cb9b3af65757b9fb9253b0c3cfa1d6cd
What is the correct way to test for a thrown ex-info
using clojure.test? At the call site of ex-info
I can specify data in a hashmap. With the (is (thrown? ...))
I would like to test the information in that hash map to make sure I'm getting the correct exception information.
You can wrap it in a try/catch, assign the exception to a local variable, use ex-data to get the data out, and check whatever part of its contents you wish, potentially using one or more (is (= x y))
expressions.
And if that proves a bit too much boiler-plate, you can hide it away by extending clojure.test with custom assertions by adding methods to the assert-expr
multimethod - see thrown?
and thrown-with-msg?
themselves for examples.
@andy.fingerhut can you help me construct a try/catch will only catch the exception I designated in the (throw (ex-info ...))
? And importantly will not accidentally catch any other.
(try (throw (ex-info "mybad" {:a 1})) (catch clojure.lang.ExceptionInfo e (prn (ex-data e))))
@kelvended, How do I find the code for thrown?
? When I press M-.
in emacs/cider, it cannot find the source code.
I don't use Emacs myself but even in Cursive I can't step into those for some reason - I tend to just "step into" is
instead as the code is all in clojure.test
. Anyway here's the code: https://github.com/clojure/clojure/blob/master/src/clj/clojure/test.clj#L504-L535
thrown? Is not actually a function
It’s actually a special symbol understood by is
you are the 973rd person to be confused about this :)
Are you concerned that some other exception other than the one you are testing will occur? Every JVM exception has a class, and there are many such classes. You can give a class name in the catch expression, and such a catch will only catch exceptions with that class, or one of its subclasses.
yes, so which class name should I give for exceptions generated by ex-info
?
You can find out in a REPL by doing a catch on the most general class for all JVM exceptions, Throwable, and then in the exception handler print out the (class x) of the exception x
(doc ex-info)
-------------------------
clojure.core/ex-info
([msg map] [msg map cause])
Create an instance of ExceptionInfo, a RuntimeException subclass
that carries a map of additional data.
Suddenly clojure -A:new
stopped working though I have it defined in ~/.clojure/deps.edn
. It instead tries to look for a deps.edn
in the current directory. Used to work.
Okay, so this command works under WSL just fine, using the Linux version of tools.cli. Only under Windows there seems to be an issue.
Yeah, it looks like support is still in an alpha stage: https://github.com/clojure/tools.deps.alpha/wiki/clj-on-Windows
Lots of functions take a predicate such as remove. I find myself often writing the following ideom.
(remove (fn [x] x=something) my-sequence)
what I'd rather write is
(remove (eql something) my-sequence)
Does such a function eql
exist? Of course I can write it in one line, but seems like it ought to already be there.or you could always use (partial = something)
or #(= % something)
, no need for a specialized function
ah because a singleton set acts like an equivalence predicate for its singular content. clever
Yup, unless the set contains false
or nil
. Sets when called as fns return the arg if present and nil
otherwise, both are falsy in this case and contains?
would be more appropriate.
If I remember correctly, clojure.java.shell
runs commands directly, rather than going through bash or cmd. I've had to use absolute paths to get around the issue.
i've used things like this: https://gist.github.com/sogaiu/0c542ee238858adfe33f1200e3a86634
@U3X7174KS I was simply trying (shell/sh "dir")
.
I think I'd look into a full path as suggested above, then. I don't have a Windows system at hand, so testing is a little hard. Is dir
a shell builtin or a program? where dir
might help you discover that.
Another option is starting a shell to run a command. Something similar to cmd /c dir
, if I recall correctly. See https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
;; On my Linux system, this works:
(sh "bash" "-c" "pwd")
;; => {:exit 0, :out "/home/teodorlu/.../scratchproject\n", :err ""}
;; For the record, this also works, since pwd is "normal" program:
(sh "pwd")
;; => {:exit 0, :out "/home/teodorlu/.../scratchproject\n", :err ""}
(sh "cmd" "/c" "dir")
;; Might give you what you want?
As a workaround, I ran the same thing from Windows Subsystem for Linux and ls
etc. worked. dir
is a Windows inbuilt command, same as ls
. Lists the contents of the current dir.
where
is not available on Windows.
I have a workaround, so not looking into this any more immediately. Thanks for the help.
Glad you got it solved! Where should probably be available, though: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/where_1
i've had where
work for me in my windows 10 environments -- unlike which
i think one can get multiple results
right, as you said before dir
is a built-in. i presume that's why it doesn't show up.
Once I've written a clojure program, are there any ways to decrease the startup time? It seems rather large. Can I compile to binary or something?
The cry of every JVM engineer in history... The short answer is: most of the startup time you're seeing is likely to be the JVM itself starting. There are numerous posts on tuning that startup time. As for compiling to binary - there is https://www.graalvm.org/docs/reference-manual/native-image/ but I've found it a difficult beast to wrestle with so far. There's also the possibility that the code itself is trying to do something at startup - so you should minimise that. This is part of the reason that writing command line apps is one of the few times I don't reach for Clojure.
Is there a cousin of map
which does not accumulate return values? for example simply returns the sequence it iterated over?
doseq
?
oh wait you want to return the original seq
or run!
which works exactly like two-arg map
but returns nil
We might be interpreting the question differently. Map returns a lazy sequence. Whether you realize it all, is up to you.
(take 5 (map (partial * 10) (range)))
;; => (0 10 20 30 40)
Actually I don't care what the return value is, I just want to call something like map
and not accumulate the return values.
(doseq [x items]
(do-something-with x))
in Common Lisp there is a function mapc
which is like the clojure map
but it simply returns its 2nd argument without accumulating anything, and calls the given function iteratively on each element of items
presumably for side effect. Thus it returns false
if it didn't iterate over anything, and returns true
if it did. something like the following (mapc println items)
which turns items
But i'm happy with doseq
, it does what I need.@jimka.issy this is what run!
is for
Can someone please help me understand this error message?
clojure-rte.core> (let [a '((1 2 3) (2 1 3) (1 3 2) (3 1 2) (2 3 1) (3 2 1))
b #{(1 2 3)
(1 3 2)
(2 1 3)
(2 3 1)
(3 1 2)
(3 2 1)}]
(= (set a) b))
Execution error (ClassCastException) at clojure-rte.core/eval20206 (form-init4386036104585295471.clj:15176).
class java.lang.Long cannot be cast to class clojure.lang.IFn (java.lang.Long is in module java.base of loader 'bootstrap'; clojure.lang.IFn is in unnamed module of loader 'app')
clojure-rte.core>
IFn is the interface for things you can call
(f) in clojure means "call f"
(1) means "call 1"
1 isn't an IFn, you can't call it
Oh, is #{(1 2 3)} trying to call the function 1 ? I just need to quote the items.
or use [] instead of () to get a sequential collection
you can quote the entirety '#{(1 2 3) ...}
or use vectors #{[1 2 3] ...}
both would work here
so [] interprets its contents as literals, and #{} evaluates them.
[] builds a collection and takes no further action
() builds a collection then executes
' prevents the execution step if any, and also prevents resolving symbols to their current value
this might help see some of the differences:
ins)user=> (def a 1)
#'user/a
(ins)user=> (= [a] '(1))
true
(ins)user=> '(a)
(a)
(ins)user=> [a]
[1]
I’m having trouble identifying the source of a bug. Isn’t there a way to pause execution at specific points and evaluate what’s going on in that environment? e.g. in the R
language you can use the browser()
function to stop evaluation and give you back control at thatpoint
there are "debug repl" implementations that allow that, but in practice since our data types are immutable, it suffices to capture data in scope (a simple def
inside your function can do that while debugging)
or the new tap>
facility
I use Cursive and so do have the ability to set breakpoints but I actually find myself using https://github.com/dgrnbrg/spyscope more.
Is there a cousin of let
which lets the n'th binding rever to bindings 0 through n-1 ?
In Common Lisp it is called let*
in scheme it is called letseq
equivalent of:
(let [a 1]
(let [b (+ 1 a)]
(let [c (+ a b)] ...
?user=> (let [x 5 y (inc x)] y)
6
Are you sure? experiments show otherwise.
clojure-rte.core> (let [a 100]
(let [a a]
a))
100
clojure-rte.core>
ahhh I see, each expression on the right hand side can use any variable above on the left hand side. yes it seems you're right.
So we cant swap a and b with
(let [a b
b a] ...)
like in Common LispTrue, but you can do (let [[a b] [b a]])
I think…
Yeah
(let [a 1 b 2
[a b] [b a]]
[a b])
=> [2 1]
And apprently I can bind the same value multiple times. That's cool!
(let [a (f)
a (remove-duplicates a)
a (sort a)]
...)
if its a series of transforms youre doing using one of the threading macros may be useful:
(-> (f) remove-duplicates sort)
https://clojure.org/guides/threading_macros
is lein install
the preferred way to get local dependencies into your classpath in a lein project?
it is the lowest complexity, but it's still inconvenient (eg. requires restarting a repl to see changes in the lib)
with deps.edn you can use :local/root to declare a file-system dep, there are plugins for lein that attempt to do similar (but less convenient in my experience)
cool, thank you very much. I think it is high time for me to seriously consider making the switch
just a pedagogical example. the general case might be more than just a series of transforms of course
If I have a sequence of objects, and from each object I can extract a key and a value, how can I best build the hashmap from that?
this sounds like a job for reduce:
(reduce (fn [m o] (assoc m (get-key o) (get-val o)))
{}
object-list)
actually, seeing your example, map
suffices - I thought you wanted all the keys in one map
user=> (map (fn [[idx kvs]] (into {} (map (fn [[_ k v]] [k v])) kvs)) input)
(#:clojure-rte.core{:Fox 1, :Cat 1} {:sigma 3} #:clojure-rte.core{:Wolf 4} #:clojure-rte.core{:Lion 2} {})
*edit: swap out map for mapv if vector is a strict requirementclojure-rte.core> (rte-to-dfa '(:cat (:or ::Cat ::Fox) :sigma ::Lion ::Wolf))
([0 [[0 :clojure-rte.core/Fox 1] [0 :clojure-rte.core/Cat 1]]]
[1 [[1 :sigma 3]]]
[2 [[2 :clojure-rte.core/Wolf 4]]]
[3 [[3 :clojure-rte.core/Lion 2]]]
[4 nil])
clojure-rte.core>
I'd like t to convert this to an array of hashmaps
[ { :clojure-rte.core/Fox 1
:clojure-rte.core/Cat 1}
{:sigma 3}
{:clojure-rte.core/Wolf 4}
{:clojure-rte.core/Lion 2}
{}]
This represents a finite-automaton. where arr[n] is a map of transitions, the key is the transition label, and the value is another index in the array.
including arr[4] = {} as there are no transitions from state 4
@jimka.issy hmm interesting puzzle, what is rte-to-dfa (..something something finite automata?) and is this the ideal place to do this conversion... if so i can think of a few hacky ways to get that output from that input, but nothing especially elegant comes to mind... maybe someone has better ideas on changing a sequence of vectors into a hash map reliably while dropping a bunch of index vals
I have a working solution in a single call to map in the thread above
I came up with a solution.
what is rte? it stands for rational-type-expression. You can read something about it in this blog posthttps://blog.ambrosebs.com/2020/03/30/what-type-clojure.html
It is a mathematically rigorous approach to spec. I'm converting my phd thesis from Common Lisp to clojure. I don't want to promise too much but it is an interesting project for me to learn clojure.
nice. best of luck with your projects! it's interesting what it will look like now, and then what you would make it look like with clojure experience after a few seasons.
People asserts that a Clojure codebase has more parenthesis than those in other languages, is it true though? I think it could be true for the density of parens, but I believe that we have fewer lines and parens in absolute number for the same number of feature than other languages?
paren count should be quite comparable as well - f()
becomes (f)
- the paren count for most things remains the same
In python, you can access properties and create mathematics without parens.
So maybe there they win.
@neo2551 We probably have more ( )
in arithmetic expressions but fewer overall than C-family languages.
(and, yes, the productivity factor can mean a substantial reduction in lines and therefore "punctuation" too)
The domain expired for http://www.4clojure.com
Alan Malloy would be my best guess as to who might be able to address that (I don't think he's on here). Repo is at https://github.com/4clojure/4clojure - 4clojure has not really been updated in many years
I'll relay the message
he's intentionally avoided changing the clojure version on the site (as that makes comparison to existing solutions unfair or might even break a few older solutions)
Thanks a lot for your answer! @seancorfield
suppose my data structure is a two dimensional vector. is there a more idiomatic approach to getting values from that matrix than (nth (nth...))
? i'm seeing clojure style guides advise against index based collection access.
What about get-in?
that could work
get-in works with vectors, as long as you know you won't have seqs mixed in
Hey! I while ago someone shared a website that, given the inputs of the function, returned the functions that could be called. It was specific for Clojure, and it was pretty nice because you could find functions you didn't know existed. I'm trying to look for it on Google, but I can't. Does someone have it?
IIRC it was a library from Raynes that did this - it's actually not so hard to build from scratch
Hello expertes, i'm trying to do some basic java interop but it's not working, what i'm doing wrong here?
(import java.security.SecureRandom
java.math.BigInteger)
user> (.probablePrime java.math.BigInteger 1024 (SecureRandom.))
Execution error (IllegalArgumentException) at user/eval14128 (form-init5284286588528896621.clj:155).
No matching method probablePrime found taking 2 args for class java.lang.Class
Should be (BigInteger/probablePrime 1024 (SecureRandom.))
since probablePrime is a static method of the class, it's accessed like Class/staticMethod
ohh now i get it, thanks 🙂
nice to know
does static means that i don't have to instantiate it?
Yeah, static means that it belongs to the class rather than an instance of a class
for instance, Math/sqrt
oh, ok
apparently i'm not casting the method on the right class