Re: GUI library for DMD 2.090 or DMD 2.091

2020-04-27 Thread Antonio Corbi via Digitalmars-d-learn

On Monday, 27 April 2020 at 11:27:57 UTC, Paulo Pinto wrote:

On Sunday, 26 April 2020 at 09:09:04 UTC, Antonio Corbi wrote:

On Saturday, 25 April 2020 at 09:30:44 UTC, Paulo Pinto wrote:

On Friday, 24 April 2020 at 18:52:55 UTC, Russel Winder wrote:

[...]


Just curious, how do you handle the whole RC> story 
in Gtk-rs?


For me it made the point that languages with tracing GC or 
implicit reference counting are much better solution for 
doing GUI programming.


Hi Paulo,

I don't know if you are referring to the `clone!` macro 
described here[1]


[1] https://gtk-rs.org/blog/2019/12/15/new-release.html

Antonio


Hi, this macro is new to me, it did not exist when I tried to 
have a go at Gtk-rs, so it simplifies not having to write such 
boilerplate ourselves, but like the author mentions it doesn't 
make it go away, it just gets hidden behind the macro.


Hi,

Yes, previously this macro lived (in a simplified form, i.e. no 
@strong) in the examples provided by the gtk-rs developers. Now 
it's part of the gtk-rs bindings.


Antonio


Re: GUI library for DMD 2.090 or DMD 2.091

2020-04-26 Thread Antonio Corbi via Digitalmars-d-learn

On Saturday, 25 April 2020 at 09:30:44 UTC, Paulo Pinto wrote:

On Friday, 24 April 2020 at 18:52:55 UTC, Russel Winder wrote:

[...]


Just curious, how do you handle the whole RC> story 
in Gtk-rs?


For me it made the point that languages with tracing GC or 
implicit reference counting are much better solution for doing 
GUI programming.


Hi Paulo,

I don't know if you are referring to the `clone!` macro described 
here[1]


[1] https://gtk-rs.org/blog/2019/12/15/new-release.html

Antonio


Re: Blog Post #0099: A Special Request

2020-01-23 Thread Antonio Corbi via Digitalmars-d-learn

On Thursday, 23 January 2020 at 12:29:11 UTC, Ron Tarrant wrote:
On Thursday, 23 January 2020 at 08:33:39 UTC, Antonio Corbi 
wrote:



https://gitlab.com/amcorbi/dmaurerrose

I think this is somewhat related to what te OP was asking for.


I'm confused. I was the OP, so I'm not sure who you're 
referring to.


Ooops, sorry, my bad!



Assuming you are on GNU/Linux, a simple 'dub run' is all you 
have to do to make it compile and run.


On Windows, `dub run` yielded the following output:

Performing "debug" build using C:\D\dmd2\windows\bin\dmd.exe 
for x86_64.
gtk-d:gtkd 3.9.0: target for configuration "library" is up to 
date.
gtk-d:gstreamer 3.9.0: target for configuration "library" is up 
to date.
gtk-d:peas 3.9.0: target for configuration "library" is up to 
date.
gtk-d:sv 3.9.0: target for configuration "library" is up to 
date.
gtk-d:vte 3.9.0: target for configuration "library" is up to 
date.

maurerrose-d ~master: building configuration "application"...
Running pre-build commands...
source/constants.d.in was unexpected at this time.
Command failed with exit code 1: if [ source/constants.d.in -nt 
source/constants.d ]; then sed -e 
's#topdir#D:\temp\dmaurerrose-master#' source/constants.d.in > 
source/constants.d; fi



Yes, it uses 'sed' to get 'constants.d' from 'constants.d.in' at 
configuration time.


So you can write your own 'constants.d' with the appropiate 
values for UIDIR and DATADIR and delete/comment the the 
preBuildCommands in dub.sdl.


Hope this helps.
Antonio


Re: Blog Post #0099: A Special Request

2020-01-23 Thread Antonio Corbi via Digitalmars-d-learn

On Wednesday, 22 January 2020 at 21:22:57 UTC, Ron Tarrant wrote:
On Tuesday, 21 January 2020 at 22:12:10 UTC, Antonio Corbi 
wrote:

On Tuesday, 21 January 2020 at 22:06:30 UTC, Ron Tarrant wrote:



Do you still have the Maurer Rose example?


I had to look this up. I'd never heard of it before. That being 
said...



Do you think it could fit this kind of example?


