This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-09
Channels
- # announcements (3)
- # babashka (17)
- # beginners (75)
- # calva (43)
- # cider (1)
- # clj-kondo (54)
- # cljdoc (8)
- # cljs-dev (70)
- # clojure (83)
- # clojure-europe (27)
- # clojure-nl (4)
- # clojure-norway (4)
- # clojure-spec (8)
- # clojure-uk (13)
- # clojured (2)
- # clojurescript (47)
- # community-development (4)
- # conjure (2)
- # cursive (3)
- # datomic (5)
- # emacs (5)
- # events (3)
- # fulcro (13)
- # graalvm-mobile (19)
- # helix (2)
- # introduce-yourself (1)
- # jobs (2)
- # jobs-discuss (28)
- # kaocha (9)
- # livestream (11)
- # malli (15)
- # meander (24)
- # nextjournal (8)
- # off-topic (26)
- # pathom (1)
- # pedestal (8)
- # polylith (2)
- # portal (31)
- # re-frame (4)
- # reagent (10)
- # reitit (8)
- # remote-jobs (3)
- # sci (1)
- # shadow-cljs (66)
- # spacemacs (20)
- # testing (6)
- # vim (15)
- # xtdb (7)
Hello, I'm new to Clojure & FP. I wanted to know what is the best way to conditionally apply map to collections in Clojure. for example,
(def d (list 1 2 3 4)
(cond-map odd? inc d)
=> (2 2 4 4)
Thanks for any advicejust "distribute" the cond into the mapped function. (map #(if (odd? %) (inc %) %) [1 2 3 4])
. If this pattern happens often make a little combinator for it
thanks for your help. I have an additional question. does map always have a space complexity of O(2n)?
(sequence (comp (filter odd?) (map inc)) d)
@U0CKDHF4L that removes the even numbers rather than keeping them unchanged
sorry yeh didn't read the question properly
(interleave
(sequence (remove odd?) d)
(sequence (comp (filter odd?) (map inc)) d))
I guess the disadvantage of this is it's longer and tests odd?
twice@U02U505JGVD yes, as long as you don't place additional restrictions on the applied function(s), map necessarily has space complexity of O(2n) = O(n). (Sorry, that was not accurate enough to be correct as stated.)
As long as you don't place additional restrictions on the (class) of applied function(s), map
itself necessarily has space complexity of Ω(n), which the result alone requires. Of course, the additional space complexity is also 𝓞_(2n) = 𝓞(n) (and thus ϴ(n)_).
Is there some kind of online apropos
style feature in the repl or in rebel etc?
Do you mean like this, in the REPL? https://clojuredocs.org/clojure.repl/apropos
user=> (apropos "find")
(clojure.core/find clojure.core/find-keyword clojure.core/find-ns clojure.core/find-protocol-impl clojure.core/find-protocol-method clojure.core/find-var clojure.core/re-find clojure.repl/find-doc)
If you do not get that behavior in your REPL, you might need to do something like (use 'clojure.repl)
first
That has been part of Clojure for at least 10 years now)
Ha, should have guessede
(apropos)
check out dir
also - and some of these:
Clojure 1.10.1
(ins)user=> (dir clojure.repl)
apropos
demunge
dir
dir-fn
doc
find-doc
pst
root-cause
set-break-handler!
source
source-fn
stack-element-str
thread-stopper
nil
I use find-doc and source frequently, they provide the parts of an IDE I would actually use if I used one
Guys before I throw this machine out the window, how does one import and use clojure’s string functions? Does it need to be in deps.edn?
(ns hello (:require [clojure.string :as string]))
In a source file, I would recommend something like this:
(ns my.namespace.name
(:require [clojure.string :as str]))
(str/join ["a" "b"])
If you want to do it in a REPL, you can also do the above, but it will create a new namespace and switch you to it right there in the REPL if you do that. If you do not want to switch namespaces in the REPL, you can do (note slightly different syntax): (require '[clojure.string :as str])
And please don't throw your machine out the window. Give it to me instead 🙂
Thanks. That all makes sense to me. Must be something wrong with my editor config because it’s just not cooperating, but the code itself is fine, compiles/runs.
The more automagical stuff in your editor/IDE, it can get confusing when things don't go as you wish. There are editor/IDE-specific channels on this Slack that might be helpful to ask in, if you get into issues that you think might be specific to your setup, e.g. #emacs #calva #cursive etc.
Thanks I may have to check that out. I see there is #vim, that’s good
There’s something I’m just not getting about (ns foo (:require…))
The ns
form is a bit tricky at first. https://gist.github.com/ghoseb/287710/ might help.
Ask away!
Here is what I have: `(ns hello (:require [clojure.string :as string] [buddy/buddy-core]))`
I don't think that buddy/buddy-core
is a valid namespace name in Clojure
It might be a valid way of naming a project in Maven, or deps.edn, but not in Clojure source code.
ah, ok
project names in Maven/deps.edn/project.clj are often based on Maven or Clojars rules for projects. A single project can contain any number of Clojure namespaces, and those namespace names might have zero relationship to the project name (but they usually are related, by convention)
so having specified the dep
I have to go check the docs or code for the namespace
You had this confusion yesterday as well and I’m not sure we’ve resolved it for you. Coordinates for dependencies are a way to specify that dependency and for some program to download those dependencies to your computer. buddy/buddy-core
and some version number can be resolved to a jar file (called the coordinates). It’s a way to specify that jar file, but does not specify what is in that jar file. The jar file is basically a zip file with a bunch of clojure files in it. And the namespaces of those clojure files have no relationship to the name of the dependency’s coordinates.
I made a note to better explain the linkage between deps and namespaces at https://github.com/clojure/clojure-site/issues/575 on the relevant Clojure pages. This is a common area of confusion for beginners.
(.getYear (new java.util.Date)) => 122
why does this return 122 ? is it offset from 1900 ?
I was looking for a way to do datetime without adding dependencies, but seems like I should now…
to be super precise: java.time meaning the jdk package (There have been a couple of horribly named clojure libraries like clojure.java-time which add to confusion)
What’s a single quote when found postfixed to a symbol?
It's a concept borrowed from maths (at least I think) called prime
, basically, in Clojure it has no special meaning, it's a valid symbol name, but I generally use it if I'm using the same variable name (where it makes sense contextually), i.e., name
and perhaps in a let I rebind it to name'
ahh yeah that’s not something i am used to yet
having these characters in symbols
hex->string and whatever
In mathematics, the prime is generally used to generate more variable names for similar things without resorting to subscripts, with x′ generally meaning something related to (or derived from) x.
(slightly off-topic 🙇). any beginners interested in a part-time Clojure internship? DM me
@james.amberger, in Clojure core there are some numeric functions that end with a prime (`'`), for which you'll see in the docs: "Supports arbitrary precision." For example, for *'
:
(* 1234567890 9876543210)
;; ArithmeticException integer overflow
(*' 1234567890 9876543210)
;;=> 12193263111263526900N
Just heard about PutIfMissing in java (add a key-value pair to a map if the key does not already exist). Does Clojure have an equivalent function in its map data structure arsenal? An empty Google search suggests not.
Don't know if this is a good pattern/idiom :thinking_face:, but you can use fnil with update
(update {} :a-missing-key (fnil identity "default"))
(update {:some-key "value"} :some-key (fnil identity "default"))
Interesting, thankyou Paul, but not the functionality I was thinking about. I am happy with hiredman's answer.
put if missing is most useful in mutable maps where it is used to provide atomic insertion when a value is missing
Hi there! I am wondering if there is a way to create a partial function by filling the right most arguments instead of the left most.
My use-case is to create a div10
function that simply divides by 10. I know this is trivially achieved with something like (defn div10 [n] (/ n 10))
. However, I'd like to be able to write something like (partial (/ _ 10))
or (rpartial (/ 10)
if that makes any sense at all.
Or is it the case that partial
really is just meant to be syntactic sugar for the case when you want to fill arguments from the left?
I see the style guide prefers partial
over a lambda when possible. Why?
Wouldn't be my preference :) I actually probably use fn
the most these days
partial
is actually slower than using #(…)
(not that this matters unless you're in “hot” code). The primary difference is that the function resulting from partial
is a varargs function.
So, to add 10 to a number you could say:
#(+ 10 %)
And apply that.
On the other hand, if you use (partial + 10)
then it will just add 10, or you could add more:
(let [f (partial + 10)] (f 5 7))
gives 22