Ways to parse D code.
What is the "easiest" way to parse D code? Given an Expression/Statement/Function/Template I want to put it into a program, and it returns me an AST. D-Scanner seems to do that with `--ast` argument. I would need to dig into it, to get it programmatically, instead of as XML on the stdout. https://github.com/dlang-community/D-Scanner libdparse seems to do it as well with `parseModule` function. https://github.com/dlang-community/libdparse/blob/master/src/dparse/parser.d dmd has to do it somewhere as well. Although I don't know exactly where. I do know ldc uses dmd's frontend for parsing. https://dlang.org/phobos/dmd_parse.html I am also a little confused about who uses what. Does D-Scanner use libdparse? Is there a D grammar for pegged? https://github.com/PhilippeSigaud/Pegged Thank you for any hints!
Re: How exactly does Tuple work?
On Sunday, 8 November 2020 at 13:10:33 UTC, Adam D. Ruppe wrote: On Sunday, 8 November 2020 at 10:03:46 UTC, Jan Hönig wrote: Is there some recourse, which explains the `alias this`? If your object is used in a way that doesn't compile, the compiler will change `obj` to `obj.whatever_alias_this_is` and try again. So say you have struct S { int a; alias a this; } S obj; obj += 5; It will see that obj +=5 doesn't compile on its own, but it has alias a this so it changes `obj` to `obj.a` and tries again. So `obj.a += 5;` is the end result. So it's like inheritance resolved at compile time. It's inheritance with virtual member functions without overhead. I am guessing only one alias works. And we use this, because struct can't do inheritance and interface is abstract.
Re: How exactly does Tuple work?
On Saturday, 7 November 2020 at 18:31:18 UTC, Paul Backus wrote: Indexing and slicing are implemented with `alias expand this`, which causes `t[i]` to be lowered to `t.expand[i]`. Is there some recourse, which explains the `alias this`? I still don't understand what it does. I can't imagine what it does to my my class/struct.
How exactly does Tuple work?
I have a simple question. How exactly does Tuple work? In detail, I am interested in expand and opSlice. For expand, I have found the line: https://github.com/dlang/phobos/blob/master/std/typecons.d#L618 How does that work, where is the rest? What does it do? Similary I can access tuples with `[0]`. But there is no opSlice. It is also not specified in the docu: https://dlang.org/phobos/std_typecons.html#Tuple Last but not least, I can use `length`. How does that work? Where is it? I probably don't need an explenation, I mainly need to be pointed in the right direction. So instead of sacrificing your time writing an answer, a link to the lines I should look at would be fully suficient. Thanks in advance! BR, Jan
Re: Empty functions
On Thursday, 29 October 2020 at 09:01:12 UTC, Jan Hönig wrote: This would mean, that this one should work as well. It does not work as I intended, as `() => {}` has not the return type of `void`. (I don't know how to print: `ReturnType!(() => {})`)
Re: Empty functions
On Thursday, 29 October 2020 at 08:48:59 UTC, rikki cattermole wrote: () => {} Is actually: () => Expression Rule: ref|opt ParameterWithMemberAttributes => AssignExpression https://dlang.org/spec/expression.html#lambdas This would mean, that this one should work as well. And you can![1] I have changed line 13 from `F();` to `return F();`. Why does this help??? This is a little weird. [1]: https://run.dlang.io/is/eGah5v
Empty functions
I have asked this on StackOverflow[1]. I have received a valid answer, which solves my problem, however, I have still not understood, why some versions of it work and some don't. The code is here[2]. I don't understand why `a` compiles just fine, while `b` and `c` don't. I think, that I don't understand what `void function()` does or is. Can someone explain it to me? And I don't see why `(){}` works and `() => {}` does not. [1]: https://stackoverflow.com/questions/64581514/void-lambda-function/64587101#64587101 [2]: https://run.dlang.io/is/GFe9Ht
Re: What is the difference between enum and shared immutable?
On Wednesday, 28 October 2020 at 22:07:06 UTC, H. S. Teoh wrote: [...] An enum only exists at compile-time, and does not occupy any space. Each time it's referenced, a new instance of the value is created. (This is why it's a bad idea to use enum with an array literal, because every time it's referenced you get a new copy of the array.) A shared immutable is initialized at compile-time, and occupies space in the object file / runtime memory. It's also a single instance, and referencing it causes a load of this instance at runtime. It's not copied unless it's a by-value type, so an array literal will get stored as an array in the executable, and every time you reference it you get a slice of it instead of a copy. T (thanks to you and Mike btw :) Is there some rule of thumb when to use what? I judge that using enum will slow down the compilation process, but might increase performance. So for a small struct, or array enum is completelty fine. Large arrays should then use immutable, let's say when they are larger than 1kb? Regarding the shared keyword, I am not sure if immutables are shared per default. I thought they are not.
What is the difference between enum and shared immutable?
Maybe this is a silly question, but I don't understand completely the difference between an `enum` and a `shared immutable`. I could have: enum x = 1; or shared immutable x = 1; What is the exact difference between those too? My best guess is, that shared immutable gets initialized during the beginning of the programm execution. Enum has to be "ready" at compile time. Is this this accessment correct and complete? Am I missing anything else? (If correct, then I understand that shared immutable is capable of doing some sideeffects during is construction (if the constructor does that))
Re: Wanted! Tree Node implementation.
On Wednesday, 7 October 2020 at 14:31:20 UTC, Виталий Фадеев wrote: Wanted! Tree Node implementation. Like a: mixin template TreeNode( T ) { T parent; T firstChild; T lastChild; T prevSibling; T nextSibling; // ForwardRange implementation @property T front() { ... } @property bool empty() { ... } void popFront() { ... } // BackwardRange implementation @property T back() { ... } void popBack(); // RandomAccessRange implementation T opIndex( ... ) { ... } // length implementation @property size_t length() { ... } } It would be nice to get a link or package or source... You can peak into the stdlibrary, how they do it: https://github.com/dlang/phobos/blob/master/std/container/rbtree.d
Re: Why does compose from std.functional return a templated function
On Wednesday, 16 September 2020 at 10:50:06 UTC, Daniel Kozak wrote: On Wed, Sep 16, 2020 at 12:00 PM Jan Hönig via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: ... My main question is why? Is there something, which I am missing, that explains, why it is beneficial to return a templated function? (maybe, because I might want to compose together templated non-initialized functions?) It has to be templated because than you can alias it and use it many times something like import std.stdio; import std.functional : compose; import std.algorithm.comparison : equal; import std.algorithm.iteration : map; import std.array : split, array; import std.conv : to; alias StrArrToIntArr = compose!(array,map!(to!int), split); void main() { auto str1 = "2 4 8 9"; int[] intArr = StrArrToIntArr(str1); } If compose would not be template it would need to store functions addresses so it would need to have some array of functions, this would be ineffective and need to use GC Right, if i give it non-initialized templated functions, it makes a lot of sense to return a template function as well. But for functions without templates? Probably not a frequent usecase I guess.
Why does compose from std.functional return a templated function
I have toyed with the compose template in std.functional and ran into some problems. rikki_cattermole on discord helped me a lot to solve my problem. However, what still remains (for me) to understand is why. Source code for `compose`: https://github.com/dlang/phobos/blob/master/std/functional.d#L1161 `compose` pipes together given functions. And returns a TEMPLATED function. Why does it return a templated function? At compile-time, compose definitly knows, what kinds of function it composes together. So with std.traits, it could put there a definitve type, depending on the given function(s). I somewhat see, that inside compose itself, this probably solves some issues with casting (maybe). However, the last composition, i.e. the one which is returned, does not need to be templated, since it is known, what parameter has the last function. In my case, I failed to understand, that it returns a non-initialized template function, which lead into compile problems. In general I can imagine that this leads to weird compile errors, which are hard to understand. (types, casting, etc.) My main question is why? Is there something, which I am missing, that explains, why it is beneficial to return a templated function? (maybe, because I might want to compose together templated non-initialized functions?)
Re: Metaprogramming with D
On Sunday, 7 June 2020 at 00:45:37 UTC, Ali Çehreli wrote: dmd -mixin= ... thanks for the tip! writeln(q{ void foo() { } }); What is the name of this `q` thing? How do i find it? Are there any recent tutorials on it?
Metaprogramming with D
We have two (little) student projects, which should use D for meta-programming/code generation. More specifically string mixins and templates. I understand (at least I think so :)) string mixins. The task is to create a small internal DSL, which is capable of printing out D code, which we then will use as a mixin. So basically using CTFE to create code at compile time. (any additional sources/ideas are welcome). The second project considers templates. My idea was to recreate some template expressions, like we all know and love (hate) in C++. However I am not so sure if that is a really good idea. As far as I understood it, that is basically what ranges in D do. So this would just mean "code linear algebra with ranges", which I am sure plenty of D's libraries do already (mir?). (that doesn't mean, it is not a good learning experience for the student). I would love to hear some opinions on that matter.
Re: Fighting the DList
On Saturday, 16 May 2020 at 08:11:20 UTC, Jan Hönig wrote: I am convinced that this works. I have another implementation, which should work: https://run.dlang.io/is/tfBgD0 It is not pretty. It is probably not fast, if the `ignore` is too large. I guess it is in O(n^2) if not O(mn^2), where m is the number of `same` structs.
Fighting the DList
I want a simple algorithm. Given DList (or a Range, Array... I am not constrained by the container type), I want to combine certain elements depending on some condition. In the toy example, the struct and condition are simpler then in my real scenario, but i think it covers my problem: https://run.dlang.io/is/1QGw85 The function `conflate` is the algorithm I want and is currently not working (either it is wrong, or cannot compile). How I imagined it to work in rough pseudo code: outer: traverse the list: inner: traverse the list from the current outer position if condition(current outer position, current inner position) combine(current outer position, current inner position) stableDelete(current inner position) I am convinced that this works. It is really not that hard, all i need is a DoublyLinked list. D's DList however does not provide me with the interface I need for implementing this. Or I am too stupid to see how I should do it. I am not restricted on DLists, I can choose whatever I want. The rest of the code should work with just InputRanges. However doubly linked list should be most efficient for this problem, since all I want to do is delete stuff at random position, i.e. to bend some pointers. Yes I might suffer from premature optimization :)
Jupyter notebook with Dlang
Today I have been toying around with jupyter. I wanted to know how it works (since I use it with Python all the time). I was trying to couple xeus[3] with drepl[4] together. But now (after using google and not duckduckgo), I have found multiple projects, which attempted similar things. It was mentioned before[1], however JupyterD was replaced with Jupyter-wire[2], which to my understanding is something similar to xeus[3], only in D. I have not found yet a jupyterized drepl. Are there any similar projects worth looking into? [1]: https://forum.dlang.org/post/hluqmwucwayuaeejg...@forum.dlang.org [2]: https://github.com/symmetryinvestments/jupyter-wire [3]: https://github.com/jupyter-xeus/xeus [4]: https://github.com/dlang-community/drepl
Re: Compililng C++ and D together without going mad
On Wednesday, 29 April 2020 at 13:12:50 UTC, Johan wrote: Manually initializing D's runtime is also possible. You need to call rt_init and rt_term: https://dlang.org/phobos/core_runtime.html#.rt_init What I meant is to - add a `int main()` in a D source file - rename your current `main` in the C++ source file to something like `the_real_main` - add a forward reference to the D file: `extern(C) int the_real_main();` - call `the_real_main` from the `main` in the D file That way you don't have to think about how to correctly initialize D's runtime. -Johan I thought it was a good idea to ask in the forum! This actually helps me a lot, thanks!
Re: Compililng C++ and D together without going mad
On Wednesday, 29 April 2020 at 13:02:36 UTC, Jan Hönig wrote: I will probably need: Also this thing: https://github.com/nlohmann/json
Re: Compililng C++ and D together without going mad
On Wednesday, 29 April 2020 at 11:38:16 UTC, Johan wrote: LDC is a (somewhat complex) project with D and C++ code (and external C++ libraries). I think it will help you if your main() is in D (such that druntime is automatically initialized for you). https://github.com/ldc-developers/ldc -Johan Hmmm, a D main means i need to do quite some C++ bindings, with which I have 0 experience. I cannot even say if it will be possible. I will probably need: https://github.com/jupyter-xeus/xeus/blob/master/include/xeus/xkernel.hpp https://github.com/jupyter-xeus/xeus/blob/master/include/xeus/xkernel_configuration.hpp c++ stdlib's unique_ptr. And then i need to subclass a C++ class and extern this D subclass. It sounds easier to me, to manually initialize D's runtime (i imagine it being some function calls), build everything with C++, and just call D functions from C++. Or do you think it is worth the hassle with bindings? I have done C++ bindings once,years ago in python and it was a not pleasent.
Compililng C++ and D together without going mad
In my pet project, I am using some C++ libraries. The main file/function is also C++. All of it successfully compiles with cmake. Now I want to add some functionality by calling my own D functions (which use some other modules/projects/phobos). My questions is, what is the "proper" building tool/way. Do I extend my CMakeFile.txt with additional instructions, which basically do a "dmd -c ...". Do I use dub to compile the D part as a library, and use that in the cmake file? Do I use dub to compile the C++ part as well? Surely somebody has done something similar. Can you pinpoint me to some examples? I could go from there.
Re: D on android and d_android
On Tuesday, 7 April 2020 at 14:51:15 UTC, H. S. Teoh wrote: On Tue, Apr 07, 2020 at 12:43:20PM +, Adam D. Ruppe via Digitalmars-d-learn wrote: On Monday, 6 April 2020 at 08:38:03 UTC, Jan Hönig wrote: > Is there some "Hello World!" example for D on Android? [...] > However there is just so much to know. > It is really overwhelming. no kidding, I spent several weekends just trying to understand the setup and build process. And I still basically don't really know, which is why my thing there builds the D code externally and copies the files where gradle can find them instead of actually changing the gradle config. I managed to build APKs without Gradle (yes, I'm crazy like that). There are several steps, and you do need some tools from the Android SDK/NDK, namely aapt, apksigner, dx (called dalvik-exchange on some distros), zipalign, and a Java 1.7 compiler (the last time I checked; maybe they support 1.8 now, I don't know). I haven't tried a native app so far, so the way I set it up is to create a dummy Java wrapper that contains main() that calls native methods that are implemented in D. You can use Adam's jni.d to generate implementations for your native methods. Steps: 1) Follow LDC wiki to build an Android cross-compiler and cross-compiled LDC libraries (this may already be prepackaged with the latest LDC releases). Most important thing you need is the path to the droid32 or droid64 directories containing the libraries libdruntime-ldc.a, libphobos2-ldc.a, libdruntime-ldc-debug.a, libphobos2-ldc-debug.a. DROID32_PATH=/usr/src/d/android/droid32/lib # for example 2) Generate R.java: TARGET_API_LEVEL=23 # for example /usr/bin/aapt package -f -m -M AndroidManifest.xml -S res -I ${PATH_TO_ANDROID_SDK}/platforms/android-${TARGET_API_LEVEL}/android.jar -J ${PATH_TO_JAVA_SOURCE_CODE} 3) Compile Java sources: /usr/bin/javac -source 1.7 -target 1.7 -bootclasspath ${PATH_TO_ANDROID_SDK}/platforms/android-${TARGET_API_LEVEL}/android.jar -d obj -sourcepath src ${JAVA_SOURCE_FILES (including R.java)} 4) Generate classes.dex (on some systems dalvik-exchange might be called simply 'dx'): /usr/bin/dalvik-exchange --dex --output=classes.dex ${PATH_TO_OBJ_DIR} 5) Setup linker config file (for optimizing apk size): LINKER_VERSION_FILE=lib${YOUR_APP_NAME}.version cat > $LINKER_VERSION_FILE LIBGAME_1.0 { global: Java_*; local: *; }; 6) Cross-compile D code with LDC: ${PATH_TO_LDC}/bin/ldmd2 -c -mtriple=armv7-none-linux-androideabi -mcpu=cortex-a8 -L-L${DROID32_PATH}/lib -Xcc=-fpie -Xcc=-pie -Xcc=--sysroot=${PATH_TO_ANDROID_NDK}/platforms/android-${TARGET_API_LEVEL}/arch-arm -Xcc=-fuse-ld=bfd -Xcc=-gcc-toolchain -Xcc=${PATH_TO_ANDROID_NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 -Xcc=-target -Xcc=armv7-none-linux-androideabi -O4 -inline -Isrc/d -od${PATH_TO_OBJ_DIR} ${D_SOURCE_FILES} ${PATH_TO_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -Wl,-soname,lib${YOUR_APP_NAME}.so -shared -Wl,--gc-sections -Wl,--version-script=${LINKER_VERSION_FILE} -llog -landroid -lEGL -lGLESv2 --sysroot=${PATH_TO_ANDROID_NDK}/platforms/android-${TARGET_API_LEVEL}/arch-arm -fuse-ld=bfd -gcc-toolchain ${PATH_TO_ANDROID_NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 -target armv7-none-linux-androideabi ${D_OBJECT_FILES} ${DROID32_PATH}/lib/libphobos2-ldc.a ${DROID32_PATH}/lib/libdruntime-ldc.a -o lib/armeabi-v7a/lib${YOUR_APP_NAME}.so 7) Create unaligned APK: /usr/bin/aapt package -f -m -M AndroidManifest.xml -S res -I ${PATH_TO_ANDROID_SDK}/platforms/android-${TARGET_API_LEVEL}/android.jar -F bin/${YOUR_APP_NAME}.unaligned.apk /usr/bin/aapt add bin/${YOUR_APP_NAME}.unaligned.apk classes.dex /usr/bin/aapt add bin/${YOUR_APP_NAME}.unaligned.apk lib/armeabi-v7a/lib${YOUR_APP_NAME}.so 8) Sign the APK: /usr/bin/apksigner sign --ks-pass file:${YOUR_SIGNING_KEY_PASSWORD_FILE} --ks ${YOUR_SIGNING_KEY_STORE_FILE} bin/${YOUR_APP_NAME}.unaligned.apk 9) Align the APK: /usr/bin/zipalign -f 4 bin/${YOUR_APP_NAME}.unaligned.apk bin/${YOUR_APP_NAME}.apk 10) Copy bin/${YOUR_APP_NAME}.apk to your device and install it. T Thanks for the effort. I will try the "easy" way frist :D
Re: D on android and d_android
On Tuesday, 7 April 2020 at 12:43:20 UTC, Adam D. Ruppe wrote: On Monday, 6 April 2020 at 08:38:03 UTC, Jan Hönig wrote: Is there some "Hello World!" example for D on Android? So I did a tiny thing in the repo: https://github.com/adamdruppe/d_android/tree/master/android-dub-test if you open that in android studio it should load up, and the makefile is commented, but tells you the 8 commands (4 dub builds, 4 file copies, just doing the same things for each of the 4 android architecture targets) to do a full add. Thanks a lot. I was blind not finding it in your repo.
Re: D on android and d_android
Little off-topic, but I think it should fit here well. Is there some "Hello World!" example for D on Android? Having a simple example is a great springboard to start a project with lot of unknowns. On the weekend I started creating an App (kotlin), really simple stuff. However there is just so much to know. It is really overwhelming.
Re: Blog post about multidimensional arrays in D
On Friday, 27 March 2020 at 14:18:13 UTC, p.shkadzko wrote: I agree. I was planning to do several follow-ups after this first brief overview. For example, looks like just one "byDim" requires a separate post. The goal was just to show ppl who know nothing or a little about D and Mir that Mir exists and is usable. Because what I am lacking is not the API docs but introductory examples how to do mundane tasks like creating matrices and reshaping. Treat the first post as such and if you have suggestions on what is redundant or good to have I shall update it accordingly. I am excited about your blog. Exactly what i have been waiting for. If you plan to do more blog posts, please announce it in the "Announce" group and also on reddit (like Ron, e.g. https://forum.dlang.org/thread/gmqqkfzejicevkbai...@forum.dlang.org). It really helps to "catch" it. Also, since you put so much work into it, you should put it somewhere (a link) on wiki.dlang.org (https://forum.dlang.org/post/orlwvfedaixnwiekq...@forum.dlang.org)
Re: Best way to learn 2d games with D?
I made a similar search two months ago as well. I recommend a simple library. I landed with https://code.dlang.org/packages/raylib-d It is supposed to be for learning how to do 2d games. It is easy to work with, which was my main search parameter. I need to figure out the logic, physics, client-server first, before I do some fancy-pants graphics.
Re: Some impressions/notes from a new D programmer
On Wednesday, 12 February 2020 at 10:39:06 UTC, mark wrote: I've been learning D for a few weeks now. ... I made exactly the same experience in December.
Re: How to use sets in D?
On Friday, 7 February 2020 at 19:37:08 UTC, mark wrote: I am porting code from other languages to D as part of learning D, and I find I've used sets quite a lot. AFAIK D doesn't have a built-in set type or one in the std. lib. However, I've been perfectly successfully using int[E] where E is my ElementType, and adding with set[element] = 0. I mostly only need add, remove, iteration, and in, with uniqueness what I care most about. I know I could use bool[E] and set[element] = false, or I suppose container.rbtree. Would either of these--or something else built-in or in the std. lib.--be better? I have not that much experience, but i have asked the exact same thing in the IRC. rbtree was recommended. However, if it is not performance critical (in terms of speed or size), your solution works fine as well.
Re: Can't compile dlangui
On Friday, 7 February 2020 at 12:04:10 UTC, A.Perea wrote: Hi, I'm trying to compile dlangide, and it fails when compiling the dependency dlangui. Trying to compile dlangui independently gives the same error message (see below for full stack trace) As phobos nor dlangui can be broken, it should be something related to wrong installation on my side?, wrong versions of dmd/dub/phobos? dmd version: DMD64 D Compiler v2.090.0 dub version: DUB version 1.19.0, built on Jan 5 2020 Thanks for any help. I am afraid that dlangui and dlangide is currently not maintained, since i can reproduce the error as well. If you are looking for a good editor for D: I am using VisualStudioCode with the code-d plugin. If you don't like Microsoft's calling home features, you can go for Codium: https://vscodium.com/
Re: books for learning D
On Friday, 31 January 2020 at 10:51:07 UTC, mark wrote: On Wednesday, 29 January 2020 at 11:57:14 UTC, Jan Hönig wrote: On Monday, 13 January 2020 at 16:37:31 UTC, Ron Tarrant wrote: On Monday, 13 January 2020 at 10:28:48 UTC, mark wrote: [...] Actually, Andrei's book has been updated a few times over the years since first being published. The latest version says this on the copyright page: D version: 2.081.1 Book revision: 2018-10-17 So, it's really only about 14 months old. I am also curious. Where can i find the revised book. I'd also like to know where the revised "The D Programming Language" by Andrei Alexandrescu is: and whether it can be bought in physical form? I think Ron confused Ali's book and Andrei's book.
Re: books for learning D
On Monday, 13 January 2020 at 16:37:31 UTC, Ron Tarrant wrote: On Monday, 13 January 2020 at 10:28:48 UTC, mark wrote: I'm just starting out learning D. Andrei Alexandrescu's "The D Programming Language" is 10 years old, so is it still worth getting? (I don't know how much D has changed in 10 years.) Actually, Andrei's book has been updated a few times over the years since first being published. The latest version says this on the copyright page: D version: 2.081.1 Book revision: 2018-10-17 So, it's really only about 14 months old. I am also curious. Where can i find the revised book.
Re: CTFE, string mixins & code generation
On Friday, 24 January 2020 at 18:43:14 UTC, H. S. Teoh wrote: CTFE in general cannot use global variables. Any state you need must be created inside a CTFE function, and accessed from within that calling context. You *can* assign values produced by CTFE to compile-time symbols via 'enum', but there are limitations (enums cannot take AA's or class objects as values, also, once assigned they are immutable). As long as you can produce a string (via CTFE or otherwise) whose value is known at compile-time and represents valid D code, it's fair game for string mixins. However, be aware that there might be scoping and order of declaration issues (they should be rare, but you might perchance run into them). So as long as i don't have some side effects (aka global state), i am good to go with anything. Only trouble might be the output. But my output has to be strings, so i am good to go. What is your use case? Maybe describing a bit more details will help us help you. In the area of my research, we do quite a lot of code generation. We have some kind of DSL (internal or external) and we generate performant code. Usually we have some "generation" language (e.g. Python) and some target language (e.g. C++). The project of my colleague (and my master's thesis) is pystencils: https://pypi.org/project/pystencils/ A stencil code generator in Python which produces C/C++ code. It doesn't need to be in Python it can be anything (another colleague does something similar in Scala) Of course there are other approaches. If you have a language which performs well on its own, you don't need to know 2 languages (source and target). There are languages like Common Lisp, which have strong (well in case of common lisp best) macros, and are still fast enough. D might be another candidate. I am just curious and wanted to toy with it a little. P.S. If you want to know more about what CTFE / templates can or cannot do, and how they interact, you might find this article helpful: https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time This article is super helpful, let me reed it and play with it a little, before you sacrifice your time more with me :) I'll come back with more questions soon enough.
Re: CTFE, string mixins & code generation
On Friday, 24 January 2020 at 16:59:53 UTC, Marco de Wild wrote: For CTFE: functions should be pure. Therefore you cannot use global or static variables. Constants (enums) are perfectly fine to use though. I don't know the state of the GC and CTFE. I recall that there might be some complexity when using the `new` keyword. https://dlang.org/spec/function.html#interpretation Basically the only limitation of string mixins is that a single string should evaluate to valid D code, opposed to C macros. So int y mixin("= 6"); doesn't compile, while int y = mixin("6"); or mixin("int y = 6;"); does. You can use CTFE to compose the string. Ok, so mixins are really easy. CTFE is the hard pard. The link that you send me, i could not find it (or i haven't tried hard enough). Thanks!
CTFE, string mixins & code generation
I am looking for a detailed explanation or showcase regarding CTFE and string mixins. I want to play with D a little bit regarding code generation. I would like to have a pseudo-AST, consisting of a few classes, to represent some calculation. Think of a loop, some statements, and expressions. To do that, I want to be certain that this AST can be computed and generated during compile-time, with CTFE and string mixins. Are there limitations regarding CTFE (GC, global variables, static variables, templates, )? Are there any limitations regarding string mixins (which are not already included int the CTFE limitations)?
Re: Blog Post #0099: A Special Request
On Tuesday, 21 January 2020 at 22:06:30 UTC, Ron Tarrant wrote: On Tuesday, 21 January 2020 at 18:57:47 UTC, Jan Hönig wrote: I would also like to request a future blog post about animation. I know you have done that in the past, but i am thinking of some animation triggered by user input (say a button, or some text field, which provides parameters for a circle). Just to make sure I know exactly what you want... - the user inputs parameters for a circle - I'm assuming size, position, perhaps the fraction of the circle (half, quarter, two-thirds, whatever) - the user then clicks a button and that draws the circle. Is that more or less what you're after? Yes something in that manner. I am not familiar with GTK, i have done a few things in QT. I am mainly interested in the information passing. When i make some change to the input || hit enter || hit the button, the scene will change accordingly. For a more satisfying example, you could have a ball on one line. Below that you have a horizontal slider. Whenever you move the slider, the ball on the line will move accordingly.
Re: Blog Post #0099: A Special Request
On Tuesday, 21 January 2020 at 14:02:59 UTC, Ron Tarrant wrote: Today's post was requested by Joel Christensen, how to have one button affect another. You can find it here: https://gtkdcoding.com/2020/01/21/0099-sfx-button-interactions-i-text-labels.html Hi Ron. Your blog is great. I have saved it for the future. (I know it from reddit). I would also like to request a future blog post about animation. I know you have done that in the past, but i am thinking of some animation triggered by user input (say a button, or some text field, which provides parameters for a circle).
Re: DMD docker image
On Friday, 17 January 2020 at 17:31:29 UTC, Andre Pany wrote: If you really need small images, you could switch to Alpine, but MUSL c could lead to headaches ;) There are also the official docker images https://hub.docker.com/u/dlang2 What is your goal? Do you want to compile s.th. in your docker image? Then you can have a layered docker file to reduce the size. Kind regards Andre I just wanted to know, whether i am doing something completely wrong. Oh i haven't found these docker images. I did only search for "dlang" not "dlang2". Thanks a lot!
DMD docker image
I have created a docker image. However the image size is not small (~500MB). I wonder if others have a suitable dockerfile. All i want is to install the current dmd release. Does somebody have something similar? Does somebody need something similar? My dockerfile: ``` FROM ubuntu:latest MAINTAINER Jan Hönig RUN apt-get update && apt-get install curl build-essential -y \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* RUN latest=$(curl -sS http://downloads.dlang.org/releases/LATEST) \ && echo "DMD Release: $latest" \\ && curl "http://downloads.dlang.org/releases/2020/dmd_${latest}-0_amd64.deb"; -o dmd.deb \ && dpkg -i "dmd.deb" \ && rm "dmd.deb" ```
Re: Intersection of two sets
On Wednesday, 4 December 2019 at 08:01:59 UTC, Per Nordlöw wrote: On Tuesday, 3 December 2019 at 13:43:26 UTC, Jan Hönig wrote: pseudocode: alias set = bool[] set foo = ... set bar = ... set result; One simple optimization is to set* smallest; set* largest; if (foo.length < bar.length) { smallest = &foo; largest = &bar; } else { smallest = &bar; largest = &foo; } foreach(key; *smallest) { if (key in *largest) { result[key] = true; } } return result; Provided that your set type has an `in`-operator with time-complexity O(1), this will, in the worst case, reduce time complexity from O(max(foo.length, bar.length)) to O(min(foo.length, bar.length)) Thanks! I didn't even thought about optimizations like this :)
Re: Intersection of two sets
On Tuesday, 3 December 2019 at 13:55:51 UTC, Alex wrote: This depends on the available accesses on your sets. In terms of ranges: Are your sets InputRanges, ForwardRange, ... ? 2) Are there some build-in function for handling such sets? This is maybe what you are looking for: https://dlang.org/phobos/std_algorithm_setops.html In terms of ranges, i need to understand ranges properly first. The std.algorithm.setops have definitely the functionality i need. I guess my current implementation would be a simple array. I just need to make sure delete, or not create double entries.
Re: Intersection of two sets
On Tuesday, 3 December 2019 at 15:14:03 UTC, ikod wrote: Never tried, but depending of the nature of your "something" you can try bit sets. There are efficient algorithms for large bit arrays (see "roaring" for example). "roaring" is massive overkill for my case, but thanks for suggesting it. I didn't know about it.
Intersection of two sets
It seems i don't google the right keywords. What i want to do: I have two sets. (I didn't find how to do sets, so i have two associative boolean arrays `bool[]`). And i want to join them, via an intersection. I know how to code this. Loop over one AA, if the key is also in the other one, we add that to the third array which is going to be returned. pseudocode: alias set = bool[] set foo = ... set bar = ... set result; foreach(key; foo) { if (key in bar) { result[key] = true; } } return result; 1) Is there a better way for creating a set, other then `alias set = bool[keyClass]`? 2) Are there some build-in function for handling such sets?
Floating-Point arithmetic in dlang - Difference to other languages
Today i have stumbled on Hacker News into: https://0.30004.com/ I am learning D, that's why i have to ask. Why does writefln("%.17f", .1+.2); not evaluate into: 0.30004, like C++ but rather to: 0.2 Many other languages evaluate to 0.30004 as well.