beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
2021-02-20T05:32:28.002500Z

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?

dpsutton 2021-02-20T05:33:16.003100Z

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

dpsutton 2021-02-20T05:36:18.003500Z

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

dpsutton 2021-02-20T05:36:32.003800Z

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

dpsutton 2021-02-20T05:37:31.004900Z

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"

2021-02-20T05:39:26.005800Z

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

dpsutton 2021-02-20T05:39:50.006Z

looks like it

2021-02-20T05:40:42.006600Z

ahhh okay... thank you so much, enlightening

dpsutton 2021-02-20T05:42:12.007Z

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

dpsutton 2021-02-20T05:44:58.007700Z

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

2021-02-20T06:06:34.008200Z

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

2021-02-20T06:07:45.008600Z

the result should be = 0:32:18

2021-02-20T06:22:26.012400Z

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 Xu 2021-02-20T07:35:56.015300Z

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

Yang Xu 2021-02-20T08:03:17.015600Z

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 Xu 2021-02-20T08:03:39.015800Z

Any wrong in there?

phronmophobic 2021-02-20T08:16:45.016300Z

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

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

phronmophobic 2021-02-20T08:22:53.016500Z

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 Xu 2021-02-20T08:36:38.016800Z

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 Xu 2021-02-20T09:01:04.017Z

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

2021-02-22T18:55:51.203300Z

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

2021-02-22T18:56:00.203500Z

you need a function or loop to recur

2021-02-22T18:57:43.203700Z

(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 Xu 2021-02-23T06:39:57.221700Z

All right, Thank you.

phronmophobic 2021-02-20T07:58:32.015400Z

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

Marco Pas 2021-02-20T09:40:34.018900Z

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!

javahippie 2021-02-20T10:00:07.020200Z

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:

javahippie 2021-02-20T10:00:34.020300Z

Mno 2021-02-20T10:00:45.020400Z

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.

Mno 2021-02-20T10:01:44.020600Z

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

Marco Pas 2021-02-20T17:48:58.023200Z

@hobosarefriends and @javahippie Thanks for the help!

👍 1
2021-02-20T17:52:36.023400Z

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

Marco Pas 2021-02-20T18:01:47.024100Z

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

Scott Starkey 2021-02-20T18:42:16.027600Z

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?

Scott Starkey 2021-02-21T14:49:19.079300Z

I appreciate all the replies. Thanks!

2021-02-20T18:45:34.027800Z

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 Starkey 2021-02-20T18:46:34.028Z

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

Mno 2021-02-20T19:45:24.030200Z

This sounds like a job for redis honestly

Mno 2021-02-20T19:45:57.030400Z

If you're comfortable with redis that is.

raspasov 2021-02-20T19:51:43.030700Z

@scotto go with SQL, I recommend https://github.com/seancorfield/next-jdbc

👍 1
raspasov 2021-02-20T19:52:38.031100Z

Also https://github.com/seancorfield/honeysql

seancorfield 2021-02-20T19:59:42.031500Z

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

seancorfield 2021-02-20T20:02:14.031700Z

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.

seancorfield 2021-02-20T20:03:52.031900Z

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 😉

👍 1
1
Mno 2021-02-20T22:08:23.032200Z

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