This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-07-30
Channels
- # announcements (4)
- # babashka (8)
- # beginners (124)
- # calva (13)
- # cider (10)
- # circleci (6)
- # clj-kondo (193)
- # cljdoc (1)
- # cljs-dev (4)
- # clojure (50)
- # clojure-europe (28)
- # clojure-serbia (1)
- # clojure-spec (22)
- # clojure-uk (30)
- # clojurescript (11)
- # clojureverse-ops (3)
- # community-development (1)
- # conjure (5)
- # cursive (1)
- # datomic (11)
- # depstar (1)
- # events (2)
- # fulcro (7)
- # graalvm (2)
- # graphql (10)
- # helix (43)
- # hyperfiddle (14)
- # introduce-yourself (6)
- # jobs (2)
- # jobs-discuss (14)
- # kaocha (4)
- # luminus (2)
- # malli (24)
- # meander (6)
- # off-topic (4)
- # pathom (1)
- # polylith (13)
- # re-frame (6)
- # releases (1)
- # remote-jobs (1)
- # sci (14)
- # shadow-cljs (209)
- # tools-deps (30)
- # xtdb (26)
I looked at the documentation of butlast
and droplast
. I must admit that I don't understand the difference. there are some examples, but but I was unable to reconstruct the difference by looking at the examples.
drop-last takes an optional n for number of items to drop. without that it defaults to 1 and essentially being the same as butlast. eg (drop-last 2 [1 2 3])
another difference is at runtime. butlast
returns processed result (in linear time) and drop-last
- lazy sequence (constant time), it will spend time dropping when sequence is consumed. Also drop-last
will not materialize input lazy sequence - useful if input sequence is expensive to build
Hey guys, I have a bit of a generic question: discoverability of functions in CJ.
In an OOP language, if you have an object, the object will have all the methods bound to it and – at least in Ruby – it's very easy to discover them: [].methods
will give me all the methods for an array. I can want to know what are the methods that are specific for array, so I'd do [].methods - Object.new.methods
. And so on. And given that Ruby is very purely OOP language, everything is a method, so everything's discoverable in this manner.
Taken even further, there was a gem, I think it's called what_method
or something like that. It allows one to do say [1, 2, 3].what?(6)
and it would go through all the array methods, call it and if the result would be 6
in this case, it'd return name of that method (`sum` for instance).
So far my way of dealing with this was to Google a method name and if nothing reasonable pops up, then ask in Slack, but I'm sure there must be better methods.
How do you discover functions? I'm open to a wide range of suggestions, for instance I'm yet to set up LSP, let's see whether that helps, I haven't really used it ever, so I have no clue.
Cheers!
Given that Clojure is open for extension by design, there's almost never going to be a fixed number of functions that could apply to some value -- but the cheatsheet doc probably would help narrow your search. I think there also used to be a service run by someone here that you could feed two values and it would try to deduce which single function would transform one to the other... I don't remember its name/URL or who wrote it...
OK. Change of the mindset is required then. Noted 🙂
https://clojuredocs.org/clojure.repl/apropos could help, perhaps.
like if you had some global function registry, you can take a schema or an example input and show all functions for which the example would be valid input
namespaces and vars have documentation attached to them and queryable. (doc clojure.test)
gives an essay about how to test things.`(apropos "var")` lists functions that might be relevant for vars. (find-doc "var")
will search documentation for what functions talk about vars in the documentation
Isn't it what https://github.com/borkdude/re-find does already?
@emccue yeah I was wondering something along these lines, with spec.
@dpsutton OK find-doc
sounds promising.
Does anybody have a recommendation for a tool that can visualize the dependency graph of project namespaces?
only works with single argument functions defined in a very specific way - but i'm sure someone smarter and with more time could generalize it
someone wrote a site that would search based on example inputs and outputs (not spec)
https://github.com/Raynes/findfn must be what I was thinking of (not a website)
I used spec because findfn gave multiple "false positives", like things that happen to work but you would probably never use as such.
(defn julian-day
"The Julian day is the continuous count of days
since the beginning of the Julian period."
[^java.time.Instant inst]
(let [utc (.atZone inst (java.time.ZoneOffset/UTC))
yr (.getYear utc)
mo (.getMonthValue utc)
year (if (<= mo 2) (- yr 1) yr)
month (if (<= mo 2) (+ mo 12) mo)
day (.getDayOfMonth utc)
hour (.getHour utc)
minute (.getMinute utc)
second (.getSecond utc)]
(+ (- (- (+ (java.lang.Math/floor (* 365.25 (+ year 4716)))
(java.lang.Math/floor (* 30.6001 (+ month 1)))
day) 13) 1524.5)
(/ (+ hour
(/ minute 60)
(/ second 3600))
24.0))))
So this function satisfies this test.
(let [reference-instant (java.time.Instant/ofEpochSecond 946749600)]
(is (= 2451545.25 (sut/julian-day reference-instant))))
Is there a library that does this out of the box? I’ve been looking at https://www.threeten.org/threeten-extra/apidocs/org.threeten.extra/org/threeten/extra/chrono/package-summary.html and https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/time/temporal/JulianFields.html#JULIAN_DAY. I’m having a lot of trouble understanding how to use them though, let alone understand if they work for my use-case.Nevermind, I totally got it!
(defn julian-day
[^java.time.Instant instant]
(let [utc (.atOffset instant (java.time.ZoneOffset/UTC))
hour (.getHour utc)
minute (.getMinute utc)
second (.getSecond utc)]
(+ (.getLong utc
(java.time.temporal.JulianFields/JULIAN_DAY))
(- (/ (+ hour
(/ minute 60)
(/ second 3600))
24.0) 0.5))))
I'm following http://troydm.github.io/blog/2016/04/11/writing-parser-combinator-library-in-clojure/. It's well written but some of the code seems a little non-idiomatic. Specifically, they're using syntax-quoted lists in function bodies to return values that are destructured like regular ol' vectors:
(defn- line-col
"find out line/column at position p in String s"
[s p]
(let [len (.length s)]
(loop [p p l 1 c 1]
(if (<= p 0)
`(~l ~c)
(if (>= p len)
`(~l ~(+ p c))
(if (= (.charAt s p) \newline)
(recur (- p 1) (+ l 1) 1)
(recur (- p 1) l (+ c 1))))))))
;; actual usage:
(let [[l c] (line-col ...) ...)
Is there some advantage here to syntax-quoting, or to returning lists rather than vectors?seems odd to me. I would personally prefer (list l c)
over the syntax quote version
it is fairly uncommon to do that in clojure, because we have some data structure choices that don't require quoting like vectors, but is seems fine
I believe it(the equivalent, but of course not exactly syntax quote) gets used more often in other lisps where you don't have the vector option (I've got some common lisp code that does it a lot)
also intersting to compare the syntax quote version vs. just calling list
> (macroexpand-1 (quote `(~a ~b)))
(clojure.core/seq
(clojure.core/concat (clojure.core/list a) (clojure.core/list b)))