新足迹

 找回密码
 注册

精华好帖回顾

· 西餐大赛---姐整的都是平民的(蛋糕详细做法已上 (2011-1-6) chesecake · 【OVATION OF THE SEAS】海洋礼赞号南太平洋【SOUTH PACIFIC】 (2023-12-12) 出国吃考拉
· 走马观花北海道-美食全部更新完毕。多图杀猫,慎入。 (2012-5-13) teddymu · For firststep and all others---《悉尼的鸽子和海鸥》 (2005-1-22) lilytop
Advertisement
Advertisement
查看: 1610|回复: 5

[IT] chatgpt根本就不能简化代码 [复制链接]

发表于 2023-10-10 08:32 |显示全部楼层
此文章由 DDD888 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 DDD888 所有!转贴必须注明作者、出处和本声明,并保持内容完整
本帖最后由 DDD888 于 2023-10-10 08:36 编辑

我写的代码和改进代码chatgpt4,结果是代码并没有简单化

make this rust language simpler pub fn download_url_as_string(user_agent: &str, url: &str) -> Result<String, ApplicationError> {
    Client::builder()
        .default_headers(get_default_headers(user_agent)?)
        .build()
        .report_context(ApplicationError:ownloadUrl)?
        .get(url)
        .send()
        .report_context(ApplicationError:ownloadUrl)?
        .text()
        .report_context(ApplicationError:ownloadUrl)
}

Certainly. Below is a simplified version of the original Rust function download_url_as_string. The original function is written in a chained manner, making it concise but potentially difficult to read. I've broken down the function into individual steps to make the code more straightforward. Additionally, I've added comments to describe what each step is doing.

rust
Copy code
pub fn download_url_as_string(user_agent: &str, url: &str) -> Result<String, ApplicationError> {
    // Build the HTTP client with default headers
    let mut headers = get_default_headers(user_agent)?;
    let client = match Client::builder().default_headers(headers).build() {
        Ok(c) => c,
        Err(_) => return Err(ApplicationError:ownloadUrl),
    };

    // Send GET request to the specified URL
    let mut request = match client.get(url).send() {
        Ok(r) => r,
        Err(_) => return Err(ApplicationError:ownloadUrl),
    };

    // Read the text from the response
    let text = match request.text() {
        Ok(t) => t,
        Err(_) => return Err(ApplicationError:ownloadUrl),
    };

    // Return the fetched text
    Ok(text)
}
Here's a breakdown of the changes:

The client building step has been separated out. The headers are obtained using the get_default_headers function, and then the client is built using Client::builder().default_headers(headers).build(). Any errors at this step will return ApplicationError:ownloadUrl.

The GET request to the specified URL is then sent using client.get(url).send(). Errors at this point will also return ApplicationError:ownloadUrl.

The text from the response is obtained using request.text(). Again, any error will return ApplicationError:ownloadUrl.

Finally, if all the steps succeed, the text is returned wrapped in an Ok variant of the Result type.

This should help in understanding what the function is doing at each step and make it easier to debug if issues arise.
Advertisement
Advertisement

发表于 2024-7-21 10:50 |显示全部楼层
此文章由 DDD888 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 DDD888 所有!转贴必须注明作者、出处和本声明,并保持内容完整
Another example chatgpt cannot generate arm assembly code for raspberry pico w

https://chatgpt.com/share/be0c8bce-402c-4a44-b6c8-02932ee00643

Here is the code I wrote and compile run correctly
pub unsafe fn asm_96_bit_add(a0: u32, a1: u32, a2: u32, b0: u32, b1: u32, b2: u32) {
    let mut c0: u32 = a0;
    let mut c1: u32 = a1;
    let mut c2: u32 = a2;

    asm!(
        "adds r0, r3",
        "adcs r1, r4",
        "adcs r2, r5",
        inout("r0") c0,
        inout("r1") c1,
        inout("r2") c2,
        in("r3") b0,
        in("r4") b1,
        in("r5") b2,
    );

    info!("Start c0 {:#X}", c0);
    info!("Start c1 {:#X}", c1);
    info!("Start c2 {:#X}", c2);
}

发表于 2024-7-21 10:57 来自手机 |显示全部楼层
此文章由 社区 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 社区 所有!转贴必须注明作者、出处和本声明,并保持内容完整
本帖最后由 社区 于 2024-7-21 11:18 编辑

There is really nothing in the code.  You ask it to search bones in an egg, that's what you get.

发表于 2024-7-23 20:24 |显示全部楼层
此文章由 DDD888 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 DDD888 所有!转贴必须注明作者、出处和本声明,并保持内容完整
DDD888 发表于 2024-7-21 09:50
Another example chatgpt cannot generate arm assembly code for raspberry pico w

https://chatgpt.com/ ...

Another example chatgpt is useless https://chatgpt.com/share/a5807067-b4eb-4885-a64b-e1a2908cdb20

Here is the code working and correct

pub unsafe fn asm_64_bit_subtraction_test() {
    asm_64_bit_subtraction(
        0x8, 0x7, //seperator
        0x9, 0x5,
    );
}
        
pub unsafe fn asm_64_bit_subtraction(a0: u32, a1: u32, b0: u32, b1: u32) {
    let mut c0: u32 = a0;
    let mut c1: u32 = a1;
      
    // https://doc.rust-lang.org/rust-by-example/unsafe/asm.html
    asm!(
        // c0 = a0 - b0
        // r0 = r0 - r2
        "subs r0, r2",
        // c1 = a1 - b1                                                                                                                                       
        // r1 = r1 - r3                                                                                                                                       
        "sbcs r1, r3",                                                                                                                                       
        inout("r0") c0,                                                                                                                                       
        inout("r1") c1,                                                                                                                                       
        in("r2") b0,                                                                                                                                          
        in("r3") b1,                                                                                                                                          
    );                                                                                                                                                        
                                                                                                                                                              
    info!("Start c0 {:#X}", c0);                                                                                                                              
    info!("Start c1 {:#X}", c1);                                                                                                                              
}                                       

发表于 2024-7-23 20:49 |显示全部楼层
此文章由 bingocn 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 bingocn 所有!转贴必须注明作者、出处和本声明,并保持内容完整
chatgpt是经常写错,我从来不纠正它

发表于 2024-7-24 09:03 |显示全部楼层
此文章由 DDD888 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 DDD888 所有!转贴必须注明作者、出处和本声明,并保持内容完整
bingocn 发表于 2024-7-23 19:49
chatgpt是经常写错,我从来不纠正它

我想我向chatgpt问前面的几个问题太愚蠢了,这问题即使问普通人,也不能得到正确的回答,要能回答我最后问题的人首先必须懂arm汇编语言,其次要会rust language编程,还要有个硬件raspberrypi pico w可以实践,这三个条件的组合大概全世界很少人啦,我还去问一个靠加法来得出答案的chatgpt显然是愚蠢的行为。
Advertisement
Advertisement

发表回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Advertisement
Advertisement
返回顶部