When I say "demo" I'm talking about examples of how to do 
various GTK GUI stuff rather than mathematical exercises. 
Trying to do this mathematical/drawing exercise, I'd be 
waaay out of my element. The few simple math/draw examples 
I did in Cairo (posts #0057 to #0064) is about my limit as far 
as math goes.


However, I can point you at this repository of Java examples: 
https://github.com/iabin/Maurer-Rose/tree/master/src


Using the methods outlined in the posts I've listed, you should 
be able to port it over from Java to D. I'm not a 
mathematician, so I wouldn't be of much help.


I wish you the best of luck on this endeavour.


I was referring to this example I made some time ago:

https://gitlab.com/amcorbi/dmaurerrose

I think this is somewhat related to what te OP was asking for.

Assuming you are on GNU/Linux, a simple 'dub run' is all you have 
to do to make it compile and run.



Antonio


Re: Blog Post #0099: A Special Request

2020-01-21 Thread Antonio Corbi via Digitalmars-d-learn

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?


Hi Ron!

Do you still have the Maurer Rose example? Do you think it could 
fit this kind of example?


Antonio


Re: Unexpected aliasing

2019-11-11 Thread Antonio Corbi via Digitalmars-d-learn

On Monday, 11 November 2019 at 19:17:37 UTC, Bastiaan Veelo wrote:
Recently I got my first surprise with our use of D. The symptom 
was that two local variables in two different functions 
appeared to be sharing data.


A simplified example is shown below (the original was machine 
translated from Pascal and involved templates and various 
levels of indirection). What I did not know is that the initial 
value of struct members is a compile time feature, apparently. 
What I suspect is happening is that the array lives in the 
static data segment (or is created in the module constructor?) 
and that the slices inside arr1 and arr2 get initialised to 
point to that same array.


I could use some help in rewriting the code below so that arr1 
and arr2 each have their own data; ideally with minimal changes 
so that I can make the transcompiler do the right thing.


Thanks!
Bastiaan.

void main()
{
import std.stdio;

WrapIntegerArray arr1;
arr1[0] = 42;

WrapIntegerArray arr2;

writeln(arr2[0]); // 42, not 0.
writeln("arr1.wrap.arr.ptr = ", arr1.wrap.arr.ptr);
	writeln("arr2.wrap.arr.ptr = ", arr2.wrap.arr.ptr); // 
identical

assert(arr2[0] == 0); // fails
}

struct IntegerArray
{
int[] arr;
alias arr this;
this(int l)
{
arr = new int[l];
}
}

struct WrapIntegerArray
{
auto wrap = IntegerArray(5); // This is CTFE! :-(
alias wrap this;
}



Defining and using a constructor for WrapIntegerArray seems to 
work:


void main()
{
import std.stdio;

WrapIntegerArray arr1 = WrapIntegerArray(5);
arr1[0] = 42;

WrapIntegerArray arr2 = WrapIntegerArray(5);

writeln(arr2[0]); // 42, not 0.
writeln("arr1.wrap.arr.ptr = ", arr1.wrap.arr.ptr);
writeln("arr2.wrap.arr.ptr = ", arr2.wrap.arr.ptr); // identical
assert(arr2[0] == 0); // fails
}

struct IntegerArray
{
int[] arr;
alias arr this;
this(int l)
{
arr = new int[l];
}
}

struct WrapIntegerArray
{
this (int v) {
  wrap = IntegerArray(5);
}

IntegerArray wrap;
alias wrap this;
}

Hope this helps.
Antonio



Re: Blog Post #79: Notebook, Part III - Customized Tabs, Part I

2019-10-16 Thread Antonio Corbi via Digitalmars-d-learn

On Tuesday, 15 October 2019 at 22:02:35 UTC, WebFreak001 wrote:

On Tuesday, 15 October 2019 at 20:03:00 UTC, Ron Tarrant wrote:

...

Do you have links for these?


thanks! :p

both the packages can simply be found on dub: 
https://code.dlang.org/search?q=glade


Not sure if there are other ways like directly loading an XML 
in GTK, haven't looked into it too much yet because I am not so 
often building GTK GUI applications, but with the new Linux 
Phones on the market (Librem 5, PinePhone) running GTK Apps 
natively and really needing some Apps those will be great 
platforms to start app development on.


Hi WebFreak001, Ron:

There's no need to generate code from glade files.
You can load at runtime the XML file that glade generates.

You have to create an instance of the Gtk.Builder class[1], and 
supply it
the XML file, i.e. from the file that you saved from glade[2] and 
after that you 'load' your UI controls into your program 
variables using getObject[3], a small snippet of this pattern:


---
   
   auto builder = new Builder();
   
if(!builder.addFromFile(buildPath(pkgdatadir,"ui/MainWindow.ui")))

   {
  writeln("Window ui-file cannot be found");
  return;
   }

   HeaderBar headerBar = cast(HeaderBar) 
builder.getObject("headerBar");
   Box windowContent = cast(Box) 
builder.getObject("windowContent");

   ...
---

Once I wrote this extremely simple class to simplify Builder 
usage:


---
module gtagui.uibuilder;

private import gobject.ObjectG;
import gtk.Builder;

class UiBuilder : Builder {
  this (string uif) {
if (!addFromFile (uif))
  throw new Exception ("File not found: " ~ uif);
  }

  public T getObject(T) (string name) {
return (cast(T) super.getObject (name));
  }
}
---

So you can now write things like this:

---
  public void loadUiFrom (string uifile) {
uib = new UiBuilder (uifile);

topbox = uib.getObject!Box ("box1");

theCanvas = uib.getObject!DrawingArea("imgwindow");
assert (theCanvas !is null);
---

Hope this helps.
Antonio

[1] https://api.gtkd.org/gtk.Builder.Builder.html
[2] https://api.gtkd.org/gtk.Builder.Builder.addFromFile.html
[3] https://api.gtkd.org/gtk.Builder.Builder.getObject.html


Re: Abstract classes vs interfaces, casting from void*

2019-08-10 Thread Antonio Corbi via Digitalmars-d-learn

On Saturday, 10 August 2019 at 08:20:46 UTC, John Colvin wrote:

On Friday, 9 August 2019 at 13:39:53 UTC, Simen Kjærås wrote:




Thanks for the extra detail.

Is there a solid reason to ever use an interface over an 
abstract class? (Other than multiple inheritance).


I'm such a noob at anything related to OO.


Hi John.

One reason could be data. Abstract classes can hold data, 
interfaces can't.


Antonio


Re: Linux & DMD & GtkD

2019-02-16 Thread Antonio Corbi via Digitalmars-d-learn

On Saturday, 16 February 2019 at 16:28:09 UTC, Ron Tarrant wrote:
On Saturday, 16 February 2019 at 14:29:56 UTC, Russel Winder 
wrote:



Pass, sorry.


Thanks for the replies, guys...

I quoted the above line because it's just about the only thing 
I understood. Let me explain...


It's been almost 20 years since I used Linux and my notes are 
long gone. A ton of things have changed. Like, for instance, it 
found all my hardware this time, without me getting involved. 
But also the software installation managers and lots of other 
things are completely unrecognizable. On top of that, my brain 
has (unfortunately) been molded into the Windows/Microsoft way 
of thinking.


I guess what I'm hoping for is a step-by-step, 
full-on-hand-holding, large-print with pictures tutorial for 
how to get GtkD and (I guess) dub working. I know the stuff you 
wrote is probably helpful, but I'm not well-enough versed in 
Linux or any flavour of UNIX ATM to understand it, pick which 
option I should use, and to put these commands in proper order.


So far, as I said, dmd is working. Up until now, I've avoided 
dub because, with so many code examples (more than 70 at 
present) dub would bring in a lot of file/directory clutter.


But you're saying that dub will make it easier to keep up with 
future changes/updates in all the various bits and bobs, so if 
that means I finally have to do dub, perhaps someone could 
point me at a good tutorial for that?


Sorry if I sound ungrateful or cranky, but I have a lot on my 
plate ATM and I'm getting frustrated.


Hi Ron,

For one-file programs, dub usage is very easy.

Placing a minimal comment-header between /+ +/ symbol comments, 
gets your job done like in this example ( I think the gtkd code 
is from one of your examples in the blog):


/+
 dub.sdl:
 
 name   "gtkhello"
 dflags "-dip25" "-dip1000"
 dependency "gtk-d:gtkd" version="~>3.8.0"
 +/

/*
  dub run --single gtkhello.d
  dub build --single gtkhello.d.
  dub gtkhello.d .
*/


module gtkhello;

import std.stdio;
import gtk.MainWindow;
import gtk.Main;
import gtk.Widget;
import gtk.Layout;
import gtk.Button;
import gdk.Event;

void main(string[] args)
{
  Main.init(args);
  TestRigWindow myTestRig = new TestRigWindow("Test Rig");
  myTestRig.showAll();
  Main.run();

} // main()


class TestRigWindow : MainWindow
{
  this(string title)
  {
// window
super(title);
addOnDestroy(delegate void(Widget w) { quitApp(); } );

auto myButton = new MyButt("Button Name");
auto myOtherButton = new MyOtherButt("Other Button Name");

// layout
auto myLayout = new MyLayout(myButton, myOtherButton);
add(myLayout);

  } // this() CONSTRUCTOR


  void quitApp()
  {
writeln("Bye.");
Main.quit();

  } // quitApp()

} // class myAppWindow


class MyLayout : Layout
{
  this(MyButt myButton, MyOtherButt otherButton)
  {
super(null, null);
put(myButton, 10, 20);
put(otherButton, 10, 60);

  } // this()

} // class MyLayout


class MyButt : Button
{
  this(string labelText)
  {
super(labelText);
addOnButtonRelease();

  } // this()


  bool doSomething(Event e, Widget w)
  {
writeln("Something was done.");

return(true);

  } // doSomething()

} // class MyButt


class MyOtherButt : Button
{
  this(string labelText)
  {
super(labelText);
string message = "Something other than that was done.";
addOnClicked(delegate void(_) { doSomething(message); } );

  } // this()


  void doSomething(string messageText)
  {
writeln(messageText);

  } // doSomething()

}
---

Antonio



Re: Submenu Not Responding Until Second Click

2019-02-08 Thread Antonio Corbi via Digitalmars-d-learn

On Friday, 8 February 2019 at 10:03:03 UTC, Ron Tarrant wrote:
On Thursday, 7 February 2019 at 08:41:29 UTC, Antonio Corbi 
wrote:



Hi Ron,

xrandr (and gui interfaces for it like arandr) are your 
friends here.


xrandr -q -> shows your card outputs and then you can use 
xrandr + options to configure monitors.


Or you can use arandr that will do that for you and will allow 
you to visually spatially-arrange your monitors.


Antonio


Thanks for the tip, Antonio. I'd never heard of xrandr or 
arandr. Must be new since I stopped paying attention a few 
years ago.


No, xrandr has been around for a long time: 
https://www.x.org/wiki/Projects/XRandR/


It makes easier than xinerama to configure several monitors. For 
example when I start my xsession with an external monitor 
attached to the HDMI port, I switch-off the internal laptop panel 
and use only the external one, a small script like this does the 
trick:


xrandr | grep "HDMI-1 conn" >/dev/null
if [ $? = 0 ]
then
  xrandr --output HDMI-1 --auto --primary --output LVDS-1 --off
fi

Those names like HDMI-1 or LVDS-1 are the ones that 'xrandr' or 
'xrandr -q' show you. Depending on the driver/card combination 
they may change.


Xrandr requires that your card driver supports it, nowadays it is 
the usual thing, but you'll have to check that.


Arandr (there are others) simplify the configure task due to 
their GUI based interface, though I prefer the text based 
interface that xrandr offers.


Antonio.


Re: Submenu Not Responding Until Second Click

2019-02-07 Thread Antonio Corbi via Digitalmars-d-learn

On Wednesday, 6 February 2019 at 13:13:44 UTC, Ron Tarrant wrote:
On Tuesday, 5 February 2019 at 09:41:06 UTC, Antonio Corbi 
wrote:



It could be so, I'm not using gnome so I can't say.
By the way, I'm using gtk3 3.24.5.


Yeah, I updated from 3.22 to 3.24, but it made no difference on 
Windows 10. Still that delay with submenus.


I'd rather be running FreeBSD, but ATM I'm using an MSI laptop 
with two external monitors and I'm still working on getting 
Xorg configured to recognize even one of those external 
monitors. So, until then, I guess I'm stuck with this behaviour.


Hi Ron,

xrandr (and gui interfaces for it like arandr) are your friends 
here.


xrandr -q -> shows your card outputs and then you can use xrandr 
+ options to configure monitors.


Or you can use arandr that will do that for you and will allow 
you to visually spatially-arrange your monitors.


Antonio


Re: Submenu Not Responding Until Second Click

2019-02-05 Thread Antonio Corbi via Digitalmars-d-learn

On Tuesday, 5 February 2019 at 09:31:01 UTC, Ron Tarrant wrote:
On Tuesday, 5 February 2019 at 08:51:49 UTC, Antonio Corbi 
wrote:


Have you tweaked your gtk theme? If so, could you try with the 
default (Adwaita) gtk theme?


This is a Linux/Gnome thing, I'm assuming? Still, I'll look 
into other configuration stuff and see where it leads. Thanks, 
Antonio.


It could be so, I'm not using gnome so I can't say.
By the way, I'm using gtk3 3.24.5.

Antonio


Re: Submenu Not Responding Until Second Click

2019-02-05 Thread Antonio Corbi via Digitalmars-d-learn

On Monday, 4 February 2019 at 18:34:55 UTC, Ron Tarrant wrote:
I posted about this over on the GtkD site, but I suspect no 
one's home until later in the day.


[...]



And it still does the same thing. First click, blue line. 
Second click, menu drops.


Hi Ron:

It's working OK for me (gtkd 3.8.5).
Have you tweaked your gtk theme? If so, could you try with the 
default (Adwaita) gtk theme?


Antonio


preconditions and interfaces

2019-01-20 Thread Antonio Corbi via Digitalmars-d-learn

Hi all,

Playing with interfaces and preconditions in methods I get 
strange results with dmd-2.0.84.0 but also with dmd-nightly. My 
code is like this:

-
import std.stdio;

interface Thing2D {
  void width(int w)
in {
  writeln("Thing2D.width contract w = ",w);
  assert(w > 0);
}
}

class Line : Thing2D {
  override void width(int w)
in {
  writeln("Line.width contract w = ",w);
  assert(w >= 0);
}
  do {
writeln("Line.width: w = ", w);
  }
}

void main() {
  auto l = new Line;
  l.width(-1);
}

---

1) With dmd-2.084.0 I get:
./ifaceprecond
Thing2D.width contract w = 2
Line.width: w = -1

2) With dmd-nightly (as of today) I get random values for 
interface's 'w':

