Fork me on GitHub
#beginners
<
2018-12-27
>
Aron12:12:27

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 https://github.com/isaacs/node-graceful-fs or other recovery and reboot options to be able to minimize runtime and power consumption

andy.fingerhut21:12:33

@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.

andy.fingerhut21:12:59

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.

andy.fingerhut21:12:24

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: https://clojuredocs.org/clojure.core/line-seq

andy.fingerhut21:12:03

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.

andy.fingerhut21:12:05

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

andy.fingerhut21:12:02

There is a book published a few years back called "The Clojure Cookbook" that you may find useful for finding many code examples in: https://github.com/clojure-cookbook/clojure-cookbook

Aron18:12:16

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

Aron18:12:03

Thanks for the references, I will soon try them

andy.fingerhut19:12:16

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.

Aron13:12:51

I meant complexities in the question I am asking.

Aron13:12:26

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

JanisOlex14:12:28

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)

valtteri14:12:07

You could store game-state in an atom https://clojuredocs.org/clojure.core/atom

valtteri14:12:39

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).

JanisOlex14:12:23

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

JanisOlex14:12:32

btw question, why atom and not ref?

valtteri14:12:11

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

valtteri14:12:50

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

valtteri14:12:31

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

JanisOlex14:12:34

thanks, will do atoms

valtteri14:12:38

Check out https://clojuredocs.org/clojure.core/gensym for identifiers. It’s sufficient if you’re staying within your app. Otherwise I’d recommend UUID:s.

alex31415920:12:59

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,

manutter5120:12:37

Maybe you're looking for reductions?

alex31415920:12:19

@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