This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-05
Channels
- # 100-days-of-code (1)
- # announcements (7)
- # beginners (84)
- # boot (1)
- # cider (22)
- # cljdoc (14)
- # cljs-dev (45)
- # cljsrn (6)
- # clojure (65)
- # clojure-conj (7)
- # clojure-finland (1)
- # clojure-italy (7)
- # clojure-nl (2)
- # clojure-serbia (1)
- # clojure-uk (111)
- # clojurescript (58)
- # cursive (8)
- # datomic (68)
- # duct (1)
- # emacs (33)
- # figwheel (3)
- # figwheel-main (9)
- # fulcro (33)
- # graphql (1)
- # juxt (30)
- # kaocha (4)
- # off-topic (22)
- # pathom (47)
- # pedestal (4)
- # planck (6)
- # re-frame (1)
- # reagent (1)
- # reitit (13)
- # shadow-cljs (49)
- # spacemacs (7)
- # sql (6)
- # tools-deps (60)
I have a list with strings that I need to turn into just the strings
'("foo" "bar" "baz") => "foo" "bar" "baz"
It has to be in a collection. Or if u want to do something with the individual strings u can use some kind of iteration
@rafael.mshidomi How are you calling sh? I assume it takes a string, so you can use clojure.string/join
:
(clojure.string/join " " '("foo" "bar" "baz"))
"foo bar baz"
If you want it as a string of string, you can use pr-str:
(apply pr-str '("foo" "bar" "baz"))
"\"foo\" \"bar\" \"baz\""
It needs sperate strings for the commands:
tools.core> (shell/sh "ls -la")
IOException error=2, No such file or directory java.lang.ProcessImpl.forkAndExec (ProcessImpl.java:-2)
I'm trying to make a function like this:
(defn run-azure-command
[cmd]
(println
(shell/sh "az" cmd)))
@shidima You just want apply then.
(apply shell/sh "az" cmd)
Where cmd is a list of commands like '("foo" "bar")@shidima this works for me
test=> (def foo ["ls" "-la"])
#'test/foo
test=> (apply clojure.java.shell/sh foo)
https://clojuredocs.org/clojure.core/apply is what you were looking for 🙂
you're welcome:v:
The Clojure Style Guide: https://github.com/bbatsov/clojure-style-guide
Thanks @jumar I was reading that one, but it doesn’t say how to sort name funcs, I don’t think that matters much
Clojure compiler requires functions to be defined before they are used so it implies this sort of ordering. Other than that it's probably a good idea to group related functions together. If there's more to your question, I might be missing something...
I was considering in doing in alphabetical order, but that wont be good because I would have to do a lot of (declare)
Probably doesn't matter much. That said it probably is a good idea to separate critical functions from non-critical functions, as well as functions with side-effects (!) Another great paradigm I've seen used is for splitting up libraries / code: Input → Events → Actions → Output
If your goal in sorting them in alphabetical order is to be able to find them more quickly later, another approach is to use an editor/IDE that lets you jump to a definition, no matter which file it happens to be in, from other places where that name is mentioned.
Thanks for your helps, that clear it up, I like the Input → Events → Actions → Output
Hai, I have a q regarding clojure spec. Lets say I have a spec for a higher order function with an int predicate as argument, and I spec this arg like so: (spec/fspec :args (spec/cat :arg int?) :ret boolean?)
, how could clojure know that I am not secretly providing a string predicate? The type of the argument of a (- for example -) anonymous function is not recorded right, so there is no way of knowning that (fn [a] (= a "spaghetti"))
is not an int pred right?
by that I mean, spec is not a mechanism for reasoning statically(meaning without running) about the properties of a program
Yeah that makes sense
Its quite tempting to compare them to Haskell’s type signatures/type checker, but yeah you are right that they aren’t. They are merely type signatures.
They're not type signatures either. They are predicates. Think of it like a cool syntax to define complex pre/post conditions.
yeah you are right. I have mostly been using them to spec input types/output types but there is nothing stopping me from using them to reason about value as well.
if nothing exercises or otherwise checks the functions spec, it's effectively a comment as data. If you actually exercise the function specced the arguments that are functions are tested by generating various args and calling with them
yeah I have instrumentation enabled in my test suite in the hope that I trigger any type errors iff they are there.
Is there a function in core or somewhere like (defn ensure-coll [x] (if (coll? x) x [x]))
No. Wanting something like that is a sign you are using a poorly thought out API, it's an easy function to write but it's better to fix the source of the data to be consistent (when possible)
anyway, the code you have there does what one would expect, if you need it
I stumbled upon kibit today: https://github.com/jonase/kibit which may be able to automatically answer those types of questions ^^
I've got a jsonschema like {"type": "number"}
, I want to make it nullable by changing it to {"type": ["number", "null"]}
, using (update schema "type" #(-> % ensure-coll (concat ["null"])))
why is it {"type": ["number", "null"]}
instead of {"type": "number", "null": true}
?
maybe a diff question tho
I don’t know actually, I thought you had some custom JSON data, but if its a standard then probably not
why not (fn [x] [x "null"])
?
is it sometimes already a vector?
do you have a spec link, now I am kinda curious
I find it strange that you would model like that
or is it just a sorta-sum-type? is {"type": ["number", "string"]}
also valid
well that makes a little more sense
IMO it would have been better to just require “type” always to be an array to simplify but they probably are optimizing the usual case of something being a single type
yeah think so too
null as a type makes sense tho 😉
instead of a value
yeah, then your ensure-coll is probably the right thing
alternatively, you could have a multimethod that dispatches differently for coll vs. string - but for only two possibilities that is probably a bit much
I assume it would never be extended to other cases
if you have control over the schema, I think this could be valid:
{"type": ["number"]}
kibit actually finds some cool stuff
hmm it has some issues understanding that and
and or
are macros it seems
I'm in unfamiliar territory here: Do I need to worry about a function name conflict between cljs.core
and a third-party library? If so, how might I fix it? Can I use :refer-clojure :exclude
here given it's not my code?
@trailcapital thank you!