./ifaceprecond
Thing2D.width contract w = 647271536
Line.width: w = -1

I think this should be an error caught by the contract, isn't it?

Thx!
Antonio


Re: Destructor for struct invoked many times

2019-01-15 Thread Antonio Corbi via Digitalmars-d-learn
On Tuesday, 15 January 2019 at 10:49:17 UTC, rikki cattermole 
wrote:
Because you passed it by value to writeln, which goes on to 
pass it to many other functions.


Thanks Rikki!

I was thinking about something like that.
Antonio


Destructor for struct invoked many times

2019-01-15 Thread Antonio Corbi via Digitalmars-d-learn

Hi,

In this simple example, the destructor for the struct is invoked 
four more times than expected:



import std.stdio;

struct Person {
  string name;
  int age;

  ~this() {
writefln("%s is gone (0x%x)", name, );
  }
}

int main(string[] args) {
  Person* p = new Person;

  writefln ("Created person (0x%x)", p);
  p.name = "Peter";
  p.age = 23;

  writeln("Person:", *p); // Comment this line and try

  return 0;
}


Output:

Created person (0x7f85ee997000)
Person:Person("Peter", 23)Peter is gone (0x7ffd916c1560)
Peter is gone (0x7ffd916c15c0)

Peter is gone (0x7ffd916c1640)
Peter is gone (0x7ffd916c16f0)
Peter is gone (0x7f85ee997000)
-

If I comment the line "writeln("Person:", *p);" then the 
destructor is invoked only one time as expected.


Why is it?


Re: getopt short-options documentation

2018-11-30 Thread Antonio Corbi via Digitalmars-d-learn

On Thursday, 29 November 2018 at 20:55:22 UTC, Daniel Kozak wrote:
Are you sure? Can you show me an example? I always forgot on 
this limitation and somtimes it cause really nesty things :D


On Thu, Nov 29, 2018 at 6:05 PM Antonio Corbi via 
Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote:



Hi!

Reading through the `getopt` documentation at one point it 
says:


   "Forms such as -t 5 and -timeout=5 will be not accepted."

But I'm able to to use short options like '-t 5' (with spaces 
between the 't' and the '5'). It seems that this limitation 
has been eliminated and it just-works-now, is it so?


