This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-11-27
Channels
- # announcements (4)
- # beginners (41)
- # biff (8)
- # cider (14)
- # clj-kondo (5)
- # clojure (45)
- # clojure-brasil (1)
- # clojure-europe (20)
- # clojure-nl (1)
- # clojure-norway (30)
- # clojure-uk (10)
- # clojurescript (8)
- # cursive (25)
- # datomic (20)
- # emacs (11)
- # events (1)
- # hoplon (9)
- # humbleui (7)
- # hyperfiddle (6)
- # lsp (63)
- # matrix (1)
- # observability (20)
- # off-topic (36)
- # polylith (11)
- # re-frame (2)
- # releases (1)
- # rewrite-clj (6)
- # scittle (42)
- # sql (6)
- # squint (86)
- # tools-deps (9)
How to solve this error message :
----- Error --------------------------------------------------------------------
Type: clojure.lang.ExceptionInfo
Message: Could not resolve symbol: args
Data: {:type :sci/error, :line 27, :column 1, :file "/mnt/exercism-iteration/src/coordinate_transformation.clj", :phase "analysis"}
Location: /mnt/exercism-iteration/src/coordinate_transformation.clj:39:13
Phase: analysis
in this code :
(defn memoize-transform
"Returns a function that memoizes the last result.
If the arguments are the same as the last call,
the memoized result is returned."
[f]
(let [mem (atom {:last-x nil
:last-y nil
:last-result nil})]
(fn [& args]
(if (= (first args) (:last-x @mem))
(= (last args) (:last-y @mem))
(:last-result @mem))))
(let [ret (apply f args)]
(swap! mem assoc :last-x (first args))
(swap! mem assoc :last-y (last args))
(swap! mem assoc :last-result ret)
ret))
look at your parens, aka, whats in the scope of what
if you arent using a paren-balancer-plugin like parinfer, I do recommend finding one. if you have one you can work similarly to python, where you can largely look at indentation to determine whats in a given scope
Hello, I am trying to give smee/binary clojure lib a try to parse a binary encoded file. I have been reading up the code for this library's implementation. The following piece of code is not very clear to me.
(defn- map-invert [m]
{:post [(= (count (keys %)) (count (keys m)))]}
(into {} (for [[k v] m] [v k])))
(defn- strict-map [m lenient?]
(fn enum-lookup [k]
(if-some [value (m k)]
value
(if lenient?
k
(throw (ex-info (str "Unknown enum key: " k) {:enum m :key k}))))))
(defn enum
"An enumerated value. `m` must be a 1-to-1 mapping of names (e.g. keywords) to their decoded values.
Only names and values in `m` will be accepted when encoding or decoding."
[codec m & {:keys [lenient?] :or {lenient? false}}]
(let [pre-encode (strict-map m lenient?)
post-decode (strict-map (map-invert m) lenient?)]
(compile-codec codec pre-encode post-decode)))
when I call the enum function like the following, I get and error.
(enum :short-le {:byte 1
:ubyte 1
:short-le 2
:ushort-le 2
:int-le 4
:uint-le 4
:float-le 4
:double-le 8
:long-le 8
:ulong-le 8})
; Execution error (AssertionError) at org.clojars.smee.binary.core/map-invert (core.clj:470).
; Assert failed: (= (count (keys %)) (count (keys m)))
Can someone shed some light on the definition of the funciton called map-invert and explain what this function is doing... especially the 2nd line in the definition {:post [(= (count (keys %)) (count (keys m)))]}
? Many thanks in advance.The provided map has to be a one-to-one mapping. In your example, multiple keys in the input map have the same value. When the map is inverted, it fails the post condition because the inverted map will have less keys as a result of the collision. Specifically, your inverted map will only have 4 keys because there are 4 unique values.
The :post
bit is an assertion to be executed after the function runs. This is explained in more detail at <https://clojure.org/reference/special_forms#_fn_name_param_condition_map_expr_2>
also https://clojure.github.io/clojure/clojure.set-api.html#clojure.set/map-invert exists already
thanks for that @U04RG9F8UJZ! I am kinda a newbie with clojure still... and I am a bit confused. The code snippet I pasted is from the actual demo of the library https://github.com/smee/binary/blob/master/src/org/clojars/smee/binary/demo/matlab5.clj. Has anybody else done any binary file parsing using clojure?
you may just want something like https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html
@U022LK3L4JJ in the sample, they do an enum of element-types
, not element-type-sizes
thanks @U0NCTKEV8! This looks promising... in order to avoid clojure/java interop... do you know if there is a clojure library for this?
@U04RG9F8UJZ aaah yes you are right, let me try that again
@U0NCTKEV8 ok... if I am to do interop... do you know how to set seek in clojure?
or will I use java routines?
newbie remember... apologies for pedantic questions
what is the best way to read a file using http://clojure.java.io but then jump from one file location to another file location within the same file?
thanks @U0NCTKEV8! However is this efficient if the file size is a concerns(in GBs)?
Depends what you are doing, seek is as efficient as it can be, but if your usage pattern involves constantly jumping back and forth across gigabytes you may have issues
Eric Rochester on memory-mapped files: http://www.ericrochester.com/posts/code/parallel-io-with-mmap/index.html
I am interested in implementing machine learning using clojure.
Ah, I didn't even know about that channel...
what is the most clojure idiomatic way of defining a bunch of constant values?
Most values in Clojure are "constant", so def
would be an easy choice here
unless you're setting up some sort of "dictionary" of mappings of X -> Y, in which case a hash-map might be what you want
for instance, I'm currently making a translator of php-dateformatter characters to java ones, which currently looks like
(def php-fmt-char->java-fmt-char
{"Y" "yyyy"
"m" "mm"
"d" "dd"
"H" "HH"
"i" "mm"
"s" "ss"
"c" "yyyy-MM-dd'T'HH:mm:ssXXX"})
@U02FVPF04A1 yes this is what I have in my code at teh moment... a long list of defs
It's more idiomatic to have one def
with a hash-map to define values than a long list of def
expressions,
e.g. https://github.com/practicalli/practicalli.github.io/blob/live/src/practicalli/data.cljs