digitalmars.D.learn - How to find the right function in the Phobos library?
- Bruce (26/26) Aug 16 Example.
- monkyyy (4/9) Aug 16 It's the correct name for the wrong function
- Ron Tarrant (21/23) Aug 16 ```
- Renato Athaydes (18/44) Aug 17 In general, you just need to know the modules available in
- Renato Athaydes (47/54) Aug 17 I've noticed that you are looking for items in an array, not
- Lance Bachmeier (8/12) Aug 17 Browsing the Phobos index will often do the trick, as will
- Steven Schveighoffer (7/9) Aug 17 Go to dlang.org, select dicumentation, then library reference.
- Vinod K Chandran (7/12) Aug 17 Steve, We need a better search system. For example, I needed
- Richard (Rikki) Andrew Cattermole (6/23) Aug 17 https://dlang.org/phobos/core_thread_osthread.html#.Thread.sleep
- Vinod K Chandran (7/12) Aug 18 Thanks Rikki. Actually, I was checked `core.thread` page. But at
- IchorDev (15/17) Aug 18 As Rikki points out, delay or sleep functions (as the name says)
- Vinod K Chandran (2/15) Aug 18 Thank you IchorDev for the sample code. Got the idea.
- Ron Tarrant (4/9) Aug 18 Good trick, Steve. I've been working/playing with D for almost
- Renato Athaydes (3/15) Aug 19 OMG why is that not the default in that search box??? It's so
- Andy Valencia (16/24) Aug 20 I'm doing some network programming, and have run things down with
- IchorDev (9/38) Aug 21 `core.sys` is OS-specific API bindings. They have no
- Andy Valencia (3/7) Aug 21 Brilliant, that API gives me exactly what I'd want. Thank you.
- IchorDev (2/9) Aug 21 No problem! :)
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
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
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 functionAre you saying this is wrong? auto i = arr.countUntil("ha");
Aug 16
On Saturday, 17 August 2024 at 06:11:58 UTC, Bruce wrote:On Saturday, 17 August 2024 at 05:42:42 UTC, monkyyy wrote:yes ```d import std; void main(){ string arr="🦶🔫 hello world ha"; arr[arr.countUntil("ha")..$].writeln; } ```On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote: It's the correct name for the wrong functionAre you saying this is wrong? auto i = arr.countUntil("ha");
Aug 16
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
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
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
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
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
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
On 18/08/2024 7:49 AM, Vinod K Chandran wrote:On Saturday, 17 August 2024 at 17:31:53 UTC, Steven Schveighoffer 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. If you want to simulate work being done, use a loop with a stop watch to determine how much time has progressed.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
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
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
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
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
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:OMG why is that not the default in that search box??? It's so much better with interactive search!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 19
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: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.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.
Aug 20
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:`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.htmlOn Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote: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.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.
Aug 21
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.htmlBrilliant, that API gives me exactly what I'd want. Thank you. Andy
Aug 21
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:No problem! :)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.htmlBrilliant, that API gives me exactly what I'd want. Thank you. Andy
Aug 21