Thx!


Hi Daniel!

Try this one :
---
module tools.trainer;

import std.stdio, std.getopt;

//
// Main Program //
//-
int main(string[] args) {

  dcharletter = 'ñ';
  string[] inputFiles;
  string[] cmpFiles;


  arraySep = ",";  // defaults to "", separation by whitespace
  auto helpInformation = getopt(args,
"letter|l", "The char that 
represent the input images.", ,
"inputf|i", "The files that 
represent the image of the same char.", ,
"cmpf|c",   "The files that 
represent images of other chars to compare with.", );


  if (helpInformation.helpWanted) {
defaultGetoptPrinter("Some information about the program.",
 helpInformation.options);
  }

  writeln("Letter selected is: ", letter);
  return 0;
}
-

trainer -l Y
trainer -lY

Both of them work for me.

Antonio


getopt short-options documentation

2018-11-29 Thread Antonio Corbi via Digitalmars-d-learn

Hi!

Reading through the `getopt` documentation at one point it says:

  "Forms such as -t 5 and -timeout=5 will be not accepted."

But I'm able to to use short options like '-t 5' (with spaces 
between the 't' and the '5'). It seems that this limitation has 
been eliminated and it just-works-now, is it so?


Thx!


dub + ddoc

2018-11-21 Thread Antonio Corbi via Digitalmars-d-learn

Hi!

I'm trying to build the docs for my project with `dub -b docs` 
and dub also generates the docs for the dependencies of my 
project.


Is it possible to generate the docs *only* for my code?

Thx!


Re: GtkD help

2017-11-19 Thread Antonio Corbi via Digitalmars-d-learn
On Saturday, 18 November 2017 at 22:31:15 UTC, Ivan Trombley 
wrote:
Any information about using gio.Settings would be really 
appreciated too.


Hi Ivan,

I would recommend you to search for information about Gtk under 
valadoc pages [1]. You'll get Vala syntax but doing the 
mental-mapping to D+GtkD is automatic, i.e TreeView [2] and Glib 
Settings [3].


A. Corbi

[1] https://valadoc.org/index.htm
[2] https://valadoc.org/gtk+-3.0/Gtk.TreeView.html
[3] https://valadoc.org/gio-2.0/GLib.Settings.html


Re: core file

2017-11-13 Thread Antonio Corbi via Digitalmars-d-learn

On Monday, 13 November 2017 at 09:49:29 UTC, codephantom wrote:

On Monday, 13 November 2017 at 06:25:20 UTC, Tony wrote:
I am on Ubuntu 16.04. Thanks, I didn't know that "producing a 
core file" was configurable, and it appears that it isn't.


ok. that's because Ubuntu is not (by default) setup for 
developers.


But you can enable core dump for your executable easily enough.

e.g, if you out file is a.out, then do this:

ulimit -S -c unlimited a.out
(now run your program, and you'll get core file in the same dir 
where the program is)
(this won't be remembered after a reboot, so just do it when/as 
required).


You can view the core file using > objdump -s core

Good luck understanding the contents of that ;-)

Core dumps typically require some specialised expertise to 
analyse.


