Skip to content

Instantly share code, notes, and snippets.

@ictrobot
Created February 6, 2017 22:16
Show Gist options
  • Save ictrobot/94c6457364a22b5428e1b1f44edc9d2b to your computer and use it in GitHub Desktop.
Save ictrobot/94c6457364a22b5428e1b1f44edc9d2b to your computer and use it in GitHub Desktop.
(lambda file:(lambda file, pos: [len(file.read(1)) == 0, file.seek(pos)][0])(file, file.tell()))

Lambda returns if file is at eof

#How it works:

len(file.read(1)) == 0

Trys to read one character and checks if the returned string has a length of 0 (if you read a file at eof you get a zero length string).

lambda file, pos: [len(file.read(1)) == 0, file.seek(pos)][0]

Creates a lambda that takes file and a position to seek back to. It creates an array of the expression above (if the file is at eof) and the return value of the seek command(moving the file position back to where it was). The lambda then returns the first value from the array (if the file is at eof).

(lambda file:(lambda file, pos: [len(file.read(1)) == 0, file.seek(pos)][0])(file, file.tell()))

The previous lambda is wrapped in a lambda which just takes file and calls the previous lambda with file and the current file position before reading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment