Fork me on GitHub
#beginners
<
2020-03-11
>
seancorfield00:03:52

@connor480 http://clojuredocs.org/clojure.core/map-indexed has some good examples (in general, http://clojuredocs.org will probably be more helpful than just the docstrings alone).

đź‘Ť 4
skoude08:03:43

any idea how do I define postgresqls jsonb type in spec.. For example I can define basic types like:

(s/def ::id integer?)
(s/def ::service string?)
(s/def ::application string?)
but if i need to have jsonb type, is there an additional library etc to get that datatype for spec?

thom09:03:47

By default it’ll just come out as a PGobject, but if you’re deserialising it into Clojure data structures it’ll be a map, vector, string, number or bool.

Frank Jay12:03:31

Hi, I'm John. I'm learning Clojure, I really like it to far. I have created two functions for andsimplify and orsimplify which takes a boolean expression like (andsimplify '(and x y false)) and returns the evaluated form, which is false in this case. I want to create a map of some sort to recursively check for each nested expression.

Frank Jay13:03:45

(defn recursive_function
  [idealreturn]

    :else
    (let [letvar (first idealreturn)]
      (map recursive_function (rest idealreturn)


           (cond (seq? idealreturn) (println "ideal" idealreturn)
                 (= letvar 'and) (and-simplify idealreturn)
                 (= letvar 'or) (or-simplify idealreturn)
               
                 ))
      idealreturn))

jsn13:03:33

(defn simplify [expr] (if-not (seq? expr) expr (let [[op & args] expr args (map simplify args)] (println op) (condp = op 'and (if (->> args (some false?)) false expr) 'or (or (->> args (some true?)) expr) (throw (ex-info "Uknown op" {:op op}))))))

jsn13:03:24

a variant with separate or-simplify / and-simplify seems more complex to me, actually

jsn14:03:48

(cleaned up a bit) (defn simplify [expr] (if-not (seq? expr) expr (let [[op & args] expr args (map simplify args)] (condp = op 'and (if (some false? args) false expr) 'or (or (some true? args) expr) (throw (ex-info "Unknown op" {:op op}))))))

hindol13:03:54

Do you want something like flatten?

Frank Jay13:03:23

This is my pseudocode? Any tips on how to fix it so I can simplify nested expressions like (and true false(and true x y(and true false)) which, in this case would simplify to false. Assume my andsimplify and orsimplify functions work perfectly

hindol13:03:07

Okay, my bad. Now I understand what you want.

hindol13:03:42

This is like a tree of predicates where a node is truthy if all children are truthy.

spasov13:03:21

Is it more idiomatic to write: (= 0 (mod x 3)) or (= (mod x 3) 0)

timcreasy13:03:27

I prefer the latter because it reads more like English to me. “(mod x 3) is equal to 0”.

timcreasy13:03:41

Keep in mind there is a zero? predicate.

đź‘Ť 4
spasov13:03:25

Yup, thanks a lot 🙂

aisamu13:03:22

Just as another data point, I prefer the former because it's easier to parse - otherwise we have to mentally close parenthesis to find 0's "level". That's not a problem here, but can be quite annoying on longer examples:

(deftest this
  (testing "test"
    (is (= (some (long (chain))) 0))))

(deftest this
  (testing "test"
    (is (= 0 (some (long (chain)))))))

đź‘Ť 4
spasov13:03:02

Yes I can see how that can be a problem in the first case.

spasov13:03:11

to me the former reads like if (nil == x) {} in regular languages, which is backward for most people but the idea it was used is to prevent assignments in the if.

hindol14:03:04

I also prefer the first. Easy on the eyes.

đź‘Ť 4
mmeix18:03:07

or: (zero? (mod x 3))

spasov13:03:55

I think I've seen the latter version more frequently, but I'm not sure if I like it better.

spasov13:03:13

Beautiful! 🙂

Santiago13:03:13

How do you recommend checking if a string is a valid UUID? (uuid? (java.util.UUID/fromString customer-uuid)) will coerce customer-uuid into a valid uuid e.g. 05401099-3a50-4987-b978-973 which is invalid is transformed into #uuid "05401099-3a50-4987-b978-000000000973" and now evaluates to true . Should I just go with regex?

hindol15:03:19

@slack.jcpsantiago Seems UUID/fromString does its own validation and your example is actually a valid UUID.

Santiago16:03:32

weird, so it doesn’t matter how many characters exist between the dashes? because 05-3a50-47-b-973 also evaluates to true . According to https://www.rfc-editor.org/info/rfc4122 the canonical form should be a total of 36 characters in 8-4-4-4-12 form

Santiago16:03:51

going with this instead (re-find #"[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}" potential-uuid-string) but I’m still curious why fromString fills the string with zeroes, so it’s always true

Santiago21:03:51

facepalm ok let’s keep it broken. nice

Lucas Vassalli17:03:41

I have a large collection I may not need to use. Would declaring it with def c (delay coll) to prevent memory usage? Or how else? It's CLJS

delaguardo17:03:38

to deliver data later delay still needs it in memory. So it can not save memory

Lucas Vassalli17:03:51

Thanks. I'm thinking about creating an atom instead and a function to populate it instead of defing the collection

Lucas Vassalli17:03:28

I'd appreciate suggestions on how to inspect memory as well

noisesmith17:03:33

delay would help if you had function calls to populate it inside the literal eg. (delay {:a (foo) :b (bar)}) where foo and bar functions return large size data

đź‘Ť 8
dil-h-jawan17:03:27

hi everyone, I am new to Clojure & its been only a week, I am already in love with Clojure, can anyone suggest a small project that I can write to learn/practice in, maybe a small library port or some tool?

hindol17:03:59

You can try creating a Clojure wrapper for some existing Java library. That will be useful for the community as well. Maybe tomlJ? A TOML 0.5 parser.

spasov17:03:35

How experienced are you with programming in general?

spasov17:03:37

I personally would suggest doing something you know how to do from other languages. That way, you can focus on implementing it with Clojure instead of trying to wrap your head around how to actually do it.

spasov17:03:43

Fight one battle at a time, so to speak.

spasov17:03:32

I personally like doing APIs, because that's something I'm familiar with and I don't really need to think about the specifics of it, just how to achieve what I already know about APIs with Clojure.

spasov17:03:59

Another thing you can try is simple text-based games. Those are always fun. Just keep the scope super small.

dil-h-jawan18:03:30

Thanks for the reply , I have quite a bit of experience in programming in general, I will try to port some library from python to Clojure

đź‘Ť 4
dil-h-jawan17:03:29

I am not sure should this question belong to off-topic channel? or asking such question is fine here.

bfabry17:03:23

I think it's a good channel to ask in. but I don't have any recommendations sorry

ruben17:03:15

Hi all, just joining.

đź‘Ť 4