Re: Procs that work only inplace like strutils.removeSuffix()

2019-08-05 Thread mratsim
That is something quite general and a decision in the end of library authors 
(including standard library).

In my case, I try to design a "low-level" API that works completely in-place, 
this has the side-benefits of allowing the return value to be an error code, nd 
also a high-level API that returns a new value (and uses exception).

The low-level is important because in scientific computing, allocating is often 
a bottleneck and you want to avoid it if you can. The high-level is because 
`let c = a + b` is much nicer than any in-place alternative that would require 
pre-allocation.

One approach that was really nice was Lua's which allow chaining of methods 
that had in-place results: 
[http://lua-users.org/wiki/MethodChainingWrapper](http://lua-users.org/wiki/MethodChainingWrapper)


Re: Procs that work only inplace like strutils.removeSuffix()

2019-08-05 Thread Stefan_Salewski
> is what you want?

Obviously not really -- strutils.removeSuffix() was only an example for some 
Nim procs which works only in place, while user may need a proc that returns a 
modified value. (English is not my native language, but after rereading my 
initial post, I think that my description was not too bad. But maybe I can 
write my next post more clearly :-)

But I think Araq has understood the core of my concern.


Re: Procs that work only inplace like strutils.removeSuffix()

2019-08-05 Thread treeform
Maybe proc changeFileExt(filename, ext: string): string {...} ( 
[https://nim-lang.org/docs/os.html#changeFileExt%2Cstring%2Cstring](https://nim-lang.org/docs/os.html#changeFileExt%2Cstring%2Cstring)
 ) is what you want?


Re: Procs that work only inplace like strutils.removeSuffix()

2019-08-04 Thread SolitudeSF
i wrote macros 
([https://github.com/SolitudeSF/imageman/commit/c29c13fb570785891f7b5458e2b70e69ae773d80](https://github.com/SolitudeSF/imageman/commit/c29c13fb570785891f7b5458e2b70e69ae773d80))
 that generate mutating/nonmutating versions of procedures specifically for 
this reason 


Re: Procs that work only inplace like strutils.removeSuffix()

2019-08-04 Thread Araq
That's actually something I intended to have language support for but your 
template solution is nice and should become part of the stdlib, IMO.


Re: Procs that work only inplace like strutils.removeSuffix()

2019-08-03 Thread juancarlospaco
I kind of agree, have you seen `os.splitPath().tail` ?, but I dont really 
understand whats the question/problem if any... 樂


Procs that work only inplace like strutils.removeSuffix()

2019-08-03 Thread Stefan_Salewski
Often that may be OK, but not always. For removeSuffix() I have used 
strutils.replace() sometimes as a substitute, but that is not always the best 
solution.

So I was just thinking about other solutions. An apply template seems to work:


import strutils

template apply(x: typed; f: typed; par: typed): untyped =
  var h = x
  f(h, par)
  h

const
  Name = "manual.txt"
  Doc = Name.replace(".txt") & ".html"
  #Doc2 = Name.removeSuffix(".txt")
  Doc3 = apply(Name, removeSuffix, ".txt")
  Doc4 = Name.apply(removeSuffix, ".txt")

echo Doc
echo Doc3
echo Doc4


Run


manual.html
manual
manual


Run

Maybe there are even better solutions?

(Of course I understand why removeSuffix() works in place, while replace not: 
The code of replace needs a temporary string as buffer, as the replacement can 
be larger, so it makes sense to return that buffer. removeSuffix() can work in 
place, so it does this. But sometimes a proc with result like suffixRemoved() 
is what one wants.)