Fork me on GitHub
David Reno17:07:17

This is my first function. I’d appreciate any feedback/comments. I’d guess there’s already a library to do this but doing it for practice. Also guessing it could be shorter or more elegant. Not sure about my commenting method.


How about


@dcreno congrats for your first function! 🎉 A few quick notes: - idiomatic naming for string argument is s - how do you expect the function to behave with non-string inputs, such as nil, 1, [], {:a 1}?

David Reno17:07:07

yeah, it doesn’t like nil or '1. I thought maybe that was OK as it’s documented to only take strings?

David Reno17:07:44

Maybe I should validate the input and return false if not a string?


I’d expect it to return false. Usually when I’m unsure how my function should behave, I test how similar functions in clojure core behave and try to make it consistent.

👍 8

Like here you could check how string? uuid? etc. behave


Also, since Clojure is a lisp, you can check if number is between 0 and 255 by simply (<= 0 % 255). You can have arbitrary amount of args, so no need for and necessarily.


Since the regex does not match the minus sign, % will always be non-negative. So, (<= 0 % 255) can be just (< % 256)


Also, I’d probably remove the if clause completely for simplicity

David Reno18:07:41

huh, not sure why I didn’t see that…

David Reno18:07:23

oh, wait. I put that in there to avoid the exception when there was no match, let me look again though.

David Reno18:07:02

v2 with feedback from @valtteri and @dorab


(every? true? (map pred coll)) is pretty much the same as (every? pred coll)


