www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Fun with OpenAI

reply Rumbu <rumbu rumbu.ro> writes:
I suppose that everybody heard about https://chat.openai.com/chat

Let's have fun.

Question: Split a string in D at each comma without using the 
garbage collector

Answer:

In the D programming language, you can split a string into 
substrings at each comma without using the garbage collector by 
using a combination of the std.array.array and std.algorithm.map 
functions from the std.array and std.algorithm modules.

The std.array.array function allows you to create a fixed-size 
array with a specified capacity, which means that the array will 
not need to be reallocated or resized as you add elements to it. 
This can be useful if you want to avoid using the garbage 
collector.

The std.algorithm.map function allows you to apply a function to 
each element of an input range and return a new range with the 
transformed elements. In this case, you can use the map function 
to iterate through the characters of the string and create a new 
string for each character that is not a comma.

Here's an example of how you might use these functions to split a 
string at each comma without using the garbage collector:

```d
import std.array;
import std.algorithm;
import std.stdio;

void main()
{
     string s = "Hello, World!, How are you doing?";

     // Create an array with a capacity equal to the length of the 
string
     auto substrings = array!string(s.length);

     // Iterate through the characters of the string and add each 
character to the array
     // if it is not a comma
     substrings = s.map!(c => c == ',' ? "" : c.to!string());

     // Print the substrings
     for (auto substring : substrings)
     {
         writeln(substring);
     }
}

```

This code will output the following:
```
Copy code
Hello
  World!
  How are you doing?
```

Keep in mind that this approach may be less efficient than using 
the std.algorithm.split function, which uses the garbage 
collector to allocate memory for the returned array of 
substrings. If you don't mind using the garbage collector, you 
may want to consider using split instead, as it can be simpler 
and more efficient in some cases.
Dec 19 2022
next sibling parent reply Robert Schadek <rburners gmail.com> writes:
Considering that the D traning data is likely sparse compared to 
java and the like, this is really good.
Took me a while to see that the for, well foreach, was incorrect.
Sure the program still depends on the GC, but hey. If I were an 
AI I would use the GC as well.

Very cool, and scary indeed.
Dec 19 2022
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 12/19/2022 12:17 AM, Robert Schadek wrote:
 Very cool, and scary indeed.
Scary, yes. I wonder if we could improve the results by improving the documentation on these functions.
Dec 19 2022
prev sibling parent RTM <riven baryonides.ru> writes:
On Monday, 19 December 2022 at 08:17:09 UTC, Robert Schadek wrote:
 Very cool, and scary indeed.
Paired with proper knowledge representation, it could become the Grail of rapid prototyping.
Dec 20 2022
prev sibling next sibling parent reply thebluepandabear <therealbluepandabear protonmail.com> writes:
Everything is good except for the 'for' loop.
Dec 19 2022
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Monday, 19 December 2022 at 09:21:22 UTC, thebluepandabear 
wrote:
 Everything is good except for the 'for' loop.
It didn't check its answer -- somehow makes it more human-like... -Bastiaan.
Dec 19 2022
prev sibling next sibling parent evilrat <evilrat666 gmail.com> writes:
On Monday, 19 December 2022 at 08:01:47 UTC, Rumbu wrote:
 Question: Split a string in D at each comma without using the 
 garbage collector
Here is GPT-J-6B output for comparison(I assume ChatGPT using their biggest model that has 176 Billion params compared to just 6 Billions in this case), I had to adapt it and run several times, also had to use limit because I was running it on CPU. One of the variants was simply using split directly in just one line. Another example had included console output but was quirky. Sometimes I see how it tries using STL but with D syntax. Anyway, haven't played that much with GPT-J, but it takes some trial and error to get something meaningful esp. with smaller GPT variants. And running this on the CPU is no fun. ``` 0: // A Dlang program that splits a string at each comma without using the garbage collector import std.algorithm; import std.stdio; void main() { string s = "Hello, World"; /// ---- (this part below is generated output) ---- foreach (i; 0.. s.length) { if (s[i] == ',') { writefln("split string at comma '%s' in line %s", s.substring(0, i), s.length); writefln("into: %s", s.substring(0, i)); split(s.substring(i+1).split(',')); } } } ```
Dec 19 2022
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/19/22 3:01 AM, Rumbu wrote:

 The std.array.array function allows you to create a fixed-size array 
 with a specified capacity, which means that the array will not need to 
 be reallocated or resized as you add elements to it. This can be useful 
 if you want to avoid using the garbage collector.
