www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to find the right function in the Phobos library?

reply Bruce <bruce technisolve.co.za> writes:
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?
Aug 16
next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
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
Aug 16
parent reply Bruce <bruce technisolve.co.za> writes:
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");
Aug 16
parent monkyyy <crazymonkyyy gmail.com> writes:
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 ```d import std; void main(){ string arr="🦶🔫 hello world ha"; arr[arr.countUntil("ha")..$].writeln; } ```
Aug 16
prev sibling next sibling parent Ron Tarrant <rontarrant gmail.com> writes:
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."); } } ```
Aug 16
prev sibling next sibling parent Renato Athaydes <renato athaydes.com> writes:
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.
Aug 17
prev sibling next sibling parent Renato Athaydes <renato athaydes.com> writes:
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.
Aug 17
prev sibling next sibling parent Lance Bachmeier <no spam.net> writes:
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.
Aug 17
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Aug 17
next sibling parent reply Vinod K Chandran <kcvinu82 gmail.com> writes:
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
Aug 17
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 18/08/2024 7:49 AM, Vinod K Chandran wrote:
 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
https://dlang.org/phobos/core_thread_osthread.html#.Thread.sleep Worth noting is that sleeping is not equivalent to performing work. It's the exact opposite, the lack of work. If you want to simulate work being done, use a loop with a stop watch to determine how much time has progressed.
Aug 17
parent Vinod K Chandran <kcvinu82 gmail.com> writes:
On Saturday, 17 August 2024 at 19:54:07 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 https://dlang.org/phobos/core_thread_osthread.html#.Thread.sleep

 Worth noting is that sleeping is not equivalent to performing 
 work. It's the exact opposite, the lack of work.
Thanks Rikki. Actually, I was checked `core.thread` page. But at the top, after a speedy search, unfprtunately, I couldn't find anything. Now, I can see that beneath first second `Jump to` section, there is a `sleep`.
 If you want to simulate work being done, use a loop with a stop 
 watch to determine how much time has progressed.
Point noted. Thanks!
Aug 18
prev sibling parent reply IchorDev <zxinsworld gmail.com> writes:
On Saturday, 17 August 2024 at 19:49:04 UTC, Vinod K Chandran 
wrote:
 All I want is a delay function which simulate some work load in 
 current thread.
As Rikki points out, delay or sleep functions (as the name says) ‘put the thread to sleep’. If you want the thread to be constantly busy, you can use a while loop: ```d import core.time; //waste electricity for 1ms: const endTime = MonoTime.currTime + 1.msecs; while(MonoTime.currTime < endTime){} ``` Note that it’s better to let the thread rest because otherwise the OS might be starved for threads, or the CPU will consume more electricity and could overheat unnecessarily, hindering your program’s performance.
Aug 18
parent Vinod K Chandran <kcvinu82 gmail.com> writes:
On Sunday, 18 August 2024 at 08:39:49 UTC, IchorDev wrote:
 As Rikki points out, delay or sleep functions (as the name 
 says) ‘put the thread to sleep’. If you want the thread to be 
 constantly busy, you can use a while loop:
 ```d
 import core.time;

 //waste electricity for 1ms:
 const endTime = MonoTime.currTime + 1.msecs;
 while(MonoTime.currTime < endTime){}
 ```
 Note that it’s better to let the thread rest because otherwise 
 the OS might be starved for threads, or the CPU will consume 
 more electricity and could overheat unnecessarily, hindering 
 your program’s performance.
Thank you IchorDev for the sample code. Got the idea.
Aug 18
prev sibling next sibling parent reply Ron Tarrant <rontarrant gmail.com> writes:
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.
Good trick, Steve. I've been working/playing with D for almost six years and I didn't know that. Thank you.
Aug 18
parent Renato Athaydes <renato athaydes.com> writes:
On Monday, 19 August 2024 at 06:53:34 UTC, Ron Tarrant wrote:
 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.
Good trick, Steve. I've been working/playing with D for almost six years and I didn't know that. Thank you.
OMG why is that not the default in that search box??? It's so much better with interactive search!
Aug 19
prev sibling parent reply Andy Valencia <dont spam.me> writes:
On Saturday, 17 August 2024 at 17:31:53 UTC, Steven Schveighoffer 
wrote:
 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.
I'm doing some network programming, and have run things down with casts and all to the point where I have an IPv4 address. The documentation says it's in "host order", so obviously a 32-bit number. My C days tell me htonl is what's needed--but it's nowhere to be found in the API index? I did a search and it's apparently under core/sys, but "sys" isn't included in the online documentation? Doing bulk searches in /usr/lib/ldc/x86_64-linux-gnu/include/d/core/sys lets me run it down to three places, of which I'd guess posix/arpa/inet.d is the one to use. But this all seems a little bit harder than it might be? A map of C or Python API's to the Dlang counterpart might be the easiest way to let people find things.
Aug 20
parent reply IchorDev <zxinsworld gmail.com> writes:
On Tuesday, 20 August 2024 at 21:53:10 UTC, Andy Valencia wrote:
 On Saturday, 17 August 2024 at 17:31:53 UTC, Steven 
 Schveighoffer wrote:
 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.
I'm doing some network programming, and have run things down with casts and all to the point where I have an IPv4 address. The documentation says it's in "host order", so obviously a 32-bit number. My C days tell me htonl is what's needed--but it's nowhere to be found in the API index? I did a search and it's apparently under core/sys, but "sys" isn't included in the online documentation? Doing bulk searches in /usr/lib/ldc/x86_64-linux-gnu/include/d/core/sys lets me run it down to three places, of which I'd guess posix/arpa/inet.d is the one to use. But this all seems a little bit harder than it might be? A map of C or Python API's to the Dlang counterpart might be the easiest way to let people find things.
`core.sys` is OS-specific API bindings. They have no documentation, and I can imagine that the sheer amount of `version` statements in them would break the documentation generation. You should’ve probably considered using the equivalent function from Phobos because it’s a D function so it can be inlined and such: https://dlang.org/library/std/bitmanip/native_to_big_endian.html
Aug 21
parent reply Andy Valencia <dont spam.me> writes:
On Wednesday, 21 August 2024 at 20:45:10 UTC, IchorDev wrote:

 You should’ve probably considered using the equivalent function 
 from Phobos because it’s a D function so it can be inlined and 
 such:
 https://dlang.org/library/std/bitmanip/native_to_big_endian.html
Brilliant, that API gives me exactly what I'd want. Thank you. Andy
Aug 21
parent IchorDev <zxinsworld gmail.com> writes:
On Wednesday, 21 August 2024 at 21:22:44 UTC, Andy Valencia wrote:
 On Wednesday, 21 August 2024 at 20:45:10 UTC, IchorDev wrote:

 You should’ve probably considered using the equivalent 
 function from Phobos because it’s a D function so it can be 
 inlined and such:
 https://dlang.org/library/std/bitmanip/native_to_big_endian.html
Brilliant, that API gives me exactly what I'd want. Thank you. Andy
No problem! :)
Aug 21