Fork me on GitHub
#clojure
<
2018-07-09
>
pithyless08:07:02

is there an existing clojure/java function for “incrementing” a string ala Ruby#succ? > "some_label_1".succ => "some_label_2"

ordnungswidrig09:07:21

I don’t think one should do these things

mfikes12:07:06

@pithyless If your broader need is to generate unique symbols for use in code, gensym is a builtin for this purpose, often used to generate unique symbols in macro code. For example (gensym "some_label_")

pithyless12:07:08

Thanks, I’m aware of gensym, but here I’m iterating over a domain which established ordering of arbitrary strings via foo_1, foo_2, etc.

pithyless12:07:45

I solved this via regexp, but I would not be so bold as @U054UD60U to say there are no uses for a sensible autoincrementing string operation (the ruby one does things like map a to b and Z to AA.). [shrug]

ordnungswidrig12:07:18

If you need to generate ids like that, I’d go with a function to parts existing ids, one to increment the parts according to your needs (leading 0s anyone?) and building a string representation for it again.

⬆️ 4
ordnungswidrig19:07:13

@pithyless sorry I didn't want to sound to harsh, but such function does not make any sense as a core function to a language, imho.

oyakushev11:07:45

No, there's nothing like that in base Clojure.

pithyless12:07:02

I figured as much; I just thought maybe one of the string extensions (`expez/superstring`, funcool/cuerdas, others?) may have covered the use-case. Either way, thanks for the confirmation 🙂

ghadi12:07:18

Sure there is:

ghadi12:07:15

(map (partial str "some_label_") (range))

oyakushev12:07:41

I don't know Ruby, but from quick Googling it seems that it's much more complicated than that. E.g., abcd => abce

nathanmarz14:07:22

@pithyless that use case can be handled cleanly by Specter

(def str<->long (parser #(Long/parseLong %) str))

(transform [#"\d+" str<->long] inc "a99bc123")
;; => "a100bc124"

(transform [#"\d+\z" str<->long] inc "a99bc123")
;; => "a99bc124"

metal 16
nathanmarz14:07:06

Decomposing the problem with a composable abstraction lets you handle not just that use case, but related use cases like:

(setval #"\d+" "" "a99bc123")
;; => "abc"

(transform [#"\d+" str<->long] #(* 10 %) "a99bc123")
;; => "a990bc1230"

(transform (subselect #"\d+") reverse "a99bc123")
;; => "a123bc99"

pithyless14:07:39

thanks @nathanmarz - I hadn’t even considered Specter for this kind of problem

Graham Seyffert16:07:12

This may be a silly question, but is there a supported way to typehint arguments of a function in defprotcol? Basically, is it possible to do something like - In one namespace:

(defprotocol MyProtocol
  (a-function [this ^SomeJavaType argument]))
In another namespace:
(defrecord MyRecord
  MyProtocol
  (a-function [this ^SomeJavaType argument] ...))
? Currently I'm having to re-bind argument in the both of a-function w/ the type-hint in order to avoid reflection, which is ugly. Trying to typehint the args results in a compiler exception that the protocol can't be resolved.

Graham Seyffert16:07:23

Or should something like this be done with definterface instead? (I haven't had to use definterface yet)

Robert17:07:57

Does anyone know if 1. Anyone is working on porting the CLJ/Clojure cli to windows 2. If I wanted to view the code for the CLJ where would I look ?

andy.fingerhut17:07:33

@bobjohnson11 Likely someone on the #tools-deps channel knows the latest status of getting those things working on Windows. Last I recall hearing it mentioned, it wasn't implemented yet.

Robert17:07:19

thanks @gseyffert and @andy.fingerhut I appreciate your help

Alex Miller (Clojure team)17:07:37

@bobjohnson11 I have a port but am still working on the fit and finish of it

Alex Miller (Clojure team)17:07:57

working state (possibly not fully pushed) is in a branch of brew-install

Robert17:07:55

thanks @alexmiller I will take a look at the repo tonight. I appreciate the help.

Alex Miller (Clojure team)18:07:13

fyi, the scripts are not directly usable out of the repo as there are some bits and pieces that get modified during the build process

Robert19:07:43

Thanks for the tip. I'm interested in how the power shell script will work and how the CLJ and Clojure wrapper scripts work will work.

arnaud_bos20:07:45

I have a question about Rich's Ant colony code I've found on Github. Why is turn using dosync while move isn't, but "Must be called in a transaction". See https://gist.github.com/spacemanaki/1093917#file-ants-clj-L119

hiredman20:07:55

does that gist exist? I get github's 404 page

hiredman20:07:08

dosync has dynamic extent

hiredman20:07:39

meaning if you (dosync (foo)) the body of foo executes in the transaction

arnaud_bos20:07:58

Wow, I guess I've had it opened in my browser for so long that I didn't even realize it's not available anymore. Let me try to find it.

hiredman20:07:44

if I recall, the stm's transactions also merge, (dosync (foo)) and then foo is (defn foo [] (dosync ...)) the transaction merge and execute as a single transaction

arnaud_bos20:07:20

Still I'm not sure I understand your answer

hiredman20:07:23

it is very rare in practice to use the stm ever

arnaud_bos20:07:08

I'm just playing with it actually, trying to learn a few things

curtosis20:07:48

random thought from the old memory banks: has anyone built/used something like the CL *features* variable in clj?

curtosis20:07:18

not necessarily thinking reader macros per set

curtosis20:07:54

just “here’s a place to see what features I have enabled in this [dynamic] build”

hiredman20:07:12

dosync having dynamic extent means that once dosync is on the call stack, everything after that executes inside the transaction (and so can do things that only work inside a transaction) until dosync is popped off the call stack

arnaud_bos20:07:44

So is there any reason to use dosync in turn and not using it in move? Since both will be merged?

arnaud_bos20:07:54

Since turn is only ever used from inside the "main" (defn behave [] (dosync ...)), just like move