This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-05
Channels
- # announcements (17)
- # architecture (5)
- # babashka (12)
- # beginners (155)
- # calva (18)
- # chlorine-clover (2)
- # cider (57)
- # circleci (2)
- # clojure (151)
- # clojure-europe (4)
- # clojure-gamedev (20)
- # clojure-italy (18)
- # clojure-nl (4)
- # clojure-norway (3)
- # clojure-spec (8)
- # clojure-uk (95)
- # clojurescript (70)
- # core-async (68)
- # css (3)
- # data-science (13)
- # datascript (1)
- # datomic (16)
- # docker (2)
- # figwheel-main (41)
- # fulcro (34)
- # graalvm (6)
- # graphql (7)
- # jobs (14)
- # joker (2)
- # kaocha (1)
- # leiningen (2)
- # malli (3)
- # midje (2)
- # overtone (1)
- # reagent (8)
- # reitit (6)
- # ring-swagger (1)
- # schema (2)
- # shadow-cljs (6)
- # spacemacs (3)
- # specter (5)
- # timbre (3)
- # uncomplicate (1)
Does .contains
works for searching if a string contains a words such as
(.contains "Hello Worlds!" "World")
My repl seems to not recognize that functions
when was string/contains? added? I don't see it in 1.10.1
Yeah I am using clj 1.10.1
maybe you meant clojure.string/includes?
includes?
can work
@contact904 generally javadoc for a type will show you what should work, but clojure often has functions that are more convenient to use
Also, clojure.string
is supported in ClojureScript too so you can write portable Clojure/Script using clojure.string
functions, whereas the Java interop only works in Clojure.
that's true, the big issue with interop is losing portability
What is the Clojure equivalent for Java's (byte) 255
=> -1
? (byte 255)
returns an error.
@jatkin unchecked-byte
there's a whole family of those
user=> (apropos "unchecked-")
(clojure.core/*unchecked-math* clojure.core/unchecked-add clojure.core/unchecked-add-int clojure.core/unchecked-byte clojure.core/unchecked-char clojure.core/unchecked-dec clojure.core/unchecked-dec-int clojure.core/unchecked-divide-int clojure.core/unchecked-double clojure.core/unchecked-float clojure.core/unchecked-inc clojure.core/unchecked-inc-int clojure.core/unchecked-int clojure.core/unchecked-long clojure.core/unchecked-multiply clojure.core/unchecked-multiply-int clojure.core/unchecked-negate clojure.core/unchecked-negate-int clojure.core/unchecked-remainder-int clojure.core/unchecked-short clojure.core/unchecked-subtract clojure.core/unchecked-subtract-int)
(defn reduce-over-sql-statement
"Reduces over the nodes in a sql statement from left to right.
Will terminate early if the reducing function returns a
(reduced ...) just like normal reduce."
[root-sql-statement acc-zero reducing-fn]
(loop [acc acc-zero
statement-stack (list root-sql-statement)]
(if (empty? statement-stack)
acc
(let [statement (first statement-stack)
next-acc-value (reducing-fn acc statement)]
(cond
(reduced? next-acc-value)
@next-acc-value
(instance? SqlNodeList statement)
(recur next-acc-value
(concat statement statement-stack))
:else
(recur next-acc-value
(rest statement-stack)))))))
I don't particuarly understand IReduceInit, but it feels as though there is some way to just hook into the builtin reduce with a tad more logic
@emccue Can you provide a bit more context around what you're trying to do?
I am trying to parse sql scripts and extract the columns and tables that were written to and the ones that were read from
I'm trying to understand your two recur
cases -- you're reducing using acc
and the top of the statement list in both cases, but if the first element is a SqlNodeList
, you're concat
'ing the first element to the front of the whole stack which includes that first element???
But then it will reduce on the sql node list and then... reduce on each element of that as well?
I am half translating this https://github.com/intuit/superglue/blob/master/parser/src/main/scala/com/intuit/superglue/pipeline/parsers/CalciteStatementParser.scala
(I'm assuming sql node list behaves like a Clojure list?)
I am translating this code because calling scalac from leiningen isn't supported and I need some basic parsing to be able to test the rest of what I am working on
and seperate the visitor thing would mandate a bit of local mutability with an atom to translate 1-1
Posting that large a code fragment inline killed my Slack client. You should post a link to code that big 😞
Ah, yes, I removed the expansion of it now.
It auto-expanded the link 😞
I don't see SqlNodeList anywhere in that file...?
...which is more involved since there isn't a straightforward way to remove the mutability
If you walk the tree of nodes, transforming each node into a pair of inputs and outputs, merging them as you go, you'll have no mutability.
A multimethod would probably be the easiest way to handle the transformation?
(since the default could produce [[] []]
and you'd only have to implement transforms for the node types that actually have input/output components -- I bet the resulting Clojure code would be a lot shorter than that Scala code)
Likely what you'll want is to write an evaluator for sql expressions, where the result is the set of selected columns (instead of a sql result set) which would let you do things like check that tables referenced in where clauses are defined in from clauses (the evaluator will build an environment and check that tables mentioned exist in the environment). https://en.m.wikipedia.org/wiki/Abstract_interpretation
(java interop) how do I instantiate a (non-static) inner class in clj? googling, only references I see are for instantiating static nested classes
I haven't done this recently, if at all, but often inner classes are named OuterClass$InnerClass, so perhaps something like (OuterClass$InnerClass. my constructor args here)
somehow, I need to refer to the outer-class instance when instantiate an actual, non-static inner class
this one doesn't work: https://gfredericks.com/blog/3
ah, I was totally thrown off by Cursive. it flags this with incorrect ctor arity:
(OuterClass$InnerClass. outer-instance)
but it does work if you just run it
it's because the reference to the parent instance is implicit. what a complex language this Java is
Is there an idiom like side-effect!
but for when you reference an external symbol inside a function.
I realised it would need to be on the actual symbol as well.
Like say you have (defonce bucket-name "my-aws-bucket")
and you use that throughout your functions in that ns. It would be nice to quickly see what isn’t passed to the function. Like @my-atom
is usually a sign. I know my example is not that good because the top level function itself would be a side-effecting one due to aws.
It would make glancing over the functions to see how pure they are faster. Though we could just simply pass it in as a param I guess..
Actually the downside of that would be using a function defined in said namespace from another namespace. I’ll just keep it as is.. it would be nice to see at a glance what is coming from outside of the function. Though I guess there is no way because then it’s implying that that symbol is always used without being an explicit input.
I’ll get to Racket one day 🙂
Does anyone have any experience of intercepting java.util.logging log writes? I would like all my logs to go via timbre and I am aware of libraries like slf4j-timbre, but iiuc I am dealing with java.util.logging, not SLF4J and I can't seem to find an equivalent library
There is a jul-to-slf4j, which will send everyting logged by java.util.logging to slf4j, and from there to what ever destination slf4j is configured to use
I guess you could try jul-to-slf4j and slf4j-timbre together? Something like this: jul -> slf4j -> timbre
I’m trying to access a directory to get a list of files. Conventional knowledge suggests I use (-> (
. While this works normally in dev, it fails once uberjarred because the directory is moved as informed by the classpath. If it were just a file, I would’ve just used (io/resource "relative/path/to/file")
, but if it’s a dir I get an exception complaining that the path does not lead to a file. Does anyone have any solution for this?
You might try resauce as a workaround for common classpaths, but it isn't general purpose
oof 😞 At least now i know that it isn’t possible. At least i have a workaround in mind.
Thanks for responding!
@emccue I have a lightning talk on this from a few years ago: https://www.youtube.com/watch?v=FjKnlzQfAPc
you can extend CollReduce if you want a protocol
IReduceInit is helpful if you are making a Java thing
(which has no way to extend a protocol)
IReduceInit is also a better factoring of CollReduce, which has an extra arity without the "init" arg
how do test fixtures compose? I've got a test with multiple fixtures and I don't understand how to call those fixtures with the test manually from the repl. Well, I do, but not multiple fixtures
they are functions, you can use comp
or nest them by hand
there's also a function in clojure.test that runs a test with your fixtures
I can call one fixture from the repl by doing (foo-fixture mytest)
, and that works fine. But when I try (base-fixture (foo-fixture mytest))
it blows up because foo-fixture doesn't return the test fn
oh, right - my bad clojure.test/compose-fixtures
or you can just use clojure.test/test-var which uses your fixtures iirc
They're interchangeable only in the simplest case. And in that case, I prefer for
simply because there's no extra function.
I prefer for
, since the collection comes before the 'body', which is more readable. It also lets you filter, assign variables, etc (`:let` and :when)
without increasing nesting further.
I usually reach for map
when the function is already named, or a one-liner, and I don't need the extra features of for
like :let
or :when
, perhaps just from habit.
map
is also nice sometimes because it lets you move to similar functions like mapv
, map-indexed
, keep
, etc with a very small diff
Just to give a diversity of viewpoints -- I avoid for
. It's a personal aesthetic preference. I would rather use into
with a transducer if I need something eager, eduction
with a transducer if I need something lazy. I only choose for
if it's the preferred convention of the project I'm working in.
hi there! is there a way to see the keys for a record in the repl? my colleague @ivanjensen1 is interested in this 😄
for instance we would like to explore the properties of datascript.db.Datom
user=> (defrecord Foo [a])
user.Foo
user=> (->Foo :a)
#user.Foo{:a :a}
user=> (keys (->Foo :a))
(:a)
user=>
Oh right I could definitely do that 😄
but you might only be interested in the declared keys that a record must have, not just what keys a particular record object has
the class generated by defrecord has a static method which you can call to get those
oh cool let me try that
thank you folks this is helpful
Is there any consideration about security when I declare a function as private? It is private as it uses a connection to the DB. However, I wanna use this function outside the namespace. Should I wrap it with a public function?
private/public are pretty much just documentation. they don't have any security implications
oh, that is good to know
this is my trick for checking keys of a record
user=> (defrecord Bar [a b c])
user.Bar
user=> (keys (map->Bar {}))
(:a :b :c)
i was thinking of a way to find min\max elements and respective indices in a vector and came up with a such contraption, is there a better way?
(def v1 (vec (take 20 (distinct (repeatedly (partial rand-int 100))))))
=> [81 61 13 21 3 98 48 92 72 30 37 2 15 71 34 73 82 50 52 29]
(reduce (fn [acc [idx val :as e]]
(let [update-fn (fn [v f]
(if (f val (peek v))
[idx val]
v))]
(if (empty? acc)
[e e]
(-> acc
(update 0 update-fn <=)
(update 1 update-fn >=)))))
[] (map-indexed vector v1))
=> [[11 2] [5 98]]
is the reason that you aren't doing (apply max v1)
that you want the index as well?
a literal vector would likely be easier to read than two chained updates
you could also use (.indexOf v1 (apply max v1))
to get the index
https://stackoverflow.com/questions/4830900/how-do-i-find-the-index-of-an-item-in-a-vector
@doby162 wouldnt linear search increase complexity? I'm not very proficient with algorithms yet.