Unless you're one of those specialists, then better to just run 
your a.out through the debugger, and forget about the core dump 
(until you can't)  ;-)


Hi,

core files can be used to do a 'post-mortem' debug session with 
gdb, from 'man gdb':


 You can also start with both an executable program and a core 
file

 specified:

   gdb program core

If your program is compiled with '-g' option, gdb will load the 
executable and extract the information from the core file and it 
will show your program's state exactly as if it had been run from 
the debugger and had failed in that exactly moment.


Antonio


Re: Working with images

2017-11-01 Thread Antonio Corbi via Digitalmars-d-learn

On Wednesday, 1 November 2017 at 12:02:08 UTC, Alexandre wrote:
I have a project written in C++, that I'm thinking to migrating 
to D, but, what is preventing me from migrating to D, is the 
part of the system that works with images, where the system 
generates the image of a payment receipt, currently in the 
system written in C ++, there is an array with image base RGB 
where I can open that RGB by lib. that I use to generate the 
image in C ++ (the lib I use is the CImg), in this system I 
need to insert texts, images and a bar code in that base image..


There is some lib. in D ( multiplataform ), that I could be 
this, insert texts, imagens and a barcode into a existing 
image, that I could be open from memory ?


Hi Alxandre,

You could use cairo[1] and the port to D provided by the GtkD 
project[2].


[1] https://www.cairographics.org/
[2] https://api.gtkd.org/gtkd/cairo/Context.html

Antonio


Re: gtkD load images

2017-08-04 Thread Antonio Corbi via Digitalmars-d-learn

On Thursday, 3 August 2017 at 21:06:36 UTC, Johnson Jones wrote:

On Thursday, 3 August 2017 at 13:12:03 UTC, Mengu wrote:
On Thursday, 3 August 2017 at 03:59:40 UTC, Johnson Jones 
wrote:
How can be use gtkD to load images, I assume through 
gdkpixbuf? While I am getting errors loading images through 
glade's image:


(test.exe:8188): Gtk-WARNING **: Could not load 
image 'a.jpg': Couldn't recognize the image file format for 
file 'test\a.jpg'


(loads fine in glade)

which needs to be resolved, I'd also like to be able to use 
gdkpixbuf to load images programmatically. There seems to be 
no demos on the gtkD github page that deals with image 
loading.


I've tried to do something like

import gtkc.gdkpixbuf;
auto x = c_gdk_pixbuf_get_formats().data;

but I don't know how to interpret x.

Also something like

import gtkc.gdkpixbuf;
void* x;
auto p = c_gdk_pixbuf_get_formats();
for(int i = 0; i < 10; i++)
{   
x = p.data;
p = p.next;
}

which doesn't offer any help.


Aside: How can we call the gtk functions directly using gtkD? 
Seems it uses stuff like


Linker.link(gdk_pixbuf_get_formats, "gdk_pixbuf_get_formats", 
LIBRARY_GDKPIXBUF);


It does seem to alias to these function but something is off 
and I'm not sure what.


hi

- is the gtk.Image class not working for you? 
https://api.gtkd.org/gtkd/gtk/Image.html


- there's also a gdkpixbuf.Pixbuf that you can use. 
https://api.gtkd.org/gtkd/gdkpixbuf/Pixbuf.html


- you can import those functions from gdkpixbuf.c.functions.


So, like I said, I've tried

import gdkpixbuf.Pixbuf;
auto x = Pixbuf.newFromResource("C:\\a.jpg");

which gives me that error I stated before for x86. For x64 it 
simply crashes and no exception is given.


If I use a dummy image I get the same error:

"Unhandled exception: glib.GException.GException The resource 
at “C:\adf.jpg” does not exist at 
generated\gtkd\glib\GException.d(40)"


The error message makes look like like it should be


"Unhandled exception: glib.GException.GException The resource 
at “C:\adf.jpg” does not exist at “C:\” 
generated\gtkd\glib\GException.d(40)"



I'd rather use Pixbuf than image because I don't need to 
generate a full blow image widget.


If I do

GError* err = null;
auto p = 
gdk_pixbuf_new_from_resource(Str.toStringz("C:\\a.jpg"), );


p is null and err is

The resource at “D:\a.jpgâ€. does not exist.

which is clearly not true.

So not sure what is going on ;/ Seems to be a bug in pixbuf or 
am I specifying the path wrong? If gtk.Image uses these 
internally then it is working so...


Hi!

I load images using Gtk like this (I use gtk under gnu/linux):

 Pixbuf pixbuf = new Pixbuf (filename);

And draw them in a GtkDrawingArea like this:

// redrawPage is a method from my window class
//theCanvas = cast(DrawingArea) 
theBuilder.getObject("wCanvas");

//theCanvas.addOnDraw ();

private bool redrawPage (Scoped!Context ctx, Widget wda) {
if (pixbuf !is null) {
  Pixbuf img;
  int w, h;

  ctx.scale (drawingScale, drawingScale);

  w = cast(int) (pixbuf.getWidth  * drawingScale);
  h = cast(int) (pixbuf.getHeight * drawingScale);

  theCanvas.setSizeRequest (w, h);

  ctx.setSourcePixbuf (cast(Pixbuf) thePixmap, 0.0, 0.0);
  ctx.paint ();
}
return true;
  }

Hope it helps you.
A. Corbi



Re: How to replace pairs tags with regexp

2017-07-21 Thread Antonio Corbi via Digitalmars-d-learn

On Friday, 21 July 2017 at 07:42:28 UTC, Suliman wrote:

On Friday, 21 July 2017 at 07:30:07 UTC, Antonio Corbi wrote:

On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:

On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:

There reason of issue above is spaces before "#".


What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56

I expect that it will select:

#Header
my header text

##SubHeader
my sub header text

Because: ^#{3}


Have you tried https://regex101.com/

It gives you results and explanations about your regex in 
realtime.


A. Corbi


I tried. But I am getting different behavior in online editor 
and in the code.


Could it be related to the regex's 'flavor' you are using?

A. Corbi


Re: How to replace pairs tags with regexp

2017-07-21 Thread Antonio Corbi via Digitalmars-d-learn

On Friday, 21 July 2017 at 07:08:34 UTC, Suliman wrote:

On Friday, 21 July 2017 at 06:19:43 UTC, Suliman wrote:

There reason of issue above is spaces before "#".


What wrong with next regex https://dpaste.dzfl.pl/024a47ed2a56

I expect that it will select:

#Header
my header text

##SubHeader
my sub header text

Because: ^#{3}


Have you tried https://regex101.com/

It gives you results and explanations about your regex in 
realtime.


A. Corbi


Re: replacement for squeeze and removechars.

2017-07-18 Thread Antonio Corbi via Digitalmars-d-learn

On Tuesday, 18 July 2017 at 15:55:00 UTC, Seb wrote:

On Tuesday, 18 July 2017 at 15:41:44 UTC, Meta wrote:
As Seb somewhat undiplomatically put, there are replacements 
listed in the changelog.


Sorry - it wasn't intended to be an offense or aggressive. I 
consider(ed) RTFM as common internet slang.


Dont' worry Seb, I really appreciate very much your help.
A. Corbi


Re: replacement for squeeze and removechars.

2017-07-18 Thread Antonio Corbi via Digitalmars-d-learn

On Tuesday, 18 July 2017 at 15:41:44 UTC, Meta wrote:

On Tuesday, 18 July 2017 at 15:28:06 UTC, Antonio Corbi wrote:

Hi all,

I'm trying dmd-2.075.0-rc1 in one of my projects where I use 
`squeeze` and `removechars`. Both of them are flagged as 
obsolete and in the docs we are suggested to use functions 
from std.regex and/or std.algorithm.


Does any one kow a one-liner from std.regex or std.algorithm 
that can take the role of those deprecated functions?


Thank's
A. Corbi


As Seb somewhat undiplomatically put, there are replacements 
listed in the changelog.


Use std.regex.replaceAll to replace std.string.removechars:

import std.string;
import std.regex;

// old
"abc".removechars("a-z");

// new
"abc".replaceAll(regex("[a-z]"), "");



Use std.algorithm.iteration.uniq to replace std.string.squeeze:

import std.algorithm;
import std.string;

// old
"hello".squeeze;

// new
"hello".uniq;



Though it would be nice to have these alternatives listed right 
there in the deprecation message.


Thanks Meta and Seb!

Yes, the replacement is there 8), but as the deprecation message 
told me "to go to the docs", I only had in mind the library 
manual pages and not the changelog.


I agree with you that, at least, they could also be listed next 
to `squeeze` and `removechars`.


Thanks for your help!
A. Corbi


replacement for squeeze and removechars.

2017-07-18 Thread Antonio Corbi via Digitalmars-d-learn

Hi all,

I'm trying dmd-2.075.0-rc1 in one of my projects where I use 
`squeeze` and `removechars`. Both of them are flagged as obsolete 
and in the docs we are suggested to use functions from std.regex 
and/or std.algorithm.


Does any one kow a one-liner from std.regex or std.algorithm that 
can take the role of those deprecated functions?


Thank's
A. Corbi


Re: std.stdio.stderr

2017-06-10 Thread Antonio Corbi via Digitalmars-d-learn

On Saturday, 10 June 2017 at 16:10:18 UTC, Russel Winder wrote:
It appears that std.stdio.stderr does not wor exactly as stdio 
stderr

does. In particular std.stdio.stderr.writef(…) does not work as
fprintf(stderr…) does.

Some code I am porting from C++ to D makes use of ANSI escape 
codes to go up a line and overwrite what was there, as well as 
change colours. This work fine in the C++ code but fails in the 
D code. The codes are definitely all the same, the only 
difference is in the writing functions.


Is this problem to be expected or should it work?


Hi Russel,

It seems to work for me with a dumb example:

```
import std.stdio;

void main()
{
  writefln("stdout: %s", "Edit source/app.d to start your 
project.");
  stderr.writefln("stderr: %s", "Edit source/app.d to 
start your project.");
  stderr.writefln("stderr: %s", "Edit source/app.d to 
start your project.");


  stderr.writefln("%s", "");
}
```
Before copy/paste take into account that in sequences like "[7m", 
etc... there's a hidden ESC char at the beginning, something 
like: "\033[7m", and I can't see it in the preview of my posting.


Antonio


Re: Switch ignores case (?)

2016-11-24 Thread Antonio Corbi via Digitalmars-d-learn

On Wednesday, 23 November 2016 at 22:13:38 UTC, ketmar wrote:
On Wednesday, 23 November 2016 at 22:00:58 UTC, Steven 
Schveighoffer wrote:
I can't see why you need to deal with the glue layer at all -- 
just tell the glue layer that it's a list of strings and not 
dstrings ;)


'cause that is how s2ir.d is done in dmd. ;-) it actually sorts 
*objects*, and objects knows how to order 'emselves. at this 
stage it is not trivial to treat objects as byte arrays (easy, 
but not a one-liner).


sure, other compilers may do that differently, and it doesn't 
really matter how exactly the code for switch is generated 
until it works correctly. ;-)


