Search
Fun with OpenAI
Dec 19, 2022
Rumbu
Dec 19, 2022
Dec 19, 2022
Walter Bright
Dec 20, 2022
RTM
Dec 19, 2022
thebluepandabear
Dec 19, 2022
Bastiaan Veelo
Dec 19, 2022
evilrat
Dec 20, 2022
Walter Bright
Dec 20, 2022
Stefan Koch
Dec 20, 2022
bachmeier
Dec 20, 2022
Dom Disc
Dec 20, 2022
Walter Bright
Dec 20, 2022
Ali Çehreli
Dec 20, 2022
Walter Bright
Dec 20, 2022
H. S. Teoh
Dec 21, 2022
AIntelligentHuman
Dec 22, 2022
Dukc
Dec 21, 2022
JN
Dec 21, 2022
Tejas
Dec 21, 2022
rikki cattermole
Dec 21, 2022
H. S. Teoh
Dec 21, 2022
Tejas
Dec 21, 2022
evilrat
Dec 21, 2022
H. S. Teoh
Dec 21, 2022
evilrat

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

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:

``````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.

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.

```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.
```

Everything is good except for the 'for' loop.

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.

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(','));
}
}
}
``````

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

```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.

```
```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 ;)
```

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.

« First   ‹ Prev
1 2 3