Jump to page: 1 2
Thread overview
How to find the right function in the Phobos library?
Aug 17
Bruce
Aug 17
monkyyy
Aug 17
Bruce
Aug 17
monkyyy
Aug 18
IchorDev
Aug 21
IchorDev
Aug 21
IchorDev
August 17

Example.
I want to find the index of an item in an array.

After a google search and a browse of the Phobos
library, no luck!

Surely D must be an indexOf function that works on
arrays?

Something like...
string[] arr = ["pa", "db", "wb", "ha", "wa"];
int i = arr.indexOf("ha");

I eventually tried ChatGPT and abandoned it after
a number of failed attempts.

So I wrote my own function ...

size_t indexOf(string[] arr, string key) {
  foreach (size_t i, val; arr)
    if (val == key) return i;
  return -1;
}

But, this is not satisfying. The Phobos library should
have this function.

I asked ChatGPT again with my example indexOf and it
came up with countUntil.

auto i = arr.countUntil("ha");

This seems to work but now I'm concerned.
Why was it so hard to find?
What is the best way to search for a function
in the Phobos library?

August 17

On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote:

>

This seems to work but now I'm concerned.
Why was it so hard to find?
What is the best way to search for a function
in the Phobos library?

It's the correct name for the wrong function

If you use it on unicode strings or filters it's just wrong, ranges do not contain index infomation

August 17

On Saturday, 17 August 2024 at 05:42:42 UTC, monkyyy wrote:

>

On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote:

It's the correct name for the wrong function

Are you saying this is wrong?
auto i = arr.countUntil("ha");

August 17

On Saturday, 17 August 2024 at 06:11:58 UTC, Bruce wrote:

>

On Saturday, 17 August 2024 at 05:42:42 UTC, monkyyy wrote:

>

On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote:

It's the correct name for the wrong function

Are you saying this is wrong?
auto i = arr.countUntil("ha");

yes

import std;
void main(){
	string arr="🦶🔫 hello world ha";
    arr[arr.countUntil("ha")..$].writeln;
}
August 17

On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote:

>

Example.
I want to find the index of an item in an array.

import std.stdio;
import std.string;

void main()
{
   string text = "Hello, World!";
   string searchSubstring = "World";

   ptrdiff_t index = text.indexOf(searchSubstring);

   if (index != -1)
   {
      writeln("The substring '", searchSubstring, "' found at index ", index);
   }
   else
   {
      writeln("The substring '", searchSubstring, "' not found in the string.");
   }
}
August 17

On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote:

>

Example.
I want to find the index of an item in an array.

After a google search and a browse of the Phobos
library, no luck!

Surely D must be an indexOf function that works on
arrays?

Something like...
string[] arr = ["pa", "db", "wb", "ha", "wa"];
int i = arr.indexOf("ha");

I eventually tried ChatGPT and abandoned it after
a number of failed attempts.

So I wrote my own function ...

size_t indexOf(string[] arr, string key) {
  foreach (size_t i, val; arr)
    if (val == key) return i;
  return -1;
}

But, this is not satisfying. The Phobos library should
have this function.

I asked ChatGPT again with my example indexOf and it
came up with countUntil.

auto i = arr.countUntil("ha");

This seems to work but now I'm concerned.
Why was it so hard to find?
What is the best way to search for a function
in the Phobos library?

In general, you just need to know the modules available in Phobos, and then you go to the relevant one in the docs and see if you can find a function that looks like what you need. In this case, you would have to know that std.string is where string-related functions are, and you would find indexOf there: https://dlang.org/phobos/std_string.html#.indexOf

That means that to save time, you should try to familiarize yourself with how Phobos is divided into modules, and what each module provides. But as someone familiar with several other languages stdlibs but not with Phobos, I've found it pretty easy to just google "what's the equivalent of 'xxx' in Dlang" (D does have a few unusual naming choices) and that normally gives me what I need.

In Haskell and Unison, one can search for functions with certain types, which is really cool. In D that may not work as well because so many functions are templated so the types are not concrete though.

August 17

On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote:

>

Surely D must be an indexOf function that works on
arrays?

Something like...
string[] arr = ["pa", "db", "wb", "ha", "wa"];
int i = arr.indexOf("ha");

But, this is not satisfying. The Phobos library should
have this function.

I've noticed that you are looking for items in an array, not substrings in a string.

For that you can use countUntil:

import std.stdio: writeln;
import std.algorithm.searching : countUntil;

void main()
{
   const words = ["hello", "world", "bye"];

   const idx = words.countUntil("world");

   if (idx == -1)
   {
      writeln("Could not find target word");
   }
   else
   {
      writeln("Found word at index ", idx);
   }
}

But notice that working in D, you probably should take advantage of slices, as they're so powerful... indexOf and countUntil are normally used to find a sub-sequence you're interested in, which in other languages requires you to manually do based on the indexes... but perhaps in D you could use the very appropriately named find function to just obtain the relevant slice directly:

import std.stdio: writeln;
import std.algorithm.searching : find;

void main()
{
   const words = ["hello", "world", "bye"];

   const subSeq = words.find("world");

   if (subSeq.length > 0)
   {
      writeln("Found it! ", subSeq);
   }
   else
   {
      writeln("Cannot find it!");
   }
}

Notice that find returns a slice, but this is very cheap in D as it's just a length and a pointer to the original slice's array, it won't copy the array if you don't modify anything.

August 17

On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote:

>

This seems to work but now I'm concerned.
Why was it so hard to find?
What is the best way to search for a function
in the Phobos library?

Browsing the Phobos index will often do the trick, as will searching in the forum using the box in the upper right (D is in a good position because most questions have been asked on this mailing list since the beginning).

But you should not be afraid to post questions here, as you have done, if searching the forum doesn't give you something. It's why the Learn forum exists.

August 17

On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote:

>

What is the best way to search for a function
in the Phobos library?

Go to dlang.org, select dicumentation, then library reference.

Pick any module, click on it

In the upper right, switch the docs from stable to ddox

Now you can use the search bar and it is interactive. Typing in indexOf found it right away.

-Steve

August 17

On Saturday, 17 August 2024 at 17:31:53 UTC, Steven Schveighoffer wrote:

>

Go to dlang.org, select dicumentation, then library reference.

Pick any module, click on it

In the upper right, switch the docs from stable to ddox

Now you can use the search bar and it is interactive. Typing in indexOf found it right away.

Steve, We need a better search system. For example, I needed something like the thread.sleep() in .net today. All I want is a delay function which simulate some work load in current thread. But sadly, it is difficult to find.
-kcvinker

« First   ‹ Prev
1 2