Could it be possible to ping M. Nowak to include the fix for this 
bug (due to its importance) in the final 2.072.1 release?


Antonio


Re: Continued looking at properties in D - interfaces and constraints

2016-10-12 Thread Antonio Corbi via Digitalmars-d-learn

On Wednesday, 12 October 2016 at 06:20:05 UTC, mikey wrote:

On Sunday, 9 October 2016 at 14:06:42 UTC, Antonio Corbi wrote:
1. Inheritance with contracts is evaluated in a special way, 
'in contracts' in the base and derived method (property) are 
or-ed, so if one of them passses, the contract is believed to 
have succeeded. As you don't have a contract in the 
base-method: "@property void width(int width)", I think that 
the compiler assumes that its empty contract is always true.



Doesn't sound right to me. "Or"ing contracts with different 
inherited methods can't possibly be a feature. I'll create a 
bug report for it.


Hi Mikey,
It's the way DbC (Design by Contract) works, have a look at point 
8 in 
https://www.eiffel.com/values/design-by-contract/introduction/


Antonio


Re: Continued looking at properties in D - interfaces and constraints

2016-10-09 Thread Antonio Corbi via Digitalmars-d-learn

On Sunday, 9 October 2016 at 11:54:50 UTC, mikey wrote:

Hi,

I'm continuing to look at properties in D and have found 
another area where I think there may be an issue - or maybe 
where I'm doing something wrong.


I have started trying to use constraints on my properties to 
constrain which values they can take I have also started trying 
to use interfaces. What I noticed was that when I combine these 
2 features the constraints get discarded.


interface Widthy {
@property inout(int) width() inout;
@property void width(int width);
}

class Test : Widthy {
private:
int _w;
public:
@property inout(int) width() inout { return _w; }
@property void width(int width)
in {
import std.exception;
if (width < 0) {
throw new
Exception("width is less than zero");
}
}
body {
_w = width;
}
}

void main() {
import std.stdio;
auto t = new Test;
t.width = -1;
writeln("width: ", t.width);
// width: -1

// hmmm... not good
}


Hi Mikey,

I think the failure you are experimenting is a mixture of two 
problems:


1. Inheritance with contracts is evaluated in a special way, 'in 
contracts' in the base and derived method (property) are or-ed, 
so if one of them passses, the contract is believed to have 
succeeded. As you don't have a contract in the base-method: 
"@property void width(int width)", I think that the compiler 
assumes that its empty contract is always true.


So it's a matter of adding some contract to it, i.e.:

interface Widthy
{
  @property inout(int) width() inout;
  @property void width(int w) in { assert(w > 7); }
}

But then comes...

2. This approach fails with dmd version: DMD64 D Compiler v2.071.2
but it works with ldc 1.0.0 and also with ldc-git (based on DMD 
v2.071.2 and LLVM 3.8.1 ,  built with DMD64 D Compiler v2.071.2), 
so I think it's a bug in DMD, but I'm very new to D, so I hope 
that someone here with deeper knowledge can throw some light :)


Antonio



Re: Learning ddoc

2016-09-29 Thread Antonio Corbi via Digitalmars-d-learn

On Thursday, 29 September 2016 at 11:50:26 UTC, bachmeier wrote:
On Thursday, 29 September 2016 at 09:35:56 UTC, Antonio Corbi 
wrote:

[...]


They used https://github.com/economicmodeling/harbored


Thanks! that's it.
Antonio


Learning ddoc

2016-09-29 Thread Antonio Corbi via Digitalmars-d-learn

Hi,

I'm in the process of learning how ddoc works.
I've successfully created docs for my code and recently learned 
how to generate it using dub.


Related to this and after seeing the announcement of the new 
release of the emsi-containers library, I had a look at its docs 
(http://economicmodeling.github.io/containers/index.html) but 
when I cloned the repo and generated it locally its appearance is 
completely different (no side-bar, different formatting for code 
examples, etc...), it's simpler, it must lack some css or 
whatever.


My question is...is there some standard way to generate 
better-looking doc-pages with ddoc?


Thanks!
Antonio


Re: How to get a screenshot?

2016-09-22 Thread Antonio Corbi via Digitalmars-d-learn
On Thursday, 22 September 2016 at 07:50:07 UTC, Antonio Corbi 
wrote:
On Thursday, 22 September 2016 at 02:21:16 UTC, Konstantin 
Kutsevalov wrote:
On Wednesday, 21 September 2016 at 16:36:32 UTC, Adam D. Ruppe 
wrote:

[...]


Hmm, that's good idea as fast solution. Thank you Adam.


Hi Konstantin,

I've got a simple example in Vala language but it's very easy 
to rewrite it in D thank's to GtkD (and also thanks to Mike Wey 
for his work in GtkD!):


//  compile: valac --pkg gtk+-3.0 --pkg gdk-3.0  screenshot.vala

int main (string[] args) {
Gtk.init (ref args);

int  width, height;

Gdk.Window win = Gdk.get_default_root_window();

width = win.get_width();
height = win.get_height();

Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 
0, width, height);


screenshot.save("screenshot.png","png");
return 0;
}


Ok, took the time to translate it to D and created a github repo 
to clone. You can download and try it from: 
https://github.com/antoniocorbibellot/dsshot


Hope It helps you.
Antonio


Re: How to get a screenshot?

2016-09-22 Thread Antonio Corbi via Digitalmars-d-learn
On Thursday, 22 September 2016 at 02:21:16 UTC, Konstantin 
Kutsevalov wrote:
On Wednesday, 21 September 2016 at 16:36:32 UTC, Adam D. Ruppe 
wrote:
On Wednesday, 21 September 2016 at 16:33:58 UTC, Konstantin 
Kutsevalov wrote:
do anybody know how to get screenshot (for now in Linux 
only)? May be some library , examples?


simplest might be to just call out to `shellExec("import 
-window root filename.png");` and it will write the file.


Hmm, that's good idea as fast solution. Thank you Adam.


Hi Konstantin,

I've got a simple example in Vala language but it's very easy to 
rewrite it in D thank's to GtkD (and also thanks to Mike Wey for 
his work in GtkD!):


//  compile: valac --pkg gtk+-3.0 --pkg gdk-3.0  screenshot.vala

int main (string[] args) {
Gtk.init (ref args);

int  width, height;

Gdk.Window win = Gdk.get_default_root_window();

width = win.get_width();
height = win.get_height();

Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, 
width, height);


screenshot.save("screenshot.png","png");
return 0;
}



Re: Array Copying syntax

2016-09-16 Thread Antonio Corbi via Digitalmars-d-learn
On Friday, 16 September 2016 at 18:12:22 UTC, Steven 
Schveighoffer wrote:

On 9/16/16 2:03 PM, Antonio Corbi wrote:


Hi Jonathan!

Probably this entry in 
https://dlang.org/spec/arrays.html#usage confused

me a bit.

int[3] s;

s = ...;   // error, since s is a compiled in static
   // reference to an array.

Thanks for your help!
Antonio


Yeah, that's bad.

https://github.com/dlang/dlang.org/pull/1474

-Steve


Hi Steve,

Way clearer (at least for me) with your patch!

Thanks,
Antonio


Re: Array Copying syntax

