Re:The unknown features of Python's `operator` module
by klyrs on Sunday August 08, 2021 @11:35AM
(#28107459)
methodcaller can be useful in functional programming, like
where exhaust is a function that consumes an iterator. Obviously this is a contrived example, but it's a weird transformation -- it turns map from 'call this function on each of a sequence of arguments' to, roughly 'call each of this sequence of functions on a fixed argument'
[ Parent
| on HN ]
Re:The unknown features of Python's `operator` module
by wruza on Sunday August 08, 2021 @02:28PM
(#28108900)
It’s higher order for the sake of higher order, and unreadable as hell when you return to five full pages of this nonsense a year later. These getter-caller generators are cool to show off, but irl the most naturally readable form is either a plain anonymous function, or a special form suggested by a sibling commenter.
Both by a compiler easily readable by a human unreadable out of purely functional languages by design this high fashion came.
Re:The unknown features of Python's `operator` module
by klyrs on Sunday August 08, 2021 @07:55PM
(#28111311)
Sometimes you need to do higher order stuff (or, maybe you don't? But it exists for good reason)
Do you think it's better to use a conceptually difficult example to showcase a high order function? Or do you think it's better to show an example where the reader can accurately understand the concept and focus on what the function does?
Re:The unknown features of Python's `operator` module
by xrisk on Sunday August 08, 2021 @12:26PM
(#28107872)
exhaust((file.close() for file in files)) works.
Re:The unknown features of Python's `operator` module
by klyrs on Sunday August 08, 2021 @12:40PM
(#28108015)
Like I said, contrived example. Though, it's a specific example of a more general pattern where you have a bunch of objects that you need to send a message to. You might use this for event handling in the guts of a game engine.