Re: Problem with std.algorithm.iteration::substitute
You can also turn your function into a fold. auto searches = ["1", "2"]; writeln (searches.fold!((a, b) => a.substitute(b, "number").to!string)("come 1 come 2")) ;
Re: Problem with std.algorithm.iteration::substitute
On Saturday, 11 January 2020 at 17:10:02 UTC, Martin Brezl wrote: Hi, i have a function like this: ``` import std.algorithm.iteration : substitute; //... string replace(string content, string[] searches , string replace) { if(searches.empty) return content; auto result = content.substitute(searches.front,replace); for(size_t i=1; i < searches.length; i++) { searches.popFront(); result = result.substitute(searches.front,replace); } //... return "example return"; } ``` The issue is the double assigned result. auto result = content.substitute(searches.front,replace); result = result.substitute(searches.front,replace); `substitute` is going to return a template range typed off `content` then it uses that type to perform another substitution. `content` and `result` don't have the same type.
Re: Problem with std.algorithm.iteration::substitute
On Saturday, 11 January 2020 at 17:10:02 UTC, Martin Brezl wrote: Hi, i have a function like this: ``` [...] auto result = content.substitute(searches.front,replace); for(size_t i=1; i < searches.length; i++) { searches.popFront(); result = result.substitute(searches.front,replace); } [...] `result = result.to!string.substitute(searches.front,replace);` Convertring result to string solves the problem - but i do not understand why the Example "Multiple substitutes" from the docs is working.
Problem with std.algorithm.iteration::substitute
Hi, i have a function like this: ``` import std.algorithm.iteration : substitute; //... string replace(string content, string[] searches , string replace) { if(searches.empty) return content; auto result = content.substitute(searches.front,replace); for(size_t i=1; i < searches.length; i++) { searches.popFront(); result = result.substitute(searches.front,replace); } //... return "example return"; } ``` I do not understand why this is not valid. This is the compilation Error: ``` /home/mab/dlang/dmd-2.089.1/linux/bin64/../../src/phobos/std/algorithm/iteration.d(5954,48): Error: template std.algorithm.searching.find cannot deduce function from argument types !(__lambda196)(Result, string), candidates are: /home/mab/dlang/dmd-2.089.1/linux/bin64/../../src/phobos/std/algorithm/searching.d(1557,12): find(alias pred = "a == b", InputRange, Element)(InputRange haystack, scope Element needle) with pred = __lambda196, InputRange = Result, Element = string must satisfy the following constraint: is(typeof(binaryFun!pred(haystack.front, needle)) : bool) /home/mab/dlang/dmd-2.089.1/linux/bin64/../../src/phobos/std/algorithm/searching.d(1823,12): find(alias pred, InputRange)(InputRange haystack) /home/mab/dlang/dmd-2.089.1/linux/bin64/../../src/phobos/std/algorithm/searching.d(1877,4): find(alias pred = "a == b", R1, R2)(R1 haystack, scope R2 needle) with pred = __lambda196, R1 = Result, R2 = string must satisfy the following constraint: isForwardRange!R1 /home/mab/dlang/dmd-2.089.1/linux/bin64/../../src/phobos/std/algorithm/searching.d(2329,23): find(alias pred = "a == b", Range, Ranges...)(Range haystack, Ranges needles) with pred = __lambda196, Range = Result, Ranges = (string) must satisfy the following constraint: Ranges.length > 1 /home/mab/dlang/dmd-2.089.1/linux/bin64/../../src/phobos/std/algorithm/searching.d(2444,19): find(RandomAccessRange, alias pred, InputRange)(RandomAccessRange haystack, scope BoyerMooreFinder!(pred, InputRange) needle) source/app.d(44,29): Error: template instance std.algorithm.iteration.substitute!((a, b) => a == b, Result, string, string) error instantiating source/app.d(44,29): Error: cannot implicitly convert expression substitute(result, front(searches), replace) of type std.algorithm.iteration.joiner!(MapResult!(__lambda7, SubstituteSplitter)).joiner.Result to std.algorithm.iteration.joiner!(MapResult!(__lambda7, SubstituteSplitter)).joiner.Result /home/mab/dlang/dmd-2.089.1/linux/bin64/dmd failed with exit code 1. ``` Can someone explain what is going on? Specially this part: "Error: cannot implicitly convert expression substitute(result, front(searches), replace) of type std.algorithm.iteration.joiner!(MapResult!(__lambda7, SubstituteSplitter)).joiner.Result to std.algorithm.iteration.joiner!(MapResult!(__lambda7, SubstituteSplitter)).joiner.Result" In the Example "Multiple substitutes" in https://dlang.org/phobos/std_algorithm_iteration.html#.substitute.substitute i understand that a call to substitute() can be made on the result of substitute(). Thanks BTW: can sourcecode be formated in this forum somehow?
Compilation error: undefined reference to 'cblas_dgemv' / 'cblas_dger' / 'cblas_dgemm'
I am trying to run example code from https://tour.dlang.org/tour/en/dub/lubeck example.d: --- /+dub.sdl: dependency "lubeck" version="~>1.1" +/ import lubeck: mtimes; import mir.algorithm.iteration: each; import mir.ndslice; import std.stdio: writeln; void main() { auto n = 5; // Magic Square auto matrix = n.magic.as!double.slice; // [1 1 1 1 1] auto vec = 1.repeat(n).as!double.slice; // Uses CBLAS for multiplication matrix.mtimes(vec).writeln; "-".writeln; matrix.mtimes(matrix).byDim!0.each!writeln; } --- I try to compile it via: --- dub build --compiler="ldc" --single example.d -v --- And get the error below: --- Linking... ldc -of.dub/build/application-debug-linux.posix-x86_64-ldc_2088-588551E77C1C779CBF3BECA58D19211B/matrix_dot .dub/build/application-debug-linux.posix-x86_64-ldc_2088-588551E77C1C779CBF3BECA58D19211B/matrix_dot.o ../../../../../.dub/packages/lubeck-1.1.7/lubeck/.dub/build/library-debug-linux.posix-x86_64-ldc_2088-09723F6E7A90ABDEB9EDD35B9DC7E7CE/liblubeck.a ../../../../../.dub/packages/mir-lapack-1.2.1/mir-lapack/.dub/build/library-debug-linux.posix-x86_64-ldc_2088-69D3CA650230A9C73F912A6D1AB44EE1/libmir-lapack.a ../../../../../.dub/packages/mir-blas-1.1.9/mir-blas/.dub/build/library-debug-linux.posix-x86_64-ldc_2088-9B27CD5D2C27A78F627CDC352C376E52/libmir-blas.a ../../../../../.dub/packages/mir-algorithm-3.7.13/mir-algorithm/.dub/build/default-debug-linux.posix-x86_64-ldc_2088-255337055B86988DA608A6F2BE06058A/libmir-algorithm.a ../../../../../.dub/packages/mir-core-1.0.2/mir-core/.dub/build/library-debug-linux.posix-x86_64-ldc_2088-554F8271B5559801959D6785138A5389/libmir-core.a -! L--no-as-needed -L-lopenblas -g /home/tastyminerals/dev/github/mir_quickstart/source/benchmarks/../../../../../.dub/packages/mir-blas-1.1.9/mir-blas/source/mir/blas.d:305: error: undefined reference to 'cblas_dgemv' /home/tastyminerals/dev/github/mir_quickstart/source/benchmarks/../../../../../.dub/packages/mir-blas-1.1.9/mir-blas/source/mir/blas.d:210: error: undefined reference to 'cblas_dger' /home/tastyminerals/dev/github/mir_quickstart/source/benchmarks/../../../../../.dub/packages/mir-blas-1.1.9/mir-blas/source/mir/blas.d:385: error: undefined reference to 'cblas_dgemm' collect2: error: ld returned 1 exit status Error: /usr/bin/gcc failed with status: 1 --- This is Linux Manjaro with openblas package installed.
Re: Win32 Api: How create Open/"Save as" Dialog?
On Saturday, 11 January 2020 at 12:22:25 UTC, Marcone wrote: wchar[1024] szFileName = 0; ofn.lpstrFile = cast(LPWSTR) szFileName; You shouldn't cast there, just use `szFileName.ptr` instead. ofn.nMaxFile = MAX_PATH; and this should be the length of the array. It may require a case nMaxFile = cast(DWORD) szFileName.length;
Re: How to parse epub content
On Saturday, 11 January 2020 at 12:38:38 UTC, Adnan wrote: How would someone approach parsing epub files in D? Is there any libraries to parse XHTML? I've done it before with my dom.d easily enough. The epub itself is a zip file. You might simply unzip it ahead of time, or use std.zip to access the contents easily enough. (basic zip file support is in phobos). Then once you get inside there's xhtml files which again are easy enough to parse. Like with my dom.d it is as simple as like import arsd.dom; // the true,true here tells it to use strict xml mode for xhtml // isn't really necessary though so it is ok auto document = new Document(string_holding_xml, true, true); foreach(ele; document.querySelectorAll("p")) writeln(ele.innerText); the api there is similar to javascript if you're familiar with that.
Re: How to parse epub content
On Saturday, 11 January 2020 at 12:38:38 UTC, Adnan wrote: How would someone approach parsing epub files in D? Is there any libraries to parse XHTML? XHTML is XML. There are libraries to parse XML, from std.xml in the standard library to libraries like dxml in the package repository.
How to parse epub content
How would someone approach parsing epub files in D? Is there any libraries to parse XHTML?
Re: Win32 Api: How create Open/"Save as" Dialog?
On Friday, 10 January 2020 at 14:48:49 UTC, Marcone wrote: How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much. Solution: import std; import core.sys.windows.windows; pragma(lib, "comdlg32"); // Function askopenfilename() string askopenfilename(const(wchar)* filter = ""){ OPENFILENAME ofn; wchar[1024] szFileName = 0; ofn.lpstrFile = cast(LPWSTR) szFileName; ofn.lpstrFilter = filter; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; if (GetOpenFileNameW()){ return to!string(szFileName[0..szFileName.indexOf('\0')]); } else { return ""; } } void main(){ writeln(askopenfilename()); // Call without filter. writeln(askopenfilename("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0")); // Cal using filter. }
Re: Win32 Api: How create Open/"Save as" Dialog?
On Saturday, 11 January 2020 at 10:34:34 UTC, Marcone wrote: This code works, but I can't get file Path. Someone can help me? import std; import core.sys.windows.windows; pragma(lib, "comdlg32"); void main(){ OPENFILENAME ofn; wchar* szFileName; You need to supply a buffer, not a pointer: wchar[MAX_PATH] szFileName; (cast(byte*)& ofn)[0 .. ofn.sizeof] = 0; ofn.lStructSize = ofn.sizeof; ofn.hwndOwner = null; The above lines are unnecessary as D structs are automatically initialized and lStructSize is already filled in. ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = szFileName; It wants a pointer to your buffer: ofn.lpstrFile = szFileName.ptr; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.lpstrDefExt = "txt"; if(GetOpenFileNameW()){ writeln(szFileName); // Print null writeln(ofn.lpstrFile); // Print null You'll need to slice the buffer to the right length: import core.stdc.wchar_ : wcslen; writeln(ofn.lpstrFile[0 .. wcslen(ofn.lpstrFile.ptr)]); } }
Re: Win32 Api: How create Open/"Save as" Dialog?
On Friday, 10 January 2020 at 14:48:49 UTC, Marcone wrote: How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much. This code works, but I can't get file Path. Someone can help me? import std; import core.sys.windows.windows; pragma(lib, "comdlg32"); void main(){ OPENFILENAME ofn; wchar* szFileName; (cast(byte*)& ofn)[0 .. ofn.sizeof] = 0; ofn.lStructSize = ofn.sizeof; ofn.hwndOwner = null; ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.lpstrDefExt = "txt"; if(GetOpenFileNameW()){ writeln(szFileName); // Print null writeln(ofn.lpstrFile); // Print null } }
Re: Win32 Api: How create Open/"Save as" Dialog?
On Saturday, 11 January 2020 at 00:12:03 UTC, René Heldmaier wrote: On Friday, 10 January 2020 at 14:48:49 UTC, Marcone wrote: How create "Open" and "Save as" Dialog using "Win32 Api" and Dlang? Please send me a simple example code in Dlang. Thank you very much. Have a look at this website: http://www.winprog.org/tutorial/app_two.html It helped me a lot when i once made a simple gui in C. The tutorial is in C, but Win32 Api is C anyway... Most of the examples will probably just compile in D. Win32 Api is really ugly, i think it's a better investment to learn something like GtkD ;) Tha examples in this site don't compile in Dlang. I want Win32 Api becouse I want create very smal window gui for simple programs.
Re: Win32 Api: How create Open/"Save as" Dialog?
On Friday, 10 January 2020 at 15:44:53 UTC, Mike Parker wrote: On Friday, 10 January 2020 at 15:06:07 UTC, Marcone wrote: Very complicated. Can you send me the simple clear code? https://docs.microsoft.com/en-us/windows/win32/dlgbox/using-common-dialog-boxes Not compile in Dlang.