Hm... this sounds wrong. std.array.array allocates. And it definitely doesn't make a fixed-size array.
      // Create an array with a capacity equal to the length of the string
      auto substrings = array!string(s.length);
Not very correct ;) -Steve
Dec 19 2022
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 12/19/2022 7:53 PM, Steven Schveighoffer wrote:
      // Create an array with a capacity equal to the length of the string
      auto substrings = array!string(s.length);
Not very correct ;)
But it *looks* seductively plausible, yet has insidious errors. I predict that using AI generated code is going to result in strange problems.
Dec 20 2022
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Tuesday, 20 December 2022 at 08:06:49 UTC, Walter Bright wrote:
 On 12/19/2022 7:53 PM, Steven Schveighoffer wrote:
      // Create an array with a capacity equal to the length 
 of the string
      auto substrings = array!string(s.length);
Not very correct ;)
But it *looks* seductively plausible, yet has insidious errors. I predict that using AI generated code is going to result in strange problems.
I predict you will be correct ;)
Dec 20 2022
prev sibling next sibling parent reply bachmeier <no spam.net> writes:
On Tuesday, 20 December 2022 at 08:06:49 UTC, Walter Bright wrote:
 On 12/19/2022 7:53 PM, Steven Schveighoffer wrote:
      // Create an array with a capacity equal to the length 
 of the string
      auto substrings = array!string(s.length);
Not very correct ;)
But it *looks* seductively plausible, yet has insidious errors. I predict that using AI generated code is going to result in strange problems.
Not if you do it correctly. You just have to give the computer a precise description of everything you want it to do for you. That can be done by creating a document and feeding it to the computer for processing.
Dec 20 2022
next sibling parent Dom Disc <dominikus scherkl.de> writes:
On Tuesday, 20 December 2022 at 16:22:24 UTC, bachmeier wrote:
 On Tuesday, 20 December 2022 at 08:06:49 UTC, Walter Bright 
 wrote:
 I predict that using AI generated code is going to result in 
 strange problems.
Not if you do it correctly. You just have to give the computer a precise description of everything you want it to do for you. That can be done by creating a document and feeding it to the computer for processing.
Aka a program. :-D Yup, that might work (most of the time). I think Walter B.'s right.
Dec 20 2022
prev sibling next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 12/20/2022 8:22 AM, bachmeier wrote:
 Not if you do it correctly. You just have to give the computer a precise 
 description of everything you want it to do for you. That can be done by 
 creating a document and feeding it to the computer for processing.
The precise description is just another programming language!
Dec 20 2022
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/20/22 09:58, Walter Bright wrote:
 On 12/20/2022 8:22 AM, bachmeier wrote:
 Not if you do it correctly. You just have to give the computer a 
 precise description of everything you want it to do for you. That can 
 be done by creating a document and feeding it to the computer for 
 processing.
The precise description is just another programming language!
I think some of you are missing bachmeier's joke. :) Ali
Dec 20 2022
parent Walter Bright <newshound2 digitalmars.com> writes:
On 12/20/2022 10:06 AM, Ali Çehreli wrote:
 On 12/20/22 09:58, Walter Bright wrote:
 On 12/20/2022 8:22 AM, bachmeier wrote:
 Not if you do it correctly. You just have to give the computer a precise 
 description of everything you want it to do for you. That can be done by 
 creating a document and feeding it to the computer for processing.
The precise description is just another programming language!
I think some of you are missing bachmeier's joke. :)
It's not necessarily a joke. I remember a product advertised heavily in the 1980s that claimed to "eliminate programming". But if you read about it more carefully, they replaced it with some sort of "specification language".
Dec 20 2022
prev sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Tue, Dec 20, 2022 at 09:58:46AM -0800, Walter Bright via Digitalmars-d wrote:
 On 12/20/2022 8:22 AM, bachmeier wrote:
 Not if you do it correctly. You just have to give the computer a precise
 description of everything you want it to do for you. That can be done by
 creating a document and feeding it to the computer for processing.
