If we were to create a Rust version of this page for Haskell, what cool programming techniques would you add to it?

  • tuna@discuss.tchncs.de
    link
    fedilink
    arrow-up
    1
    ·
    2 hours ago

    Something i didnt know for a long time (even though its mentioned in the book pretty sure) is that enum discriminants work like functions

    #[derive(Debug, PartialEq, Eq)]
    enum Foo {
        Bar(i32),
    }
    
    let x: Vec<_> = [1, 2, 3]
        .into_iter()
        .map(Foo::Bar)
        .collect();
    assert_eq!(
        x,
        vec![Foo::Bar(1), Foo::Bar(2), Foo::Bar(3)]
    );
    

    Not too crazy but its something that blew my mind when i first saw it

    • little_ferrisOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      24 minutes ago

      Yea it’s like when we writeSome(2). It’s not a function call but a variant of the Option enum.

    • Ephera@lemmy.ml
      link
      fedilink
      arrow-up
      2
      ·
      1 hour ago

      This works with anything that one might call “named tuples”.

      So, you can also define a struct like so and it’ll work:

      struct Baz(i32);
      

      On the other hand, if you define an enum variant with the normal struct syntax, it does not work:

      enum Foo {
          ...
          Qux { something: i32 } //cannot omit braces
      }
      
      • SorteKanin@feddit.dk
        link
        fedilink
        arrow-up
        9
        ·
        3 hours ago

        It’s a test for the compiler which ensures that these legal yet extremely weird expressions continue to compile as the compiler is updated. So there is a purpose to the madness but it does still look pretty funny.

  • Ephera@lemmy.ml
    link
    fedilink
    arrow-up
    12
    ·
    5 hours ago

    A cool thing you can do, is to store values of all kinds of types in a big HashMap, basically by storing their TypeId and casting them to Box<dyn Any> (see std::any).
    Then you can later retrieve those values by specifying the type (and optionally another ID for storing multiple values of the same type).

    So, you can create an API which is used like this:

    let value = MyType::new();
    storage.insert(value);
    let retrieved = storage.get::<MyType>();
    assert_eq!(retrieved, value);
    

    There’s various ECS storage libraries which also implement such an API. Depending on what you’re doing, you might prefer to use those rather than implementing it yourself, but it’s not too difficult to implement yourself.

    • Buttons
      link
      fedilink
      English
      arrow-up
      1
      ·
      3 hours ago

      What if I specify the wrong type? let retrieved = storage.get::<SomeOtherType>();?

      Is it a runtime error or a compile time error?

        • Ephera@lemmy.ml
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          3 hours ago

          Well, you would determine the TypeId of SomeOtherType, then search for that as the key in your HashMap and get back a None, because no entry exists and then you’d hand that back to the user.
          I guess, my little usage example should’ve included handling of an Option value…

          So, it’s only a runtime error, if you decide to .unwrap() or similar.

  • BB_C
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    3 hours ago

    Can’t think of anything.
    The novelty must have worn off over time.
    Or maybe my mind doesn’t get blown easily.

  • silasmariner
    link
    fedilink
    arrow-up
    11
    ·
    8 hours ago

    Rust isn’t really a language that lends itself to terse point-free functional idioms… The sort of examples I might want to share would probably require a bit more context, certainly more code. Like I think type guards and arena allocation are cool and useful tricks but I don’t think I could write a neat little example showing or motivating either

  • solrize@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    7 hours ago

    I’d like to see a Rust solution to Tony Morris’s tic tac toe challenge:

    https://blog.tmorris.net/posts/scala-exercise-with-types-and-abstraction/index.html

    His rant about it is here:

    https://blog.tmorris.net/posts/understanding-practical-api-design-static-typing-and-functional-programming/

    I did a Haskell GADT solution some time back and it’s supposed to be doable in Java and in C++. Probably Rust too. I wonder about Ada.