Skip to content

Latest commit

 

History

History
153 lines (92 loc) · 6.21 KB

20230320_01.md

File metadata and controls

153 lines (92 loc) · 6.21 KB

程序员辅助编程利器只是AI应用的开始: cursor, An editor built for programming with AI

作者

digoal

日期

2023-03-20

标签

PostgreSQL , PolarDB , chatgpt , openai , 辅助编程


背景

程序员用得最多的是IDE, 编程工具. cursor这个厂商很有想法, 与GPT-4结合, 直接做了一个AI编程的editor, 与广大程序员建立了0距离连接.

推而广之: openai 将AI能力接口化, 确实会被非常方便的普及AI能力, 控制算法算力, 就像控制短信、网络流量一样, 是一门长期、市场容量天花板极高的生意. 那些占有大量用户时间(包括碎片时间)的产品该注意了, 随时可能被加持AI的产品颠覆, 成为新的用户入口.

这是一个辅助编程的“chatgpt” , 后台应用了openai的gpt技术. 当然解决的是编程的问题, 具备一定编程基础的同学, 可以大幅度提升编程效率. 对于没有编程能力的同学就别做梦了, 因为它给你的代码也不知道对不对, 而且总有需要你微调的地方.

An editor built for programming with AI

Build Software. Fast. Write, edit, and chat about your code with GPT-4

https://www.cursor.so/

例子1: statistic from duckdb:

// Welcome to Cursor  
  
// 1. Try generating with command K on a new line. Ask for a new react component of an error popup.  
// 2. Then, select the outputted code and hit chat. Ask if there's a bug. Ask how to improve.  
// 3. Try selecting some code and hitting edit. Ask the bot to add a button that updates a statefield.  
// 4. To try out cursor on your own projects, go to file menu (top left) and open a folder.  
  
  
  
import duckdb  
  
# connect to the database  
con = duckdb.connect(database='/path/to/database.db')  
  
# execute a query  
result = con.execute('SELECT COUNT(*) FROM my_table')  
  
# fetch the result  
count = result.fetchone()[0]  
  
# print the count  
print(f'Total rows in my_table: {count}')  

例子2: write as rust, connect to duckdb, statistic something.

use duckdb::{Connection, Result};  
  
fn main() -> Result<()> {  
    let conn = Connection::open("/path/to/database.db")?;  
    let result = conn.query("SELECT COUNT(*) FROM my_table", &[])?;  
    let count: i64 = result.fetch_one()?.get(0)?;   
    println!("Total rows in my_table: {}", count);  
    Ok(())  
}  

例子3: write as rust, connect to postgresql, load data from csv file, modify it by some logical, finaly query it by some logical.

use postgres::{Client, NoTls};  
use std::error::Error;  
use std::fs::File;  
use std::io::{BufRead, BufReader};  
  
fn main() -> Result<(), Box<dyn Error>> {  
    let mut client = Client::connect("postgresql://postgres:password@localhost:5432/mydb", NoTls)?;  
  
    let file = File::open("data.csv")?;  
    let reader = BufReader::new(file);  
  
    for line in reader.lines() {  
        let line = line?;  
        let parts: Vec<&str> = line.split(',').collect();  
  
        let name = parts[0];  
        let age = parts[1].parse::<i32>()?;  
        let email = parts[2];  
  
        client.execute(  
            "INSERT INTO users (name, age, email) VALUES ($1, $2, $3)",  
            &[&name, &age, &email],  
        )?;  
    }  
  
    client.execute(  
        "UPDATE users SET age = age + 1 WHERE age < 30",  
        &[],  
    )?;  
  
    let rows = client.query(  
        "SELECT name, age, email FROM users WHERE age >= 30",  
        &[],  
    )?;  
  
    for row in rows {  
        let name: &str = row.get(0);  
        let age: i32 = row.get(1);  
        let email: &str = row.get(2);  
  
        println!("{} ({}): {}", name, age, email);  
    }  
  
    Ok(())  
}  

读不懂的地方, 还可以提问, 例如下面好多问号, 到底是什么意思, 你可以选中一个问号, 然后问它: why here is a ?

The ? in the selected code block is used to propagate errors up the call stack. It is a shorthand for the try! macro. The try! macro is used to propagate errors up the call stack by returning early from the function if an error occurs. The ? operator was introduced in Rust 1.13 as a more concise way of using the try! macro.

In the given code, parts[1].parse::<i32>()? is used to parse the second element of the parts vector as an i32 and propagate any errors that occur during the parsing process up the call stack. If an error occurs, the function will return early with the error.

digoal's wechat