Re: Web APis

2023-12-31 Thread Alexandru Ermicioi via Digitalmars-d-learn

On Sunday, 31 December 2023 at 04:40:02 UTC, Axel Casillas wrote:

Hi there,

I'm trying to implement web api's into a terminal program. With 
some help at the IRC have gotten pretty far but just hit a 
roadblock trying to manipulate the web api to accept input from 
the user.


Example:

auto content = 
get("www.webapiurl.com/data/v4/example?name=category=2022-01-24=2023-01-24=01010101010101010101");


writefln! %s (content);

the above is just an example and for the most part it works but 
I am unable to make the 'category','from=date' and 'to=date' a 
user modified variable.


It doesn't matter how I attempt to split the API into different 
sections and define variables before running the program it 
wont compile.


Has anybody ever run into this and if someone has could you 
point me to some example code that might make it ease for me to 
understand, would greatly appreciate.


Perhaps:?

```d
auto content = 
get(format("www.webapiurl.com/data/v4/example?name=category=%s=%s=01010101010101010101", fromDate, toDate));


writefln!"%s"(content);
```

Best regards,
Alexandru



Re: How to implement filterMap

2023-12-31 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 31 December 2023 at 09:47:27 UTC, Siarhei Siamashka 
wrote:
Also take a look at the `c` array. The handling of arithmetic 
overflows is a safety problem of the D language design. Certain 
types of input may cause overflows, which result in producing 
bogus data as a result of running your program and are very 
difficult to troubleshoot. The use of the GDC's `-ftrapv` 
option surely helps in troubleshooting such cases, but some 
software or D libraries may intentionally rely on the D's 
arithmetic wraparound feature.


`CheckedInt` should be another solution for overflows, if you 
really need it to throw exceptions, on overflow errors.





Re: How to implement filterMap

2023-12-31 Thread Siarhei Siamashka via Digitalmars-d-learn
On Sunday, 31 December 2023 at 09:47:27 UTC, Siarhei Siamashka 
wrote:
On Saturday, 30 December 2023 at 13:25:00 UTC, Christian 
Köstlin wrote:
The "original" 
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter_map works with the Option(Some/None) ...


Here's an example with `Nullable`, inspired by the earlier 
comment from Alexandru Ermicioi:


[...]


And for comparison, here's a Ruby/Crystal example too:

```Ruby
# Emulate the Crystal's method ".to_i?" for Ruby
class String def to_i?; self.to_i rescue nil if self =~ 
/^\s*\-?[0-9]+\s*$/ end end


a = ["1", "2", "abc", ""]
pp a.map {|s| (v = s.to_i?) ? v * 2 : nil}.compact
pp a.map {|s| s.to_i?}.compact.map {|v| v * 2}

b = ["1", "2", "abc", "", "", "99"]
pp b.map {|s| (v = s.to_i?) ? v * 2 : nil}.compact
pp b.map {|s| s.to_i?}.compact.map {|v| v * 2}

c = ["1", "2", "abc", "", "19", "99"]
pp c.map {|s| (v = s.to_i?) ? v * 2 : nil}.compact
pp c.map {|s| s.to_i?}.compact.map {|v| v * 2}
```

Running as a Ruby program:
```
[2, 4]
[2, 4]
[2, 4, 19998, 198]
[2, 4, 19998, 198]
[2, 4, 38, 198]
[2, 4, 38, 198]
```

Running as a Crystal program:
```
[2, 4]
[2, 4]
[2, 4, 198]
[2, 4, 198]
Unhandled exception: Arithmetic overflow (OverflowError)
```

Ruby is an interpreter with an arbitrarily large BigInt integer 
type. Crystal is a native compiler with the default 32-bit 
integer type (but it also supports 64-bit integers, 128-bit 
integers and BigInts via optional type annotations).


This particular Crystal code is badly written and stinks because 
it's silently filtering out "" (similar to my 
earlier D example, which has exactly the same problem). But the 
arithmetic overflow during multiplication is at least caught at 
runtime. The code can be of course updated to allow wraparounds 
or filter out the arithmetic overflows from the resulting array 
too, but I think that this is a dubious approach in general.


