I seem to be having brain block again today, I’m trying to think of a sensible way of searching backwards through a file to find a trailer definition. At the moment the only thing I can think of seems very inefficient, does anyone have a clever approach? I need to find a line that has the world ‘trailer’ by searching from the end of the file. At the moment I have two ideas, one is to search backwards for a new line and then do a read-line
to see if it is the word ‘trailer., the other is to read backwards byte by byte looking for the word ’trailer’. Both seem very wasteful, but I can’t seem to think of a better way. Any ideas, or will one of my approaches be the best idea?
Large or small file?
Large
@soegaard2 It’s probably not really a question for this channel, as it’s more algorithm based than a question about Racket. I was just having brain block and I thought I’d ask.
If the distance from the end to the start of the trailer is small, then I think both your suggestions will perform the same.
If the distance is large you can consider reading a block of, say, 4096 bytes at a time (from the end) and then search for newlines in the block.
@soegaard2 Hmm.. Yes that’s a possibility, treat it as a smaller file effectively, I’ll try it.
If you don’t mind calling out to system utilities, I saw a few suggestions on StackOverflow to use tac file \| grep -m1 pattern
Here tac
prints the lines in reverse order, and grep -m1
stops at first match.
I’d like to keep it in Racket, but that is an interesting approach.
If you are very curious, you could check the source of tac
to see if there are some tricks involved.
Good idea.
I’ve been reading through the style guide and I was wondering if there is a naming convention for functions that have side effects. It mentions setters ending with !, but I can’t see anything about side effects. Does anyone have an opinion on this?
I think ! is generally used for side effects.
Ok that makes sense.
Compare the Racket style guide with: https://mumble.net/~campbell/scheme/style.txt
Thanks, I’ll read that now. I am doing a number of functions that work on ports and I wanted to indicate that the function may leave the current position changed.
Also see this list:
Thanks