Fork me on GitHub

i installed clojure, started clj on my local machine. how do you find the longest word (something like /(\w+)/) from all .java files from a particular local git repository? this is not needed for anything really, I just realized that while with js I can do this if not easily but I do know most of the possible solutions and their pitfalls, I have no idea how to do this with clojure. I saw file-seq, and I saw how to filter by filenames, but I don't know how to go from there to content strings and how to convert those to words, especially that this is a very large git repo, so for js I need something like or other recovery and reboot options to be able to minimize runtime and power consumption


@U0VQ4N5EE Are you looking for a Clojure/Java solution, or ClojureScript? I ask because the details of file I/O often require at least a little bit of direct interaction with the host platform (Java, or JavaScript), so the answers will differ slightly between them.


At least for Clojure/Java, you mention file-seq, which is good for getting a list of all files recursive from some specified root directory. You can use a function like (clojure.string/ends-with? filename ".java") to determine whether the string filename ends with ".java" or not.


There is a function line-seq that returns a sequence of strings, one per line of the contents of a file you have opened, which you can see examples of use here:


A function like re-seq can be used to take a string, e.g. containing a line of text from a file, and return all matches it contains to some specified regex, e.g. a sequence of word-constituent characters like #"\w+" that you mentioned.


user=> (re-seq #"\w+" " The quick brown fox jumped-over the lazy dog?") ("The" "quick" "brown" "fox" "jumped" "over" "the" "lazy" "dog")


There is a book published a few years back called "The Clojure Cookbook" that you may find useful for finding many code examples in:


either, really. I seems like a question that has lots of hidden complexities


Thanks for the references, I will soon try them


I don't think there are a lot of hidden complexities -- it is just that I am much more knowledgeable about the details of Clojure/Java than I am of ClojureScript, and would direct you to the #clojurescript channel if you would prefer a solution to how to do file I/O on that platform.


I meant complexities in the question I am asking.


Not the one you are answer, probably, if you don't think so. I am not assuming anything about that.


hi, Question I am using compojure/ring libs to create simple web game. I am thinking how to share game state betewen players (2-4) on the web. Session usually is for one user, where you can keep stuff, but how can I make like "game-session" where all 4 players can access the data Some strange scope? I don't want to keep data in database, only in memory (in worst case - on file system)


You could store game-state in an atom


You can read/write the atom value and add it to responses accordingly. Atom will survive as long as your app runs, but there’s no persistence in case your JVM crashes etc. Your clients need to poll for state changes unless you’re using websockets to push the changes to all interested clients (players).


Thanks. Sounds like a plan. Also probably I have to add some random generated "identifier", which is shared between players, but random enough so players can not "join" other game sessions on the server. It is ok, if JVM crashes etc. I am ok to live in only memory. Server restarts are not required for me at this point


btw question, why atom and not ref?


Atoms are simpler and more widely used for this kind of situations.


AFAIK you’d need refs only if you have to coordinate multiple state changes in a “transaction” kind of situation.


I’ve never actually used refs myself. :thinking_face: Good question!


thanks, will do atoms


Check out for identifiers. It’s sufficient if you’re staying within your app. Otherwise I’d recommend UUID:s.


Hi - is there a simple way to map or reduce a function over an expanding sequence? That is if m is a map (0 ... n) I'd like the first iteration to use (0) the second to use (0 1) and the last one to use (0 ...n)? I guess I can create a range with the size of the sequence and then have the function call (take j m) but it seemed to me there could be a more native way? thanks,


Maybe you're looking for reductions?


@manutter51 thanks... that might work, I should be able to do map f (reductions conj [] m) Maybe not very efficient but I don't need speed at this point