Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expressive Lambda Expressions #3497

Closed
wants to merge 1 commit into from

Conversation

wiseaidev
Copy link

@wiseaidev wiseaidev commented Sep 24, 2023

  • This RFC proposes the introduction of lambda expressions (anonymous functions) in Rust using (arg1, arg2, ..., argn) -> { // code block } notation.
  • Lambda expressions provide a concise syntax for defining small, inline functions that can capture surrounding scope variables.
  • The feature aims to improve code readability and expressiveness, especially for short, one-time-use functions.
  • It aligns with the trend of incorporating functional programming features into modern languages.
  • This RFC will not only introduce lambda expressions but also enable developers to implement custom closures from lambda expressions:
// Define a Closure struct
struct MyClosure<F> {
    func: F,
}

impl<F> MyClosure<F> {
    // Create a new Closure instance
    fn new(func: F) -> Self {
        MyClosure { func }
    }

    // Call the closure with arguments
    fn call(&self, x: i32, y: i32) -> i32
    where
        F: Fn(i32, i32) -> i32,
    {
        (self.func)(x, y)
    }
}

fn main() {
    // Create a Closure instance using a lambda expression
    let addition = MyClosure::new((x, y) -> { x + y });
    let result_add = addition.call(6, 3);
    println!("Addition Result: {}", result_add); // Outputs "Addition Result: 9"
}

Rendered

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This RFC simultaneously seems to acknowledge the existence of closures (also focusing very narrowly on RFC 1558) while suggesting an addition to the language that is essentially just closures with a new syntax. What is the difference here?


## Alternatives

While closures are powerful and versatile, which involve using the `|args| { code }` notation. they may appear less intuitive, especially to developers accustomed to languages with lambda expressions. Lambda expressions provide a more concise and readable way to define short, anonymous functions, which can lead to improved code maintainability and understandability, especially in situations where functions are used as arguments or returned values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not really an argument being made here about why closures are less intuitive or why this new syntax is more intuitive.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more like a complement to closures.

@compiler-errors
Copy link
Member

Also, honest question -- was this RFC written by a LLM? Sorry if I'm overstepping here, but the way the text of the RFC reads suggests that it may have been.

@wiseaidev
Copy link
Author

Also, honest question -- was this RFC written by a LLM?

Off-topic/grey question. Well, I can't reveal all my secrets, but let's just say I'm here to make Rust even more "LLMazing"! 😄 On a serious note, lambda expressions can be a powerful addition to the language, and I'm here to help answer any questions or concerns you might have about this RFC.

@CryZe
Copy link

CryZe commented Sep 24, 2023

Well the LLM failed to acknowledge (partly) that Rust already has closures and thus there's no section even comparing the two and how the new syntax would be better. Half of this reads like Rust doesn't have any "lambdas" at all.

@wiseaidev
Copy link
Author

Y'all pressing L + Ratio for no objective reason. It's making me feel really sad 😢. I'mma heading out 🚪🚶.

@BoxyUwU
Copy link
Member

BoxyUwU commented Sep 24, 2023

dont waste peoples time by opening PRs generated with ai

@BoxyUwU BoxyUwU closed this Sep 24, 2023
@wiseaidev
Copy link
Author

wiseaidev commented Sep 24, 2023

Fine. I'mma try harder next time. Besides the point, why does it matter if a PR was generated with AI? Does it suggest that human-generated ones are inherently superior? 🤔

P.S. I see you
Screenshot from 2023-09-24 21-03-20

P.P.S. On President Joe Biden's life, this PR it is not generated with AI.

@crabdancing
Copy link

I... wha? What is happening!? Why, @wiseaidev? Why are you doing this to us? What did we do to deserve this?

@wiseaidev
Copy link
Author

I... wha? What is happening!? Why, @wiseaidev?

Nothing worth mentioning, ma fren. I'm simply trying my best to enhance the language, nothing more, nothing less. You're welcome to either accept or decline this PR, and that's perfectly okay. It seems like there's a bit of unnecessary drama surrounding it. And why criticize the author of this PR instead of objectively evaluating the content?

@workingjubilee
Copy link
Contributor

Off-topic/grey question. Well, I can't reveal all my secrets, but let's just say I'm here to make Rust even more "LLMazing"! 😄

Fine. I'mma try harder next time. Besides the point, why does it matter if a PR was generated with AI? Does it suggest that human-generated ones are inherently superior? 🤔

Machines currently cannot claim copyright on their works, and that does not mean that you can then assert copyright by copying their output and saying "I am a human, therefore it is my work." We rely on contributors having title over what they write, or if they reference outside material, declaring the reference.

P.P.S. On President Joe Biden's life, this PR it is not generated with AI.

The question to answer would have been, "did you use any machine-learning-model-driven tools to generate even one byte of output which you then copied into this pull request?"

And why criticize the author of this PR instead of objectively evaluating the content?

Because the content is meaningless if the PR cannot be accepted due to legal considerations. Do you want to be part of a lawsuit for the next 10 years of your life ongoing for every 3 months because you did something as pointless as fudge the truth about whether you used a model to emit text or not?

@wiseaidev
Copy link
Author

Machines currently cannot claim copyright on their works

The key question here is: How can you tell if it's written by machines or humans? What GPTs and other language models do is quite similar to what we do naturally – they read documents, learn from them, and predict/write/generate relatively new outputs/content, but at a higher scale. So, you could level the same accusation against humans: learning/using copyrighted material, and you cannot claim anything at all.

@NicoleKai
Copy link

@wiseaidev
Thanks for the PR, but please let it go. We already have closures.

@rust-lang rust-lang locked as too heated and limited conversation to collaborators Sep 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants