This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-02-20
Channels
- # architecture (1)
- # babashka (5)
- # beginners (46)
- # calva (8)
- # cider (12)
- # clj-kondo (2)
- # cljfx (3)
- # clojure (23)
- # clojure-europe (7)
- # clojure-israel (2)
- # clojure-nl (11)
- # clojure-norway (8)
- # clojure-uk (1)
- # clojurescript (27)
- # conjure (2)
- # cursive (50)
- # data-oriented-programming (1)
- # data-science (1)
- # datahike (1)
- # datascript (12)
- # emacs (3)
- # events (1)
- # fulcro (13)
- # lambdaisland (7)
- # leiningen (4)
- # lsp (102)
- # meander (2)
- # off-topic (19)
- # parinfer (3)
- # reveal (8)
- # rewrite-clj (13)
- # shadow-cljs (3)
- # specter (13)
- # tools-deps (6)
- # vim (12)
i've tried to read this,
https://clojuredocs.org/clojure.core/format
but still not understand (format "%d %s")
means? what is %d %s do?
https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/Formatter.html is a nicer doc of this i think. at least formatting
> The format specifiers which do not correspond to arguments have the following syntax: > %[flags][width]conversion
s', 'S' general If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().
so %s
is "no flags, no width info, general conversion, basically toString"
%d
is "no flags, no width infor, general conversion, basically toDecimalInteger >>> am i correct?
ahhh okay... thank you so much, enlightening
and combinging several flags: (format "%(,08.2f" -3000.0)
(
for negative numbers in parens, ,
for magnitude separators, 0
for leading zeros, etc
i tried (format "%tH:%tM:%tS" 0.53846153846)
result in error
Execution error (IllegalFormatConversionException) at java.util.Formatter$FormatSpecifier/failConversion (Formatter.java:4426).
H != java.lang.Double
the result should be = 0:32:18
You appear to be trying to use formatting options for datetimes, if you read the section in the already linked formatting docs on datetimes it lists the types that may be formatted that way, and doubles are not on the list
Hi, Is there have a way to convert PersistentArrayMap to vector? It shows me Key must be integer when I tired into [].
you may have the arguments in the wrong order. into
should work:
user> (type {:a 1})
clojure.lang.PersistentArrayMap
user> (into [] {:a 1})
[[:a 1]]
My data is: [{:a :b} {:c :d}], so I want to convert to [[:a :b] [:c :d]]. And I write code like this:
(mapv (into []) data)
that seems like an odd transformation. I might try something like:
user> (->> [{:a :b} {:c :d}]
(mapcat vec)
(into []))
[[:a :b] [:c :d]]
it's common for programmers that come to clojure, especially from statically typed languages, to overemphasize the underlying concrete data type. You may only need:
user> (into [] cat [{:a :b} {:c :d}])
[[:a :b] [:c :d]]
Yeah, That's a bit odd indeed, because the logic of this is for handle data from Java, so in Java, I have defined some concrete type to describe data which like you said. I reference your answer write code shows below. And thank you for your help.
->> records (mapv vec) (into [])
->>
is only a syntax transform, it's not an operator or function of any sort
you need a function or loop to recur
(I mean strictly speaking it's a function that runs at compile time and transforms the form before it's compiled, but it's not the kind of function that creates a binding that can recur)
I am looking for a way top operate on all the keys of a nested map to get them lowercased. from:
{
"Isbn": "123-456-222",
"Title": "The Ultimate Database Study Guide",
"Category": ["Non-Fiction", "Technology"],
"Author": {
"Lastname": "Doe",
"Firstname": "Jane"
}
}
to :
{
"isbn": "123-456-222",
"title": "The Ultimate Database Study Guide",
"category": ["Non-Fiction", "Technology"],
"author": {
"lastname": "Doe",
"firstname": "Jane"
}
}
How can this be achieved? Any hints and tips are greatly appreciated!I’ve used this to lowercase string-keys (assuming you convert from json to maps beforehand) for a single layer one before but I’m sure there’s a better way:
(into {} (map #([[k v] [(str/lower-case k) v]) input))
and for a recursive thing that only affects maps specifically:
(defn deep-lowercase-map [m]
(reduce
(fn [acc [k v]]
(if (map? v)
(merge acc (lowercase-map k (deep-lowercase-map v)))
(assoc acc k v)))
{}
m))
I’m doing this by eye I haven’t tested it, but it also comes with a few issues/caveats:
1. it’s going to fail with keys that aren’t strings
2. it’s not tail recursive and will blow the stack for big maps.
3. It’s probably better to use clojure.walk, but if it’s throwaway code then it’ll be alright.Instead of using reduce
on a hash map, consider reduce-kv
reduce
is going to call seq
on the hash map and turn it into a sequence of pairs (`MapEntry` pairs which are like vectors) and you have to destructure the key/value pair -- as you have with [k v]
-- reduce-kv
operates directly on the hash map and invokes the reducing function with three arguments: (fn [acc k v] ..)
-- no destructuring needed.
There is Clojure function “clojure.walk/keywordize-keys”, which walks trough a nested map and converts all string keys to keywords: https://clojuredocs.org/clojure.walk/keywordize-keys You can adapt the code, to lowercase the keywords:
(into {} (map f x))
can become (into {} (map f) x)
Hi Clojure community - a friend of mine and I are developing an online multiplayer board game. The first game we developed as a proof of concept was tic-tac-toe and got it working over a network using peerjs
peer-to-peer. But I’d like this version to be a bit finicky. But both players had to be on at the same time. Using a database would allow asynchronous play.
A typical turn consumes a map with about 10 lines of data.
Can you recommend a good simple database in Clojure?
peak the one you most familiar with and then look for the client in clojure or java (unlikely you will not be able to find one)
OK, I guess I’m most familiar with regular ol’ SQL.
And if you get stuck with either of those, ask in #sql and/or #honeysql as appropriate. They're lower traffic than #beginners and it's more likely someone will give you a good answer quickly. Probably me 😉
I appreciate all the replies. Thanks!