This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-06-23
Channels
- # beginners (93)
- # boot (3)
- # cljs-dev (5)
- # clojure (49)
- # clojure-india (8)
- # clojure-russia (1)
- # clojure-spec (4)
- # clojure-uk (37)
- # clojurescript (33)
- # cursive (4)
- # datomic (4)
- # fulcro (11)
- # graphql (1)
- # lumo (16)
- # mount (1)
- # off-topic (21)
- # onyx (2)
- # portkey (4)
- # shadow-cljs (3)
- # spacemacs (4)
- # sql (1)
- # vim (1)
Hi guys! I'm writing little clojure scripts. And I tried to use clojure.tools
but it should be installed. So I wonder if I can do something like in ruby: gem install clojure.tools
to get latest version and just continue scripting.
You can use leiningen build tool. Install lein. Run lein new app
You have to add all dependencies to project.clj
Lein run
I know about lein. I am writing scripts. I dont need a whole project, I just need a single library
Will download jars
In yourscript do you have ns macro?
(ns mynamespace.scripts (:require [clojure.tools :as tools])
That will import clojure.tools into your code
You can use (tools/a-fn )
You dont hear me. I know about lein and I know how to require library. The question is can I install it with one command to have it in classpath via using clj
You can download the jar to project folder, add it to classpath before starting clojure, or add the jar in the Java -cp myjars:clojure.jar clojure.main myscripy.clj
@non.dmitriy The clj tool will find dependencies on your deps.edn file and download them on first invocation. It doesn’t require a separate step like gem/pip/npm do.
If you are writing scripts, you can even pass the deps.edn as a command line argument and it will be the same.
@orestis yeah, i've read this. But I REALLY dont wanna have more than one file per script.
so this is not even near to what I asked about. It's not really much different from having a lein project
I’m also a beginner but as far as I know, there are a couple of ways to accomplish them — unfortunately I’m on mobile so can’t dig around too much.
First one is to invoke clj passing in the dependencies as command line arguments. Essentially typing out your deps.edn on command invocation.
And I think there might be a way to have a hash bang #!/usr/bin/clj
that even includes deps as command line args.
Some discussion here https://clojureverse.org/t/scripting-with-clj/1618
Hello I'm trying to resolve the and symbol but it returns true instead of false here. How can I make it work?`((resolve (symbol "and")) true false)`
@kwcharllie379 and
is a macro so I guess you need to dig in this direction
ahh I forgotten about that
So I should do macroexpand 😛
@orestis well, turns out shebang requires to use only one option without whitespaces so I cant pass deps hash to clj
. I can create some wrapping script but it kinda smelly:(((
Macroexpand does not work as well. Do not know how to do it then.
@kwcharllie379 why can't you just use ((symbol "and") true false)
?
@karolinasokolowska you can't apply a macro as a function
So literally there is no possibility to somehow call it?
@kwcharllie379 try (eval (macroexpand (list (symbol "and") true false)))
so ((resolve 'and) true false)
is passing true
as &form
and false
as &env
, and no arguments
user=> ((resolve 'and) nil nil true false true)
(clojure.core/let [and__5324__auto__ true] (if and__5324__auto__ (clojure.core/and false true) and__5324__auto__))
Yes it's working
Now i see
ok here is my fn
(defn- logic-op [a-str b-str op]
(let [a-vec (s/split a-str #"")
b-vec (s/split b-str #"")]
(s/join
(map #(if (eval (macroexpand (list (symbol op) (= %1 "1") (= %2 "1"))))
1 0)
a-vec b-vec))))
@bronsa not sure if I got it. You suggest preparing a hashmap with functions including and
?
eval macroexapnds?
WOWOWOW
Guys you're great
My inputs are strings like "111" "1111001" and so on
REPL is (loop (print (eval (read)))
, eval
is (emit (analyze (macroexpand form)))


I have to put that information somewhere in my notes. Thank you @bronsa
thank you @non.dmitriy
I am still looking for solution to run a script with dependent library via single file with shebang.
#!/bin/sh
true;exec clojure -Sdeps '{:deps {my-deps-here}}' -i "$0"
true;exit
(require 'my-deps)
(println "hello")
The input is a list of elements.
The loop-body is to split the list into 2-element pairs and merge each pair (which I do via partition-by
and map appropriate-merge-fn
)
The loop-end-condition is to stop when the list has only 1 element
How would you execute the looping part? The clearest mechanism for me right now would be loop-recur, but I remember reading that people should try to use other functions. There’s iterate+drop-while, but that feels… clunky.
(Note: I know I could use reduce; bear with me, this is an exercise.)
Also, style question. For argument validation, would you throw IllegalArgumentException
or use {:pre []}
assertion?
@lady3janepl you can reduce over the list of pairs and terminate the reduction using reduced
@bronsa that’s brilliant, I needed that functionality for something else 😄 but not sure it would do what I want here, let me type out an example
Let’s say list elements are integers, and the merge function is +
. Then:
input: [ 1 2 3 4 5 ]
(enter loop)
after first pass: [ 3 7 5 ]
after second pass: [ 10 5 ]
after third pass: [ 15 ]
(loop terminates)
return: 15
The list with merged elements from the first pass gets chunked again in second pass.