And (map f (map g coll)) is the same as (map #(f (g %)) coll) Assuming f and g have no side-effects.

David Reno18:07:40

was thinking imperatively…


Also the same as (map (comp f g) coll)


One of the benefits of side-effect-free functions is that you can have rules like those above that you can apply with full confidence that they will not change the semantics of your code.

David Reno18:07:45

v3 with more feedback. Still not sure about removing an if…seems to break the case of (ipv4? "")


Take a look at if-let and see if that helps you make the code shorter.

👍 4

Check also not-empty


Idiomatic Clojure typically uses (if (seq something-that-could-be-nil-or-a-seq) ...) to check.

David Reno18:07:42

seq and not-empty seem very similar except one works on seq and the other on coll. I guess seq is the more general and thus preferred?


On second thoughts, what I said is true, but not germane to this discussion.


could probably do (if-let [match (next (re-matches ,,,))])

David Reno18:07:59

thinking about the diff between rest and next now…thought it would be the same in this situation but unsure about the return value when empty.


(next coll) is essentially (seq (rest coll))

👍 4

so (next ()) is nil, rather than ()

David Reno18:07:41

Thinking about combining the two if’s but seems difficult due to if-let…


Remember that (if cond expr false) is the same as (and cond expr) or (when cond expr) (modulo returning truthy/falsey rather than strictly true/false).

👍 4

(defn ipv4?
  "Takes a string.
  Returns true if it is a valid IP v4 address.
  Else false."
  (if (string? s)
    (if-let [gs (re-matches #"(\d+)\.(\d+)\.(\d+)\.(\d+)" s)]
      (every? #(< (Long/parseLong %) 256) (rest gs))


similar to what you had.


@dorab (every? pred []) is true -- I don't think your code has the same behavior as David's in the case where (rest gs) returns an empty sequence?


(or are we guaranteed that if re-matches returns non-`nil` then its rest will be non-empty for that regex? I didn't try that in the REPL yet)


that was my assumption. did not verify with repl.


Yeah, I think that's a safe assumption now I think about it...


If the regex matches at all, you'll get a 5-tuple back.



(and (string? s)
     (when-let [gs (re-matches #"(\d+)\.(\d+)\.(\d+)\.(\d+)" s)]
       (every? #(< (Long/parseLong %) 256) (rest gs))))

David Reno18:07:22

trying to understand the differences between if-let/`when-let`


(when-let binding lots of stuff) is equivalent to (if-let binding (do lots of stuff) nil)

David Reno19:07:07

seems like it’s also equivalent to (if-let binding (do lots of stuff)), am I wrong?


Yes, both if and if-let can omit the "else" expression and that is equivalent to having nil there.

David Reno19:07:01

these both return nil: (when-let [s false] true) (if-let [s false] true)


But it's considered non-idiomatic to have a one-armed if so when would be used instead.

David Reno19:07:19

yeah, I get it now.


A lot of the time in Clojure, you write predicates based on truthy/falsey without needing to worry about returning strictly true/`false` but if you put ? at the end of a predicate name, there's an assumption that it returns a Boolean, not just truthy/falsey values. So it's a bit of a trade off there.


There are some functions that care about the difference between nil and false tho' so it can matter sometimes.


(assuming that it's OK to return nil rather than false -- or just wrap that in a call to boolean if you want true/`false` explicitly)

David Reno19:07:29

Wow, this has been an excellent learning exercise. No embedded comments anymore, but certainly short. Thanks everyone!


forgive me for beating the horse, but two more points: \d+ will match a numeric sequence that is too long for Long/valueOf, and will cause it to throw. java/clj has a min/max syntax: \d{1,3} that will cause the match to fail

👍 4

and the docstring, somewhat counter-intuitively, goes before the args

👍 8

the reason that it doesn't cause an error is that a string is a perfectly valid expression


and the reason it makes sense for it to go before the args is that clj supports different function bodies for different numbers of arguments


though that's not the common case


nil is considered as ‘falsey’ so in most cases it’s OK to return nil. However if you wish to return always a boolean value you can use if-let instead.

(defn ipv4? [s]
  (if-let [matches (and (string? s)
                        (re-matches #"^(\d+)\.(\d+)\.(\d+)\.(\d+)$" s))]
    (every? #(<= (Long/valueOf %) 255) (rest matches))
Then it behaves similarly to functions in clojure core that end with ?.


Btw, notice the neat thing with and is that it returns the last truthy value. (and true "hey!") => "hey!"


I think I like that better than my version. Nice.


So in the vein of "now you have two problems"- the {1,3} syntax should replace the + and be contained in the group

David Reno20:07:15

With @jonahbenton’s comments on size of octets. Now I find myself wondering if that regexp can be reduced in size without adding too much complexity… time to walk away? [edit to add the comment part]


In theory one can create a larger group- exact 3 of 1-3 digits followed by a ., followed by 1-3 digits- but that's very messy to process

David Reno20:07:36

yeah, the theory of an “octet” seems nice but messy.


and regexp is a heavy/expensive tool

David Reno20:07:56

think I should have split on . and processed directly?


btw, looks like slack has gotten confused about comment order- but see my earlier comment about the syntax- each should be (\d{1,3}) rather than (\d+){1,3}

David Reno20:07:26

well, I deleted it and re-added because I forgot to paste the version with the comment in the right place.


in practice, split is not much better- the split is on a regexp

David Reno20:07:43

so if you need a regexp just use it?

David Reno20:07:56

seemed like a good application


yeah, split wants to split on a regex, rather than a char


in lower level languages, str -> ip walks the string. for some that's the right level of abstraction. for others, regexp is the right level.


It has been years since I did a CLJC project, and I just went googling for a template to create such a beast, but have found nothing. Help the addled: is nothing particular required, do I just add the .cljc extension and go?


Dunno if you got a response to this -- answer is yes! Can be any project, just add a .cljc file (& if you put it in a separate source dir, which some people do, you may have to add that source dir to your project). And be aware that you can use reader conditionals if you need to include clj- or cljs-specific code in it.


so i think i understand the usage of "and" and "or" in that "and" will give you the first false value or if no false values then it will give you the last truthy value. but i'm not quite understanding why it works like that. It doesn't make intuitive sense to me. I have little experience with and's and or's in other languages but it wasn't presented like this.


i think when i saw it before the use of "and" and "or" was specifically to give you a boolean value only.

Timo Freiberg22:07:48

You understand why it works though?


or and and are implemented as if chains


(and true false true) is equivalent to

(let [a true]
  (if a
    (let [a false]
      (if a true a))

Timo Freiberg22:07:15

1. The implementations are valid because returning a thruthy value is the same as returning true, vice versa for falsy 2. The implementation was probably easier, definitely not harder that way, and it allows for cool elegant code


ok, that helps. i think it's just the whole truthy thing. the way i understood the "and" and "or" logic is that you would get true or false. I hadn't encountered it where you would just get a value like 4 that is considered truthy.


like when you use "and" in logic all the things have to be true right and then you get true. but here you get the first false value.

Timo Freiberg22:07:40

If you're used to a static, strict language, it feels weird. At least it did to me, coming from Java with a bit of haskell in my spare time

Timo Freiberg22:07:48

If you use the return values as booleans (in ifs etc) then you don't even have to notice

Timo Freiberg22:07:13

But if not, you get a bit of extra information


logic wise, and and or is the same, whether it accepts and returns truthy/falsy values vs boolean values. Since everything in clojure can be evaluated as truthy/falsy, may as well have it accept those rather than hard boolean types. Its just more generic and flexible.


yep, this is helping. it's starting to make sense. i was coming from fiddling around with strictly typed languages like Timo mentioned. That if chain posted above is not quite clicking yet so i'm working on that understanding right now.


try playing around with macroexpand:

=> (macroexpand '(and true true))
(let* [and__5314__auto__ true] (if and__5314__auto__ (clojure.core/and true) and__5314__auto__))

Timo Freiberg22:07:34

Maybe use other values than booleans to see the difference :)


or rather,

=> (clojure.walk/macroexpand-all '(and true true))
(let* [and__5314__auto__ true] (if and__5314__auto__ true and__5314__auto__))


@U9J50BY4C if you haven't had a chance to watch Rich and also Stu Halloway's talks (check the ClojureTV channel on YouTube)- there are several that go into the implications of thinking about the world in terms of predicates rather than types


in all choices there are tradeoffs


i've listened to a few of their talks. it's almost like i want to save them though until i'm further along so i can really dig into the wisdom. but yeah, clojure talks and projects are a big thing that drew me to this language. they all seem creative and well thought out and just the right vibe for what i'm looking for in this whole experience. it's hard to explain.


absolutely, I think grokking that vibe is a common experience. the talks are dense enough that they generally reward followup viewings


As a side note, clojure has a convention for "predicate" functions. They typically are named with a ? at the end and return booleans. i.e. int?, some?


and and or don't have ? at the end of their names. Following this convention, they don't return booleans.


conceivably, clojure could have implemented a predicate version of and as and? that strictly returns booleans only, but the behavior would be redundant.


@hiskennyness Right, you just put *.cljc files on the classpath and go. No real project-level considerations.


so basically you just know if you get a value other than false or nil then there was something truthy in the expression? am i getting closer to understanding?


yup! the only falsy values in Clojure are false and nil; everything else is truthy


this is opposed to some other languages that have things like 0 and "" be falsy


I saw someone mention a nasty bug that the java native "false" is even truthy in clojure

😱 4

Yeah. (Boolean. false) is true


That will bite you if you serialize booleans and use them on the other end.


Same happens with (js/Boolean. false)


looks like doing an explicit boolean cast resolves that particular problem:

=> (if (Boolean. false) true false)
=> (if (boolean (Boolean. false)) true false)


(if (js/Boolean. false) :t :f)


Yeah, one approach is to use clojure.walk to coerce any Boolean instances using boolean


(If you have a large value with unknown structure)


Interestingly, that trick doesn't work in ClojureScript (if (boolean (js/Boolean. false)) :t :f) yields :t


i suppose that's in keeping with JavaScript's semantics?


since if (new Boolean(false)) { true } else { false } is true


Dunno. I honestly don't think there is much desire to worry about the semantics of wrappers.


though if you leave the new off it's false 😛


(boolean? (js/Boolean. false)) is false in ClojureScript, (boolean? (Boolean. false)) is true in Cloure


Unfortunately the docstring in ClojureScript is a bit ambiguous


Return true if x is a Boolean could mean Boolean as in the Boolean wrapper object, or it could mean Boolean, as in the type derived from the dude named George Boole.


JavaScript's primitive object instances are treated differently to the primitives themselves

Boolean(new String(""))


Hah. I refuse to even dwell upon that. 🙂

😁 4