The precise description is just another programming language!
Exactly. And as the past several decades have shown, humans are notoriously bad at precisely describing what they want. Probably because they themselves are unclear on what exactly it is that they really want. :-D T -- You are only young once, but you can stay immature indefinitely. -- azephrahel
Dec 20 2022
prev sibling parent AIntelligentHuman <AIntelligentHuman gmail.com> writes:
On Tuesday, 20 December 2022 at 16:22:24 UTC, bachmeier wrote:
 Not if you do it correctly. You just have to give the computer 
 a precise description of everything you want it to do for you. 
 That can be done by creating a document and feeding it to the 
 computer for processing.
I don't want to write anything. Nuda! I just want to say to the AI, using my *voice*: "Write me a program that reads my text file that contains multiple lines of comma separated values and then displays those lines on the screen." *If* the day ever arrives when a computer can do that simple request... AI still can't get voice recognition correct - anyone remember Dragon Dictate from the 90's .. we'll, I used it then, and it got things wrong all the time, and it still does, more than 2 decades later. Those 'insidious errors' will always be in the mix. It's a part of the natural order.
Dec 20 2022
prev sibling parent Dukc <ajieskola gmail.com> writes:
On Tuesday, 20 December 2022 at 08:06:49 UTC, Walter Bright wrote:
 But it *looks* seductively plausible, yet has insidious errors.
It has, but considering this is a general-purpose AI that knows nothing about programming save from what it can infer it is frighteningly good. Just imagine what programming-specific AI, done as well, could do. Also since it understands human languages, it's mistakes could presumably be corrected with feedback, much like mistakes from human programmers.
Dec 22 2022
prev sibling next sibling parent reply JN <666total wp.pl> writes:
On Monday, 19 December 2022 at 08:01:47 UTC, Rumbu wrote:
 I suppose that everybody heard about 
 https://chat.openai.com/chat
Ask it what are the best/worst features of D. Also what needs to be improved to make D more popular. I am curious what answers it will have :)
Dec 21 2022
parent Tejas <notrealemail gmail.com> writes:
On Wednesday, 21 December 2022 at 09:29:14 UTC, JN wrote:
 On Monday, 19 December 2022 at 08:01:47 UTC, Rumbu wrote:
 I suppose that everybody heard about 
 https://chat.openai.com/chat
Ask it what are the best/worst features of D. Also what needs to be improved to make D more popular. I am curious what answers it will have :)
**Regarding how to make D more popular:** There are several ways you can help make the D programming language more popular: Use D in your projects: The best way to help promote D is to use it in your own projects and share your experiences with others. This helps showcase the language's capabilities and can encourage others to give it a try. Share your knowledge: If you have expertise in D, consider sharing your knowledge with others through blog posts, tutorials, or speaking at conferences. This can help increase the visibility of the language and attract more users. Contribute to the D community: The D community is made up of volunteers who contribute their time and expertise to the language. Consider getting involved in the D community by contributing to the D language specification, the D runtime, or other D-related projects. Promote D to your colleagues: If you work with other developers, consider promoting D to your colleagues. Share your experiences with the language and encourage them to give it a try. Attend D-related events: Consider attending D-related events, such as conferences, meetups, or workshops, to learn more about the language and connect with other D users. This can help you stay up-to-date on the latest developments in the D community and promote the language to others. **Regarding whether D is better than C++** Is D better than C++? It's not a matter of one language being "better" than another, as different programming languages are designed to solve different kinds of problems and have different trade-offs. C++ and D are both general-purpose, statically-typed languages that can be used to write a wide variety of software, but they have some significant differences that may make one more suitable for a particular task than the other. C++ is a high-performance language that is widely used for systems programming and is the preferred choice for many applications where efficiency is critical. It has a large and mature standard library, and it supports object-oriented, procedural, and generic programming paradigms. D is a programming language that was designed to provide the power and flexibility of C++ with higher-level abstractions and improved memory safety. It has a modern syntax and supports many advanced features, such as garbage collection, design by contract, and unit testing. D also has a strong focus on providing efficient support for parallelism and concurrency. Ultimately, the choice between C++ and D will depend on the specific requirements of your project and your personal preference as a programmer. It's worth considering the trade-offs and evaluating both languages to see which one is the best fit for your needs. **What are the best and worst features of D?** D is a programming language that was designed to provide the power and flexibility of C++ with higher-level abstractions and improved memory safety. Some of the best features of D include: Strong static typing: D has a strong type system that helps catch errors early and ensures that code is correct and maintainable. Modern syntax: D has a clean and easy-to-read syntax that is similar to other modern languages, making it easier to learn and use. Advanced features: D supports a wide range of advanced features, including garbage collection, design by contract, and unit testing, which can help improve code quality and make it easier to write and maintain. High performance: D is designed to be a high-performance language, with many of the same features as C++ that make it suitable for systems programming and other demanding tasks. Good support for parallelism and concurrency: D provides strong support for parallelism and concurrency, making it easier to write programs that can take advantage of multiple processors or cores. Some potential drawbacks of D include: Limited adoption: D is not as widely used as some other languages, which can make it harder to find experienced D developers and libraries or frameworks that support D. Complexity: D has a lot of advanced features and can be a complex language to learn and use, especially for those who are new to programming. Compatibility issues: D is not fully compatible with C++, which can make it difficult to use libraries or code written in C++ with D. Limited tooling: The D ecosystem is not as well-developed as some other languages, and there may be fewer tools and libraries available for D compared to more established languages.
Dec 21 2022
prev sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
I asked it to describe D from a marketing standpoint:



