3 ms·
It's true it's not nearly as ergonomic as haskell to pass around functions. You don't necessarily need to box the closure up on the heap in order to store a bun
by leshow 10y ago
It's true it's not nearly as ergonomic as haskell to pass around functions. You don't necessarily need to box the closure up on the heap in order to store a bunch of functions in say a vector though, it just needs to sit behind some kind of &, &mut, Arc, Rc, Box, etc.
I have seen a few hashmaps that store functions as values, in terms of real world uses.
You mentioned storing functions (closures here) in a vector, this isn't too much of a stretch I don't think:
let x = |c| c * 2;
let y = |v| v + 2;
let b: Vec<&Fn(_) -> _> = vec![&x, &y];
Definitely not "no higher order functions" as you claimed.
- wyager 10y agoBy the same token, C "supports" higher order functions; you can pass around a function pointer and a pointer to a closure. It's sort of an idiomatic question; HOFs are inconvenient enough in Rust that no one really uses them outside of a few specific cases (like local maps and folds).
- leshow 10y agoThere's a wide berth between inconvenient, which is subjective, and not supported. HOF's exist in Rust, could the ergonomics be better; that's up for debate.