By the way, under the hood Crystal is effectively using a more 
powerful generalization of D's `Nullable`: 
https://crystal-lang.org/reference/1.10/syntax_and_semantics/union_types.html


Re: Web APis

2023-12-31 Thread ryuukk_ via Digitalmars-d-learn

nvm, that's not what you are asking for


Re: Web APis

2023-12-31 Thread ryuukk_ via Digitalmars-d-learn

On Sunday, 31 December 2023 at 04:40:02 UTC, Axel Casillas wrote:

Hi there,

I'm trying to implement web api's into a terminal program. With 
some help at the IRC have gotten pretty far but just hit a 
roadblock trying to manipulate the web api to accept input from 
the user.


Example:

auto content = 
get("www.webapiurl.com/data/v4/example?name=category=2022-01-24=2023-01-24=01010101010101010101");


writefln! %s (content);

the above is just an example and for the most part it works but 
I am unable to make the 'category','from=date' and 'to=date' a 
user modified variable.


It doesn't matter how I attempt to split the API into different 
sections and define variables before running the program it 
wont compile.


Has anybody ever run into this and if someone has could you 
point me to some example code that might make it ease for me to 
understand, would greatly appreciate.



Try this:

```D
import std.uri;
auto content = 
get("www.webapiurl.com/data/v4/example?name=category=2022-01-24=2023-01-24=01010101010101010101".encode);

```

Notice the ``.encode``

https://dlang.org/phobos/std_uri.html#.encode


Re: How to implement filterMap

2023-12-31 Thread Siarhei Siamashka via Digitalmars-d-learn
On Saturday, 30 December 2023 at 13:25:00 UTC, Christian Köstlin 
wrote:
On Saturday, 30 December 2023 at 01:22:31 UTC, Siarhei 
Siamashka wrote:
On Friday, 29 December 2023 at 23:10:47 UTC, Christian Köstlin 
wrote:
Is there a way to implement filterMap (meaning do mapping of 
a range, but if something happens during the map, leave this 
element out of the resulting range).


It's probably not a good idea to do this in general. Expecting 
a lot of exceptions handling happening during normal program 
execution (if you want to filter out roughly half of the input 
array) will result in a major performance loss. Exceptions are 
best left to just do error handling on a very rarely used code 
path for troubleshooting purposes.


Thanks for the feedback.

This might be true, but in my example I would parse the input 
always with conv.to, so I would need to handle the exception(s).


I still think that a much better design is to have a try/catch 
block much higher in the call stack and print a "malformed input" 
error message to the user. Or handle the error in some other way 
(for example, allow the user to try again with a different input 
data).


The "original" 
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter_map works with the Option(Some/None) ...


Here's an example with `Nullable`, inspired by the earlier 
comment from Alexandru Ermicioi:


```D
import std;
auto toNullableInt(string s) {
  try { return s.to!int.nullable; } catch (Exception e) {}
  Nullable!int ret;
  return ret;
}
void main() {
  // prints [2, 4]
  auto a = ["1", "2", "abc", ""];
  a.map!toNullableInt.filter!"!a.isNull".map!"a.get * 2".writeln;

  // prints [2, 4, 198]
  auto b = ["1", "2", "abc", "", "", "99"];
  b.map!toNullableInt.filter!"!a.isNull".map!"a.get * 2".writeln;

  // prints [2, 4, -294967298, 198]
  auto c = ["1", "2", "abc", "", "19", "99"];
  c.map!toNullableInt.filter!"!a.isNull".map!"a.get * 2".writeln;
}
```

Take a look at the `b` array. With this particular design, the 
"" value is going to be silently filtered 
out. If you or your users happen to expect only non-digit string 
literals or empty strings to be filtered out, then there may be a 
very nasty and difficult to debug unexpected surprise awaiting 
down the road.


Also take a look at the `c` array. The handling of arithmetic 
overflows is a safety problem of the D language design. Certain 
types of input may cause overflows, which result in producing 
bogus data as a result of running your program and are very 
difficult to troubleshoot. The use of the GDC's `-ftrapv` option 
surely helps in troubleshooting such cases, but some software or 
D libraries may intentionally rely on the D's arithmetic 
wraparound feature.