Fork me on GitHub
#beginners
<
2023-05-08
>
Sam Ferrell19:05:03

Is there an alternative to (fn [x] (if x 1 0))? Nothing wrong with this function just wondering if theres a built-in for coercing a value to 1 or 0 depending on whether its truey or falsey

dpsutton19:05:43

you can make it more complicated. but that’s a very readable function (def foo (comp {true 1 false 0} boolean)) . Is more characters and less clear.

leif19:05:25

Is there any way I can read and write a record? Say for example:

(ns test.core
  (:require [clojure.data.xml :as xml]
            [clojure.tools.reader :refer [read-string]]))

(read-string (pr-str #xml/element{}))
gives me
#error {:message "No reader function for tag xml/element.", :data {:type :reader-exception, :ex-kind :reader-error}}

hiredman19:05:19

#xml/element {} isn't a record (which have a related but distinct readable format) it is a tagged literal

hiredman19:05:10

The doc string for read-string should tell you how to plugin in handlers for different tags

hiredman20:05:29

user=> (defrecord F [a])
user.F
user=> (require 'clojure.edn)
nil
user=> (pr-str (->F 1)) ; record printing using literal record syntax
"#user.F{:a 1}"
user=> (read-string (pr-str (->F 1)))
#user.F{:a 1}
user=> (clojure.edn/read-string (pr-str (->F 1))) ; edn reader doesn't support literal records
Execution error at user/eval163 (REPL:1).
No reader function for tag user.F
user=> (defmethod print-method F [o w] (.write w "#foo/bar ") (print-method (into {} o) w)) ; install a method to serialize the record as a tagged literal
#object[clojure.lang.MultiFn 0x6d4a65c6 "clojure.lang.MultiFn@6d4a65c6"]
user=> (pr-str (->F 1))
"#foo/bar {:a 1}"
user=> (clojure.edn/read-string (pr-str (->F 1)))
Execution error at user/eval171 (REPL:1).
No reader function for tag foo/bar
user=> (clojure.edn/read-string {:readers {'foo/bar map->F}} (pr-str (->F 1)))
#foo/bar {:a 1}
user=> (type (clojure.edn/read-string {:readers {'foo/bar map->F}} (pr-str (->F 1))))
user.F
user=>

leif19:05:32

Oh wait, this might be specifically a clojurescript problem.

sheluchin20:05:03

Is there some way to inspect a tap> (using Reveal, but presuming the question is more generic) and see where in the filesystem it is invoked from?

seancorfield20:05:48

You can tap> any data you want, so you could probably write a macro that would invoke tap> on some data but also pass file/line data obtained by the macro.

seancorfield20:05:35

In Portal, there's a portal.console/log macro which "logs" a value using tap> but also captures the file/line data...

sheluchin21:05:12

@U04V70XH6 thanks for the link. Your posts keep nudging me towards trying Portal.

jpmonettas11:05:52

probably not the same, but maybe works for you, in #C03KZ3XT0CF you have #tap-stack-trace , you can put it in front of any form and it will tap the current stack trace, which contains file info and line

👍 2