mark.warren
2019-7-24 08:35:25

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?


soegaard2
2019-7-24 10:17:25

Large or small file?


mark.warren
2019-7-24 10:37:41

Large


mark.warren
2019-7-24 10:39:29

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


soegaard2
2019-7-24 10:40:12

If the distance from the end to the start of the trailer is small, then I think both your suggestions will perform the same.


soegaard2
2019-7-24 10:40:58

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.


mark.warren
2019-7-24 10:42:12

@soegaard2 Hmm.. Yes that’s a possibility, treat it as a smaller file effectively, I’ll try it.


soegaard2
2019-7-24 10:43:06

If you don’t mind calling out to system utilities, I saw a few suggestions on StackOverflow to use tac file \| grep -m1 pattern


soegaard2
2019-7-24 10:43:31

Here tac prints the lines in reverse order, and grep -m1 stops at first match.


mark.warren
2019-7-24 10:43:52

I’d like to keep it in Racket, but that is an interesting approach.


soegaard2
2019-7-24 10:44:49

If you are very curious, you could check the source of tac to see if there are some tricks involved.


mark.warren
2019-7-24 10:45:00

Good idea.


mark.warren
2019-7-24 13:22:43

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?


soegaard2
2019-7-24 13:24:28

I think ! is generally used for side effects.


mark.warren
2019-7-24 13:25:04

Ok that makes sense.


soegaard2
2019-7-24 13:25:39

Compare the Racket style guide with: https://mumble.net/~campbell/scheme/style.txt


mark.warren
2019-7-24 13:27:04

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.


soegaard2
2019-7-24 13:28:21

Also see this list:



mark.warren
2019-7-24 13:28:58

Thanks