2016-09-16 Thread Antonio Corbi via Digitalmars-d-learn
On Friday, 16 September 2016 at 17:55:59 UTC, Jonathan M Davis 
wrote:
On Friday, September 16, 2016 17:22:41 Antonio Corbi via 
Digitalmars-d-learn wrote:
Shouldn't it be mentioned then in the docs that this works for 
statically sized arrays and that in that case it copies 
contents?


Well, I confess that I don't know why you would ever have 
thought that


s = t;

wouldn't work. The only mutable types that can't be assigned to 
like this are the ones that have disabled assignment or copying 
(which would only be certain structs).


And I thought that the spec made it clear that static arrays 
were value types, in which case the behavior of


s = t;

should be obvious. So, I would have thought that it would be 
clear enough as-is, but clearly, it wasn't for you. So, I don't 
know. We do want the spec to be clear, but it would also be bad 
to add a bunch of redundant information in an attempt to be 
clearer. I'd have to go digging through the spec though to give 
any concrete suggestions on how to improve this though.


- Jonathan M Davis


Hi Jonathan!

Probably this entry in https://dlang.org/spec/arrays.html#usage 
confused me a bit.


int[3] s;
...
s = ...;   // error, since s is a compiled in static
   // reference to an array.

Thanks for your help!
Antonio


Re: Array Copying syntax

2016-09-16 Thread Antonio Corbi via Digitalmars-d-learn

On Friday, 16 September 2016 at 17:11:54 UTC, Adam D. Ruppe wrote:
On Friday, 16 September 2016 at 17:03:20 UTC, Antonio Corbi 
wrote:
Is it safe to use or do I have to use the proposed 's[] = t;' 
or 's[] = t[]' ?


That works for all arrays. `s = t` for dynamically sized arrays 
(aka slices) just sets the references to the same, but for your 
statically sized arrays, it also copies.


Thank's Adam!

That's what my toy program told me:
---8><--
import std.stdio;

alias matrix = uint[3][2];

int main(string[] args) {
  matrix s;
  matrix t = [[1,2,3],
  [4,5,6]];

  writeln ("s= ", s);
  writeln ("s.ptr= " , s.ptr);

  writeln ("t= ", t);
  writeln ("t.ptr= " , t.ptr);
  writeln ("t[1][2]= ", t[1][2]);

  s = t;

  writeln ("s= ", s);
  writeln ("s.ptr= " , s.ptr);

  return 0;
}
---8><--

...but I wanted to be sure! :)

Shouldn't it be mentioned then in the docs that this works for 
statically sized arrays and that in that case it copies contents?




Array Copying syntax

2016-09-16 Thread Antonio Corbi via Digitalmars-d-learn

Hi!

I was just playing with array initialization and copying and 
discovered that this syntax works as expected but it is not 
referenced under https://dlang.org/spec/arrays.html#array-copying:


-8><-
int[3] s;
int[3] t;

s = t;
-8><-

Is it safe to use or do I have to use the proposed 's[] = t;' or 
's[] = t[]' ?

Thank's!


Re: std.functional.compose compilation error

2016-08-25 Thread Antonio Corbi via Digitalmars-d-learn

On Thursday, 25 August 2016 at 14:30:00 UTC, Meta wrote:
On Thursday, 25 August 2016 at 14:06:32 UTC, Antonio Corbi 
wrote:

Hello,

Trying to compile this example from Chuck Allison:
---
import std.stdio;
import std.functional;

void main() {
auto div3 = (double x) => x/3.0;
auto sq = (double x) => x*x;
auto pls1 = (double x) => x+1.0;
alias compose!(div3,sq,pls1) comp;
writeln(comp(2.0)); // 3 == (2.0+1.0)^^2 / 3.0
alias pipe!(div3,sq,pls1) pip;
writeln(pip(2.0));  // 1.4 == (2.0/3.0)^^2 + 1.0
}


I get this error (with DMD64 D Compiler v2.071.1 in linux):

compose.d(8): Error: template instance compose!(div3, sq, 
pls1) compose is not a template declaration, it is a module


But the error disappears if I use this import:
   import std.functional:compose,pipe;

Is this a bug or is it the expected behaviour under the recent 
'import' changes?

Thanks!


Try renaming your source file to something other than 
compose.d, I think that's confusing the compiler.


Yep, that did the trick!

I also noticed that 'old' compilers like:

- gdc: gdc (GCC) 6.1.1 20160501
- ldc: LDC - the LLVM D compiler (1.0.0):  based on DMD v2.070.2 
and LLVM 3.8.0


*do* compile the original code posted without error.

Thank's to all of you for your answers.



std.functional.compose compilation error

2016-08-25 Thread Antonio Corbi via Digitalmars-d-learn

Hello,

Trying to compile this example from Chuck Allison:
---
import std.stdio;
import std.functional;

void main() {
auto div3 = (double x) => x/3.0;
auto sq = (double x) => x*x;
auto pls1 = (double x) => x+1.0;
alias compose!(div3,sq,pls1) comp;
writeln(comp(2.0)); // 3 == (2.0+1.0)^^2 / 3.0
alias pipe!(div3,sq,pls1) pip;
writeln(pip(2.0));  // 1.4 == (2.0/3.0)^^2 + 1.0
}


I get this error (with DMD64 D Compiler v2.071.1 in linux):

compose.d(8): Error: template instance compose!(div3, sq, pls1) 
compose is not a template declaration, it is a module


But the error disappears if I use this import:
   import std.functional:compose,pipe;

Is this a bug or is it the expected behaviour under the recent 
'import' changes?

Thanks!




Re: Caesar Cipher Cracking

2016-08-15 Thread Antonio Corbi via Digitalmars-d-learn

On Sunday, 14 August 2016 at 18:36:02 UTC, Stefan wrote:

same code, just a little shorter.

usage of ".array"
more UFCS
replaced cast with ".to"



Wow Stefan!
Thanks for your time, I'll have a look at it!
Antonio


Caesar Cipher Cracking

2016-08-14 Thread Antonio Corbi via Digitalmars-d-learn

Hi folks,

