Fork me on GitHub
#beginners
<
2021-02-20
>
Adrian Imanuel05:02:28

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?

dpsutton05:02:16

if you check out (doc format) it gives some indication of what's going on

dpsutton05:02:32

> The format specifiers which do not correspond to arguments have the following syntax: > %[flags][width]conversion

dpsutton05:02:31

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"

Adrian Imanuel05:02:26

%d is "no flags, no width infor, general conversion, basically toDecimalInteger >>> am i correct?

dpsutton05:02:50

looks like it

Adrian Imanuel05:02:42

ahhh okay... thank you so much, enlightening

dpsutton05:02:12

so you can check out how the width stuff works with (format "%8.2f" 3.0)

dpsutton05:02:58

and combinging several flags: (format "%(,08.2f" -3000.0) ( for negative numbers in parens, , for magnitude separators, 0 for leading zeros, etc

Adrian Imanuel06:02:34

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

Adrian Imanuel06:02:45

the result should be = 0:32:18

hiredman06:02:26

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

Yang Xu07:02:56

Hi, Is there have a way to convert PersistentArrayMap to vector? It shows me Key must be integer when I tired into [].

phronmophobic07:02:32

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]]

Yang Xu08:02:17

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)

Yang Xu08:02:39

Any wrong in there?

phronmophobic08:02:45

that seems like an odd transformation. I might try something like:

user> (->> [{:a :b} {:c :d}]
           (mapcat vec)
           (into []))
[[:a :b] [:c :d]]

phronmophobic08:02:53

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]]

Yang Xu08:02:38

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 [])

Yang Xu09:02:04

I have another question , How recur with ->>?

noisesmith18:02:51

->> is only a syntax transform, it's not an operator or function of any sort

noisesmith18:02:00

you need a function or loop to recur

noisesmith18:02:43

(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)

Yang Xu06:02:57

All right, Thank you.

Marco Pas09:02:34

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!

Mno10:02:45

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.

Mno10:02:44

(javahippie seems to have done a postwalk version, probably better to refer to that)

seancorfield19:02:42

Instead of using reduce on a hash map, consider reduce-kv

seancorfield20:02:14

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.

Mno22:02:23

Ohhh that’s actually awesome! another gold nugget for my collection, Thanks Sean

javahippie10:02:07

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:

Vincent Cantin17:02:36

(into {} (map f x)) can become (into {} (map f) x)

Marco Pas17:02:58

@hobosarefriends and @javahippie Thanks for the help!

👍 3
Marco Pas18:02:47

Still going to need some time to reason and understand what this code does 🙂

Scott Starkey18:02:16

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?

delaguardo18:02:34

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)

Scott Starkey18:02:34

OK, I guess I’m most familiar with regular ol’ SQL.

Mno19:02:24

This sounds like a job for redis honestly

Mno19:02:57

If you're comfortable with redis that is.

seancorfield20:02:52

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 😉

👍 3
clj 3
Scott Starkey14:02:19

I appreciate all the replies. Thanks!