|
| 1 | +## 程序员辅助编程利器只是AI应用的开始: cursor, An editor built for programming with AI |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2023-03-20 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , PolarDB , chatgpt , openai , 辅助编程 |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +程序员用得最多的是IDE, 编程工具. cursor这个厂商很有想法, 与GPT-4结合, 直接做了一个AI编程的editor, 与广大程序员建立了0距离连接. |
| 16 | + |
| 17 | +推而广之: openai 将AI能力接口化, 确实会被非常方便的普及AI能力, 控制算法算力, 就像控制短信、网络流量一样, 是一门长期、市场容量天花板极高的生意. 那些占有大量用户时间(包括碎片时间)的产品该注意了, 随时可能被加持AI的产品颠覆, 成为新的用户入口. |
| 18 | + |
| 19 | +这是一个辅助编程的“chatgpt” , 后台应用了openai的gpt技术. 当然解决的是编程的问题, 具备一定编程基础的同学, 可以大幅度提升编程效率. 对于没有编程能力的同学就别做梦了, 因为它给你的代码也不知道对不对, 而且总有需要你微调的地方. |
| 20 | + |
| 21 | +An editor built for programming with AI |
| 22 | + |
| 23 | +Build Software. Fast. Write, edit, and chat about your code with GPT-4 |
| 24 | + |
| 25 | +https://www.cursor.so/ |
| 26 | + |
| 27 | + |
| 28 | +例子1: statistic from duckdb: |
| 29 | + |
| 30 | +``` |
| 31 | +// Welcome to Cursor |
| 32 | + |
| 33 | +// 1. Try generating with command K on a new line. Ask for a new react component of an error popup. |
| 34 | +// 2. Then, select the outputted code and hit chat. Ask if there's a bug. Ask how to improve. |
| 35 | +// 3. Try selecting some code and hitting edit. Ask the bot to add a button that updates a statefield. |
| 36 | +// 4. To try out cursor on your own projects, go to file menu (top left) and open a folder. |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +import duckdb |
| 41 | + |
| 42 | +# connect to the database |
| 43 | +con = duckdb.connect(database='/path/to/database.db') |
| 44 | + |
| 45 | +# execute a query |
| 46 | +result = con.execute('SELECT COUNT(*) FROM my_table') |
| 47 | + |
| 48 | +# fetch the result |
| 49 | +count = result.fetchone()[0] |
| 50 | + |
| 51 | +# print the count |
| 52 | +print(f'Total rows in my_table: {count}') |
| 53 | +``` |
| 54 | + |
| 55 | + |
| 56 | +例子2: write as rust, connect to duckdb, statistic something. |
| 57 | + |
| 58 | +``` |
| 59 | +use duckdb::{Connection, Result}; |
| 60 | + |
| 61 | +fn main() -> Result<()> { |
| 62 | + let conn = Connection::open("/path/to/database.db")?; |
| 63 | + let result = conn.query("SELECT COUNT(*) FROM my_table", &[])?; |
| 64 | + let count: i64 = result.fetch_one()?.get(0)?; |
| 65 | + println!("Total rows in my_table: {}", count); |
| 66 | + Ok(()) |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +例子3: write as rust, connect to postgresql, load data from csv file, modify it by some logical, finaly query it by some logical. |
| 72 | + |
| 73 | +``` |
| 74 | +use postgres::{Client, NoTls}; |
| 75 | +use std::error::Error; |
| 76 | +use std::fs::File; |
| 77 | +use std::io::{BufRead, BufReader}; |
| 78 | + |
| 79 | +fn main() -> Result<(), Box<dyn Error>> { |
| 80 | + let mut client = Client::connect("postgresql://postgres:password@localhost:5432/mydb", NoTls)?; |
| 81 | + |
| 82 | + let file = File::open("data.csv")?; |
| 83 | + let reader = BufReader::new(file); |
| 84 | + |
| 85 | + for line in reader.lines() { |
| 86 | + let line = line?; |
| 87 | + let parts: Vec<&str> = line.split(',').collect(); |
| 88 | + |
| 89 | + let name = parts[0]; |
| 90 | + let age = parts[1].parse::<i32>()?; |
| 91 | + let email = parts[2]; |
| 92 | + |
| 93 | + client.execute( |
| 94 | + "INSERT INTO users (name, age, email) VALUES ($1, $2, $3)", |
| 95 | + &[&name, &age, &email], |
| 96 | + )?; |
| 97 | + } |
| 98 | + |
| 99 | + client.execute( |
| 100 | + "UPDATE users SET age = age + 1 WHERE age < 30", |
| 101 | + &[], |
| 102 | + )?; |
| 103 | + |
| 104 | + let rows = client.query( |
| 105 | + "SELECT name, age, email FROM users WHERE age >= 30", |
| 106 | + &[], |
| 107 | + )?; |
| 108 | + |
| 109 | + for row in rows { |
| 110 | + let name: &str = row.get(0); |
| 111 | + let age: i32 = row.get(1); |
| 112 | + let email: &str = row.get(2); |
| 113 | + |
| 114 | + println!("{} ({}): {}", name, age, email); |
| 115 | + } |
| 116 | + |
| 117 | + Ok(()) |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +读不懂的地方, 还可以提问, 例如下面好多问号, 到底是什么意思, 你可以选中一个问号, 然后问它: `why here is a ?` |
| 124 | + |
| 125 | +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. |
| 126 | + |
| 127 | +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. |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +#### [期望 PostgreSQL|开源PolarDB 增加什么功能?](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216") |
| 132 | + |
| 133 | + |
| 134 | +#### [PolarDB 云原生分布式开源数据库](https://github.com/ApsaraDB "57258f76c37864c6e6d23383d05714ea") |
| 135 | + |
| 136 | + |
| 137 | +#### [PolarDB 学习图谱: 训练营、培训认证、在线互动实验、解决方案、内核开发公开课、生态合作、写心得拿奖品](https://www.aliyun.com/database/openpolardb/activity "8642f60e04ed0c814bf9cb9677976bd4") |
| 138 | + |
| 139 | + |
| 140 | +#### [PostgreSQL 解决方案集合](../201706/20170601_02.md "40cff096e9ed7122c512b35d8561d9c8") |
| 141 | + |
| 142 | + |
| 143 | +#### [德哥 / digoal's github - 公益是一辈子的事.](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae") |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
0 commit comments