Re: Function which returns a sorted array without duplicates

2023-01-21 Thread evilrat via Digitalmars-d-learn

On Sunday, 22 January 2023 at 04:42:09 UTC, dan wrote:
I would like to write a function which takes an array as input, 
and returns a sorted array without duplicates.



```d
private S[] _sort_array( S )( S[] x ) {
  import std.algorithm;
  auto y = x.dup;
  y.sort;
  auto z = y.uniq;
  // Cannot just return z; this gives:
  // Error: cannot implicitly convert expression `z` of type
  // `UniqResult!(binaryFun, uint[])` to `uint[]`


uniq and other algorithms often returns a lazy range, you can 
build an array by using `std.array.array()`


https://dlang.org/phobos/std_array.html#array

try something like this or just `return array(y.uniq);`

```d
private S[] _sort_array( S )( S[] x ) {
import std.algorithm;
import std.array;
return x.dup
   .sort
   .uniq
   .array();
}
```

And IIRC you probably don't need `dup` as sort produces a lazy 
range.






Function which returns a sorted array without duplicates

2023-01-21 Thread dan via Digitalmars-d-learn
I would like to write a function which takes an array as input, 
and returns a sorted array without duplicates.


In fact, i have a function which does this, but i think it may 
have some extra unnecessary steps.


```d
private S[] _sort_array( S )( S[] x ) {
  import std.algorithm;
  auto y = x.dup;
  y.sort;
  auto z = y.uniq;
  // Cannot just return z; this gives:
  // Error: cannot implicitly convert expression `z` of type
  // `UniqResult!(binaryFun, uint[])` to `uint[]`
  //
  // You also cannot just return cast( S[] ) z;
  //
  // Nor can you do:
  //  import std.conv;
  //  return to!( S[] )( z );
  typeof( x ) w;
  foreach ( v ; z ) w ~= v;
  return w;
}
```

My only constraint is that i really want to keep the same 
signature (i.e., return an array, not sharing structure or 
storage with the input).


Here's the usage:

```d
void main( ) {
  uint[] nums = [1, 3, 2, 5, 1, 4, 2, 8];
  auto sorted = _sort_array( nums );
  import std.stdio;
  writeln( "Input:  ", nums );
  writeln( "Output: ", sorted );
}
```

Thanks in advance for any info!

dan


Re: More Elegant Settable Methods?

2023-01-21 Thread ryuukk_ via Digitalmars-d-learn

```D
TestStruct ts = {
a: 2, b: 3,
op: (s) {
return s.a + s.b;
}
};
```


This simple! just like with C's designated initializers




Re: More Elegant Settable Methods?

2023-01-21 Thread ryuukk_ via Digitalmars-d-learn

Oops i clicked "Send" too fast



Re: How to write a library

2023-01-21 Thread Hipreme via Digitalmars-d-learn

On Saturday, 21 January 2023 at 22:53:19 UTC, Matt wrote:
I am trying to write a graphics engine for my university 
capstone project, and really wanted to give it a try in D, as 
both a talking point, and because I love the language. I'm 
using dub to build the library, and the demo application 
that'll use it.


However, I've come across a problem. In C/C++, when you build a 
library, you compile and link the source, then provide the 
header files for the library user to include. I have built the 
library, but what is the D equivalent to header files, and what 
do I have to do to prepare and use my library in another 
project?


For using your thing as a library you need to do 2 things:

1: Include the library files by using `importPaths` on dub or -I= 
on your favorite compiler

2: Add the linker flag to include the library.


importPaths don't compile your source files. Only templates and 
some ctfe features can be used when using importPaths.


Re: How to write a library

2023-01-21 Thread Steven Schveighoffer via Digitalmars-d-learn

On 1/21/23 5:53 PM, Matt wrote:
I am trying to write a graphics engine for my university capstone 
project, and really wanted to give it a try in D, as both a talking 
point, and because I love the language. I'm using dub to build the 
library, and the demo application that'll use it.


However, I've come across a problem. In C/C++, when you build a library, 
you compile and link the source, then provide the header files for the 
library user to include. I have built the library, but what is the D 
equivalent to header files, and what do I have to do to prepare and use 
my library in another project?


D uses modules, not header files. So no header files are *necessary*.

However, there is one case where you might want to use import files (D's 
equivalent to header files), and that's if you want to declare a 
specific API without providing the implementation.


For a project such as a university project, I would say this isn't 
necessary, as you don't need to obscure the implementation source from 
the users of your library.


If you do want to look into import files, for D, they are `.di` files, 
and should include prototypes for all the public functions you are 
providing, and definitions for all the types. Templates need to be 
included fully implemented source code, not just prototypes (just like C++).


-Steve


More Elegant Settable Methods?

2023-01-21 Thread jwatson-CO-edu via Digitalmars-d-learn

Hi,

I am trying to create a struct with a settable method that has 
access to the struct scope.

Is this the only way?
Is there a way to give access without explicitly passing `this`?

```d
import std.stdio;