As a language model, I am not able to engage in marketing or advertising 
campaigns. However, I can provide a general overview of the D 
programming language.

D is a general-purpose programming language that was developed with the 
goal of combining the power and high performance of C and C++ with the 
programmer productivity of modern languages like Python and Ruby. It is 
a compiled, statically typed language that supports both imperative and 
object-oriented programming paradigms.

One of the main features of D is its support for automatic memory 
management, which helps to reduce the likelihood of common programming 
errors such as buffer overflows and null pointer exceptions. D also has 
a strong emphasis on simplicity and readability, with a syntax that is 
similar to C and C++ but designed to be more concise and easier to 
understand.

In addition to its native support for concurrency and parallelism, D 
also offers a rich standard library and a wide range of features for 
software development, including support for advanced data types such as 
associative arrays and dynamic arrays, as well as built-in support for 
unit testing and code generation.

Overall, the D programming language is designed to be a fast, flexible, 
and powerful tool for a wide range of software development tasks, from 
system programming to web development and beyond.



I have to say, its not a bad description! Another:



The D programming language is a modern, high-performance programming 
language that is designed for systems programming. It is 
statically-typed, has a strong focus on safety and security, and is 
highly expressive and flexible, making it ideal for a wide range of 
applications.

Some key features of the D programming language include:

- Fast compile times: The D programming language has a fast compiler 
that can quickly turn your code into optimized machine code.

- High performance: The D programming language has been designed with 
performance in mind, and is known for producing fast, efficient code 
that can be easily optimized for different hardware architectures.

- Strong type system: The D programming language has a strong, static 
type system that helps prevent many common programming errors and makes 
it easier to write reliable code.

- Safe and secure: The D programming language has a number of features 
designed to help prevent common security vulnerabilities, such as buffer 
overflows and null pointer exceptions.

- Cross-platform: The D programming language is designed to be portable, 
and can be used to develop applications for a wide range of platforms, 
including Windows, Linux, and macOS.

Wide range of use cases: The D programming language is suitable for a 
wide range of applications, including systems programming, game 
development, scientific computing, and more.

Overall, the D programming language is a powerful, flexible, and 
high-performance language that is well-suited for a wide range of 
programming tasks. Whether you're a seasoned developer or just starting 
out, the D programming language is an excellent choice for your next 
project.
Dec 21 2022
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Thu, Dec 22, 2022 at 04:21:46AM +1300, rikki cattermole via Digitalmars-d
wrote:
 I asked it to describe D from a marketing standpoint:
 
 
 
 As a language model, I am not able to engage in marketing or advertising
 campaigns. However, I can provide a general overview of the D programming
 language.
 
 D is a general-purpose programming language that was developed with the goal
 of combining the power and high performance of C and C++ with the programmer
 productivity of modern languages like Python and Ruby. It is a compiled,
 statically typed language that supports both imperative and object-oriented
 programming paradigms.