I was just following Graham Hutton's excellent book "Programming 
in Haskell" (http://www.cs.nott.ac.uk/~pszgmh/book.html) and in 
chapter 5 He implements a Caesar-Cipher cracking algorithm in a 
few lines of Haskell code 
(http://www.cs.nott.ac.uk/~pszgmh/cipher.lhs).


So, as I'm also learning D, I decided to port it to D using the 
very little functional-programming in D that I know.


No attempt to optimize it has been made, only following the 
original Haskell code and rewrite it into D so there's room 
(plenty) for optimization and usage of functional-programming 
(I'm just learning here) techniques to make it more D-ish.


Hope this is useful to someone that is also learning D.
Happy message-cracking!

Antonio

---8><---
import std.stdio, std.conv;
import std.algorithm, std.algorithm.searching, std.range;
import std.ascii, std.string : countchars;

int let2int (char c) {
  return cast(int) (c - 'a');
}

char int2let (int n) {
  if (n >= 0)
return cast(char) ('a' + n);
  else
return cast(char) ('z' + n + 1);
}

char shift (int n, char c) {
  return isLower (c) ? int2let ((let2int (c) + n) % 26) : c;
}

string encode (int n, string s) {
  string r;

  s.each!(c => r ~= shift(n, c));
  return r;
}

alias decode = (int n, string s) { return encode (-n, s); };

float percent (int n, int m) {
  return (n / cast(float) m) * 100;
}

int[] positions (float x, float[] fl) {
  int[] il;

  auto r = zip (fl, iota(fl.length)).filter!(t => t[0]==x).map!(t 
=> t[1]);

  r.each!(a => il ~= cast(int)a);

  return il;
}

int lowers (string s) {
  return cast(int) count!(a => a.isLower)(s);
}

int cccount (char c, string s) {
  return cast(int) countchars(s, to!string(c));
}

float[] freqs (string s) {
  float[] f;
  auto allChars = "abcdefghijklmnopqrstuvwxyz";
  auto n = lowers(s);

  auto r = allChars.map!(a => percent (cccount(to!char(a), s), n) 
);

  r.each!(a => f ~= a);

  return f;
}

float chisqr (float[] os, float[] es) {
  return zip(os, es).map!(t => ((t[0]-t[1])^^2)/t[1]).sum;
}

float[] rotate (int n, float[] fl) {
  float[] f;

  auto r = fl.drop(n) ~ fl.take(n);
  r.each!(a => f ~= a);

  return f;
}

string crack (string s) {
  // ASCII letters frequency from 'a'..'z'
  float[] table = [8.2, 1.5, 2.8, 4.3, 12.7, 2.2, 2.0, 6.1, 7.0, 
0.2, 0.8, 4.0, 2.4,
   6.7, 7.5, 1.9, 0.1, 6.0,  6.3, 9.1, 2.8, 1.0, 
2.4, 0.2, 2.0, 0.1];

  float[] table2 = s.freqs;

  auto chitabr = iota(26).map!(n => rotate(n, 
table2).chisqr(table));

  float[] chitab;
  chitabr.each!(a => chitab ~= a);
  auto minval = reduce!(min)(chitab);
  auto factor = positions (minval, chitab)[0];

  return decode (factor, s);
}

void main()
{
   writeln ("wyvnyhttpun pu kshun pz clyf mbu! = ",
crack ("wyvnyhttpun pu kshun pz clyf mbu!"));
}




Re: Default implementations in inherited interfaces

2016-07-24 Thread Antonio Corbi via Digitalmars-d-learn

On Sunday, 24 July 2016 at 07:54:11 UTC, Jonathan Marler wrote:

On Thursday, 21 July 2016 at 13:37:30 UTC, Saurabh Das wrote:

On Thursday, 21 July 2016 at 12:42:14 UTC, Adam D. Ruppe wrote:

On Thursday, 21 July 2016 at 09:41:27 UTC, Saurabh Das wrote:
Java 8 has a 'default' keyword that allows interfaces to 
provide a default implementation and sub-classes can 
optionally override it if needed. The rationale behind it was 
extending interfaces without causing old code to faill. 
(called "virtual extension methods" or "defender methods"). 
The use case is similar to above.


Is there a way to achieve an equivalent functionality in D?

Thanks,
Saurabh


What an interesting technique. I've never seen this before. 
Maybe a DIP is in order? I think it would be low priority 
relative to the current work being done, but this technique 
seems like a good thing to support in the language.


I first heard about this technique (or similar) in this post by 
Jim Nelson about the Vala language: 
https://blogs.gnome.org/jnelson/2011/11/01/a-few-of-my-favorite-vala-things-interface/


Antonio


Re: Dub recursive build and forcing pre-build commands to run

2016-07-10 Thread Antonio Corbi via Digitalmars-d-learn

On Sunday, 10 July 2016 at 07:00:18 UTC, Meta wrote:

On Saturday, 9 July 2016 at 07:52:56 UTC, Antonio Corbi wrote:

I use the "preGenerateCommands" and "dependencies" like this:

"configurations" : [
{
"comment" : "Classic app.",
"name": "eqapp",
"preGenerateCommands" : ["sed -e 
's#topdir/#$PACKAGE_DIR#' src/config/constants.d.in > 
src/config/constants.d"],
"dependencies": {":dgtalib" : "*", 
"gtk-d:gtkd": "~>3.3.0"},

},
  

Hope this helps you.
Antonio


When are pre-generate commands run?


Official docs say this:

preGenerateCommands	string[]	A list of shell commands that is 
executed before project generation is started


If my memory serves me well I started to use it instead of 
preBuildCommands because they are always executed and, in my 
case, I needed that (to simulate the autoconf/cmake 'file.in -> 
file' idiom).


Antonio


Re: Dub recursive build and forcing pre-build commands to run

2016-07-09 Thread Antonio Corbi via Digitalmars-d-learn

On Saturday, 9 July 2016 at 00:03:22 UTC, Meta wrote:

I have two questions.

1. Is it possible to build subpackages when dub build is 
invoked for the main package? It's a pain to first do dub build 
:mysubpackage and then dub build.


2. It seems that when dub detects that no dependencies need to 
be updated, it does not run the pre-build commands. I have some 
pre-build commands that generate data to be imported by the 
main package and thus must always be run for a build. Is there 
a way to specify this?


I use the "preGenerateCommands" and "dependencies" like this:

"configurations" : [
{
"comment" : "Classic app.",
"name": "eqapp",
"preGenerateCommands" : ["sed -e 
's#topdir/#$PACKAGE_DIR#' src/config/constants.d.in > 
src/config/constants.d"],
"dependencies": {":dgtalib" : "*", 
"gtk-d:gtkd": "~>3.3.0"},

},
  

Hope this helps you.
Antonio


Re: Needed return type in static method? bug or feature?

2016-03-08 Thread Antonio Corbi via Digitalmars-d-learn

On Tuesday, 8 March 2016 at 14:13:17 UTC, Adam D. Ruppe wrote:

On Tuesday, 8 March 2016 at 13:40:06 UTC, Antonio Corbi wrote:

Is it a feature or a bug?


It is allowed because the "auto" keyword doesn't actually 
required for auto functions (or variables), what you need is 
any one of the storage classes.


Those include static, auto, const, immutable, even pure.

If any of them are present, the compiler knows you are writing 
a function or declaring a variable and will infer the type.


Thank's Adam!.

I had figured out something like this but I couldn't find 
anything in the docs 
(http://dlang.org/spec/attribute.html#static), moreover, the 
example there:

--8><-
class Foo
{
static int bar() { return 6; }
...
--8><-

does mention the return type, that's what confused me.



Needed return type in static method? bug or feature?

2016-03-08 Thread Antonio Corbi via Digitalmars-d-learn

Hi all!

The following code compiles and works, but the static methods do 
not
have a return type. It also compiles and works if the appropiate 
(or auto)

return type is added to them.

-8><
import std.stdio;

class B {
int foo () { return 1; }

static sbar () { return "hi!"; }
static ibar () { return 0; }
}

void main () {
auto b = new B;
writeln (B.sbar);
writeln (B.ibar);
}
-8><
Is it a feature or a bug?

I've seen it being used in
https://github.com/gecko0307/dlib/blob/master/dlib/math/matrix.d

Thank's for your help!