struct TestStruct{
float /*--*/ a;
float /*--*/ b;
float function( TestStruct ) op;

float run(){
return op( this );
}
}

void main(){
TestStruct ts = TestStruct(
2,
3,
function( TestStruct s ){ return s.a + s.b; }
);

writeln( ts.run() );
}
```


Re: How to write a library

2023-01-21 Thread Adam D Ruppe via Digitalmars-d-learn

On Saturday, 21 January 2023 at 22:53:19 UTC, Matt wrote:
but what is the D equivalent to header files, and what do I 
have to do to prepare and use my library in another project?


The most common and easiest thing in D is to just distribute the 
source files, the compiler can pull whatever it needs out of 
there.


I almost never even build libraries separately, instead letting 
the compiler include them as-needed in the build.


How to write a library

2023-01-21 Thread Matt via Digitalmars-d-learn
I am trying to write a graphics engine for my university capstone 
project, and really wanted to give it a try in D, as both a 
talking point, and because I love the language. I'm using dub to 
build the library, and the demo application that'll use it.


However, I've come across a problem. In C/C++, when you build a 
library, you compile and link the source, then provide the header 
files for the library user to include. I have built the library, 
but what is the D equivalent to header files, and what do I have 
to do to prepare and use my library in another project?


Re: Pyd examples or resources for Python 3.x

2023-01-21 Thread Seamus via Digitalmars-d-learn

Thanks for all the suggestions folks!
I will take a look. Cheers.




Re: Tab completion using neovim

2023-01-21 Thread Alain De Vos via Digitalmars-d-learn

```
call dutyl#register#tool('dcd-client','dcd-client')
call dutyl#register#tool('dcd-server','dcd-server')
call deoplete#custom#option('auto_complete_delay',200)
```


Re: Tab completion using neovim

2023-01-21 Thread Alain De Vos via Digitalmars-d-learn

I managed after some tuning.
cat coc-settings.json
```
{
"languageserver": {
"d": {
"command": "/usr/home/x/serve-d/serve-d",
"filetypes": ["d"],
"trace.server": "on",
"rootPatterns": ["dub.json", "dub.sdl"],
"initializationOptions": {
},
"settings": {
}
}
},
"suggest.autoTrigger": "none",
"suggest.noselect": false
}
```



cat init.vim
```
call plug#begin()
"
Plug 'neovim/nvim-lspconfig'
Plug 'idanarye/vim-dutyl'
Plug 'landaire/deoplete-d'
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
call plug#end()
let g:ycm_language_server = [
\ {
\ 'name': 'd',
\ 'cmdline': ['/usr/home/x/serve-d/serve-d'],
\ 'filetypes': ['d'],
\ }]
let g:deoplete#sources#d#dcd_client_binary = 'dcd-client'
let g:deoplete#sources#d#dcd_server_binary = 'dcd-server'
let g:deoplete#sources#d#dcd_server_autostart = 1
```


Re: Tab completion using neovim

2023-01-21 Thread Sergey via Digitalmars-d-learn

On Saturday, 21 January 2023 at 13:17:44 UTC, Alain De Vos wrote:

Let's say i write
"write" press tab in neovim i want it to guess "writeln".
How to configure neovim for this.
[ Note "ncm2" lets my neovim crash. But maybe there are 
alternatives ]


[ vscode is not an option as compiling electron takes ages]


Is Ctrl+Space doing suggestion?
It could also be “writefln”.. so it should make several 
suggestions.


Tab completion using neovim

2023-01-21 Thread Alain De Vos via Digitalmars-d-learn

Let's say i write
"write" press tab in neovim i want it to guess "writeln".
How to configure neovim for this.
[ Note "ncm2" lets my neovim crash. But maybe there are 
alternatives ]


[ vscode is not an option as compiling electron takes ages]