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?
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
if you check out (doc format)
it gives some indication of what's going on
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?
looks like it
ahhh okay... thank you so much, enlightening
so you can check out how the width stuff works with (format "%8.2f" 3.0)
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 [].
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)
Any wrong in there?
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 [])
I have another question , How recur with ->>?
->>
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)
All right, Thank you.
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]]
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!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:
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.(javahippie seems to have done a postwalk version, probably better to refer to that)
@hobosarefriends and @javahippie Thanks for the help!
(into {} (map f x))
can become (into {} (map f) x)
Still going to need some time to reason and understand what this code does 🙂
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?
I appreciate all the replies. Thanks!
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.
This sounds like a job for redis honestly
If you're comfortable with redis that is.
@scotto go with SQL, I recommend https://github.com/seancorfield/next-jdbc
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.
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 😉
Ohhh that’s actually awesome! another gold nugget for my collection, Thanks Sean