My vibe-d test app is crashing on Windows
The app is just a test echo server. JSON sent in the body of a POST request is echoed back to the client. On Pop!_OS it works fine but on Windows it responds, there's a delay of about 10 seconds and then it crashes with the error: ``` Error Program exited with code -1073741819 ``` Here's the code: ```d import vibe.vibe; import std.json, std.stdio; void main() { auto settings = new HTTPServerSettings; settings.port = 8080; settings.bindAddresses = ["::1", "127.0.0.1"]; auto router = new URLRouter; router.get("*", serveStaticFiles("public/")); router.post("/", &onPost); auto listener = listenHTTP(settings, router); scope (exit) { listener.stopListening(); } logInfo("App listening at http://127.0.0.1:8080/";); runApplication(); } static string handlePost(string req) { return req; } void onPost(HTTPServerRequest req, HTTPServerResponse res) { try { auto jsr = async(&handlePost, req.json.toString()).getResult(); res.contentType("application/json; charset=utf-8"); res.writeBody(jsr); } catch (Exception e) { res.contentType("text/plain; charset=utf-8"); res.writeBody(e.msg); } } ``` Also when you launch/build the app with dub there's a shed load of deprecation warnings, e.g.: ``` \Local\dub\packages\vibe-d-0.9.5\vibe-d\stream\vibe\stream\wrapper.d(334,23): Deprecation: reference to local variable `this` assigned to non-scope parameter `bytes` calling `writeToStream` ``` I'm a D newbie so it's quite possibly something I'm doing wrong ...
Re: ddbc with Vibe-d
On Monday, 13 February 2023 at 01:43:38 UTC, Steven Schveighoffer wrote: I think it needs to be immutable if it's a reference. -Steve I have tested args with isWeaklyIsolated!(typeof(arg)) and it looks like good news 👍️ Thanks for your help, Steve.
Re: ddbc with Vibe-d
On Sunday, 12 February 2023 at 19:38:47 UTC, Steven Schveighoffer wrote: That might work, depending on args. As documented in the API, if anything in args are mutable references, then it is run as a task, and the same problem still applies (it will use a fiber, and count on the concurrency of vibe's fibers to deal with the asynchronicity). If it is possible, it will run in a worker thread, and then it can execute concurrently. I believe you can do: isWeaklyIsolated!(typeof(args)) to see if it will run in another thread. I admit not having any experience with this function. -Steve In my case args will just be the body of the HTTPServerRequest which is JSON. How can I get that JSON and pass it to async() in a manner that ensures I get a worker thread?
Re: ddbc with Vibe-d
On Sunday, 12 February 2023 at 15:24:14 UTC, Steven Schveighoffer wrote: Any synchronous calls will just be synchronous. They aren't going to participate in the async i/o that vibe uses. In other words, when you block on a call to sqlite, it will block everything else in your web server until that completes. Would this be the correct approach to stop it blocking?: ```d auto result = async(&doSqliteStuff, args...).getResult(); ```
ddbc with Vibe-d
Hi, I'm trying D for the first time and so far I'm really impressed with both D and vibe-d. My test project is an application server and I want to use SQLite3 as its database. I understand Vibe.d uses an async model under the hood and so my question is are Vibe-d and ddbc compatible? Thanks.
Re: how to return map(fn)
float times_two(float x) {return 2*x;} // if you would rather make sure the result is an array float[] times_two_array(float[] arr) { import std.algorithm; // for map import std.array; // for array return arr .map!times_two // map your function .array; // convert to an array } void main() { import std.stdio; import std.algorithm; // for map alias map2x = map!times_two; writeln(map2x([1., 2., 3.])); float[] arr2 = times_two_array([1., 2., 3.]); writeln(arr2); } ``` -Steve that is exactly what I was trying to achieve thank you so much!
Re: how to return map(fn)
Thank you both a lot for your help. I am new to D so all of this is incredibly helpful. This seems like an amazing community! @Ali I will have a look at std.functional as I think this is really what I was looking for. Until then, I have solved the problem with a simple class (e.g. below). I'm sure its not a particularly good way of doing it but it will work for now :). Also thank you for your book, its amazing! ``` class Mapper { float function(float) fn; this(float function(float) func) {this.fn = func;} float[] opCall(float[] x){ auto arr = new float[x.length]; foreach (i,elem; x) { arr[i] = fn(elem);} return arr; } } float times_two(float x) {return 2*x;} void main() { import std.stdio; Mapper map2x = new Mapper(×_two); writeln(map2x([1., 2., 3.])); }```
Re: how to return map(fn)
On Monday, 21 February 2022 at 23:07:44 UTC, Ali Çehreli wrote: On 2/21/22 12:44, steve wrote: ... thanks for your help. I'm unfortunately still a bit confused. Maybe I wasn't clear or maybe I'm just missing something here. What I was trying to return is function that can then be applied to e.g. an array. As I understand the mapped function you wrote expects a range and a function argument something that would let me do ``` float my_function(float x){ return 2*x;} auto my_function_mapped = mapped(my_function); assert(my_function_mapped([1.,2.,3.]) == [2.,4.,6.]); ```
how to return map(fn)
following this example in the documentation of map: ``` import std.algorithm.comparison : equal; import std.conv : to; alias stringize = map!(to!string); assert(equal(stringize([ 1, 2, 3, 4 ]), [ "1", "2", "3", "4" ])); ``` I would like to write a function that takes as its parameter a function and returns something like the alias in the example. i.e. something like: ``` float[] function(float[]) get_map(alias f){ return map!(f) } ``` I had a look at the source code for map but it seems to return a private struct, which doesn't help. Is there any way to get this to work?
Re: Differences between lambda function and regular functions in higher order functions
thanks a lot both! Yes I'm aware that map exists already. This was didactic. I had tried to find out whether lambdas generate function pointers but also couldn't figure that one out :D
Differences between lambda function and regular functions in higher order functions
I am trying to implement a simple map function. I found code to do this in another post but it only seems to work with lambda functions and I do not understand why. Any help would be greatly appreciated ``` import std.stdio; T[] map_vals(T,S)(scope T function(S) f, S[] a){ auto b = new T[a.length]; foreach(i,x;a) b[i] = f(x); return b; } auto timestwo(float x) { return 2*x; } void main(){ float[] my_array = [1., 2., 3.]; auto ff = (float x)=>2*x; // This works writeln(map_vals(ff, my_array)); // this does not // writeln(map_vals(timestwo, my_array)); } ```