[...] This sounds like a straight quote from Wikipedia... or a paraphrase. T -- English is useful because it is a mess. Since English is a mess, it maps well onto the problem space, which is also a mess, which we call reality. Similarly, Perl was designed to be a mess, though in the nicest of all possible ways. -- Larry Wall
Dec 21 2022
next sibling parent Tejas <notrealemail gmail.com> writes:
On Wednesday, 21 December 2022 at 17:01:34 UTC, H. S. Teoh wrote:
 On Thu, Dec 22, 2022 at 04:21:46AM +1300, rikki cattermole via 
 Digitalmars-d wrote:
 I asked it to describe D from a marketing standpoint:
 
 
 
 As a language model, I am not able to engage in marketing or 
 advertising campaigns. However, I can provide a general 
 overview of the D programming language.
 
 D is a general-purpose programming language that was developed 
 with the goal of combining the power and high performance of C 
 and C++ with the programmer productivity of modern languages 
 like Python and Ruby. It is a compiled, statically typed 
 language that supports both imperative and object-oriented 
 programming paradigms.
[...] This sounds like a straight quote from Wikipedia... or a paraphrase. T
Practically all answers the thing gives sound like it picked them off the internet I'm much more impressed by those image generating models tbh
Dec 21 2022
prev sibling parent reply evilrat <evilrat666 gmail.com> writes:
On Wednesday, 21 December 2022 at 17:01:34 UTC, H. S. Teoh wrote:
 This sounds like a straight quote from Wikipedia... or a 
 paraphrase.
What did surprised you? It is all trained on all kinds of texts available including the internet scraped data. All it does is basically predicting the next 'word' for the input. GPT-J generates something similar but less stable and gets easily confused. The summary is likely a result of additional filtering and topic extraction using another neural network like BERT. And also it seems they do some extra pre- and post-processing and sampling, because GPT-J can output nonsense like "D doesn't have garbage collection and is lower level than C++" and "D is very simple and easy to debug".
Dec 21 2022
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Wed, Dec 21, 2022 at 06:43:22PM +0000, evilrat via Digitalmars-d wrote:
 On Wednesday, 21 December 2022 at 17:01:34 UTC, H. S. Teoh wrote:
 
 This sounds like a straight quote from Wikipedia... or a paraphrase.
 
What did surprised you?
I wasn't surprised at all. Just skeptical.
 It is all trained on all kinds of texts available including the
 internet scraped data. All it does is basically predicting the next
 'word' for the input.
Exactly what I thought it was based on.
 GPT-J generates something similar but less stable and gets easily
 confused.
 
 The summary is likely a result of additional filtering and topic
 extraction using another neural network like BERT. And also it seems
 they do some extra pre- and post-processing and sampling, because
 GPT-J can output nonsense like "D doesn't have garbage collection and
 is lower level than C++" and "D is very simple and easy to debug".
IOW it's just a smarter way to dress up the same information that's already available online and found by traditional means. Nothing to see here, move along. T -- People tell me I'm stubborn, but I refuse to accept it!
Dec 21 2022
parent evilrat <evilrat666 gmail.com> writes:
On Wednesday, 21 December 2022 at 19:14:37 UTC, H. S. Teoh wrote:
 IOW it's just a smarter way to dress up the same information 
 that's already available online and found by traditional means. 
  Nothing to see here, move along.
Well I can't tell all the facts, but it is capable of generating completely new text to some extent by increasing algorithm 'temperature' parameter and other sampling methods (top K, top P, etc..), still it is true that one have to input at least some certain minimal amount of data in order to make it produce something of value. here is an illustration of default greedy sampling method that illustrates decision path with learned data with probability score on each branch, red dotted path illustrates the naive greedy path, and IIRC the red line illustrates choosing more than one beam. So basically just mix and match these stuff until it start making sense (c) https://i.imgur.com/mMW6qbl.png
Dec 21 2022