Richard W.M. Jones
2017-Jan-31 11:35 UTC
Re: [Libguestfs] [PATCH v6 2/3] mllib: modify nsplit to take optional noempty and count arguments
On Mon, Jan 30, 2017 at 10:43:15PM +0100, Tomáš Golembiovský wrote:> Added two new optional arguments to nsplit: > > * keep_empty: if set to false empty elements are not stored in the > returned list. The default is to keep the empty elementsThe ?keep_empty flag is pointless. It's simpler and more clear to write: List.filter ((<>) "") (nsplit ...) when you don't want empty elements. However the ?count flag is fine, since that's what Perl's split function also does.> - let rec nsplit sep str > + let rec nsplit ?(keep_empty = true) ?(count = -1) sep str > let len = length str in > let seplen = length sep in > let i = find str sep in > - if i = -1 then [str] > + if i = -1 || count = 0 then > + if str = "" && not keep_empty then [] else [str] > else ( > let s' = sub str 0 i in > let s'' = sub str (i+seplen) (len-i-seplen) in > - s' :: nsplit sep s'' > + let elem, count > + if s' = "" && not keep_empty then > + [], count > + else > + [s'], if count > 0 then count-1 else count > + in > + elem @ nsplit ~keep_empty:keep_empty ~count:count sep s''You can write this line as: elem @ nsplit ~count sep s'' (after dropping keep_empty). Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Richard W.M. Jones
2017-Jan-31 12:13 UTC
Re: [Libguestfs] [PATCH v6 2/3] mllib: modify nsplit to take optional noempty and count arguments
On Tue, Jan 31, 2017 at 11:35:01AM +0000, Richard W.M. Jones wrote:> You can write this line as: > > elem @ nsplit ~count sep s''For why, see: https://www.redhat.com/archives/libguestfs/2017-January/msg00020.html and follow-ups. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
Tomáš Golembiovský
2017-Jan-31 12:17 UTC
Re: [Libguestfs] [PATCH v6 2/3] mllib: modify nsplit to take optional noempty and count arguments
On Tue, 31 Jan 2017 11:35:01 +0000 "Richard W.M. Jones" <rjones@redhat.com> wrote:> On Mon, Jan 30, 2017 at 10:43:15PM +0100, Tomáš Golembiovský wrote: > > Added two new optional arguments to nsplit: > > > > * keep_empty: if set to false empty elements are not stored in the > > returned list. The default is to keep the empty elements > > The ?keep_empty flag is pointless. It's simpler and more > clear to write: > > List.filter ((<>) "") (nsplit ...) > > when you don't want empty elements.That's not entirely true when keep_empty is used in combination with count. When keep_empty is false then the split is not considered a (successful) split and internal state of count is not decreased. Example: # List.filter ((<>) "") (nsplit ~count:3 "," "a,,,b,,,c,,d" ) ;; - : string list = ["a"; "b,,,c,,d"] # nsplit ~keep_empty:false ~count:3 "," "a,,,b,,,c,,d" ;; - : string list = ["a"; "b"; "c"; ",d"]> > However the ?count flag is fine, since that's what Perl's split > function also does. > > > - let rec nsplit sep str > > + let rec nsplit ?(keep_empty = true) ?(count = -1) sep str > > let len = length str in > > let seplen = length sep in > > let i = find str sep in > > - if i = -1 then [str] > > + if i = -1 || count = 0 then > > + if str = "" && not keep_empty then [] else [str] > > else ( > > let s' = sub str 0 i in > > let s'' = sub str (i+seplen) (len-i-seplen) in > > - s' :: nsplit sep s'' > > + let elem, count > > + if s' = "" && not keep_empty then > > + [], count > > + else > > + [s'], if count > 0 then count-1 else count > > + in > > + elem @ nsplit ~keep_empty:keep_empty ~count:count sep s'' > > You can write this line as: > > elem @ nsplit ~count sep s'' > > (after dropping keep_empty). > > Rich. > > -- > Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones > Read my programming and virtualization blog: http://rwmj.wordpress.com > libguestfs lets you edit virtual machines. Supports shell scripting, > bindings from many languages. http://libguestfs.org-- Tomáš Golembiovský <tgolembi@redhat.com>
Richard W.M. Jones
2017-Jan-31 12:24 UTC
Re: [Libguestfs] [PATCH v6 2/3] mllib: modify nsplit to take optional noempty and count arguments
On Tue, Jan 31, 2017 at 01:17:12PM +0100, Tomáš Golembiovský wrote:> On Tue, 31 Jan 2017 11:35:01 +0000 > "Richard W.M. Jones" <rjones@redhat.com> wrote: > > > On Mon, Jan 30, 2017 at 10:43:15PM +0100, Tomáš Golembiovský wrote: > > > Added two new optional arguments to nsplit: > > > > > > * keep_empty: if set to false empty elements are not stored in the > > > returned list. The default is to keep the empty elements > > > > The ?keep_empty flag is pointless. It's simpler and more > > clear to write: > > > > List.filter ((<>) "") (nsplit ...) > > > > when you don't want empty elements. > > That's not entirely true when keep_empty is used in combination with > count. When keep_empty is false then the split is not considered a > (successful) split and internal state of count is not decreased. > > Example: > > # List.filter ((<>) "") (nsplit ~count:3 "," "a,,,b,,,c,,d" ) ;; > - : string list = ["a"; "b,,,c,,d"] > > # nsplit ~keep_empty:false ~count:3 "," "a,,,b,,,c,,d" ;; > - : string list = ["a"; "b"; "c"; ",d"]IMHO that shows how ?keep_empty is a very complex interface that's difficult for ordinary programmers to understand. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Maybe Matching Threads
- Re: [PATCH v6 2/3] mllib: modify nsplit to take optional noempty and count arguments
- Re: [PATCH v4 4/6] mllib: modify nsplit to take optional noempty and count arguments
- Re: [PATCH v6 2/3] mllib: modify nsplit to take optional noempty and count arguments
- [PATCH v6 2/3] mllib: modify nsplit to take optional noempty and count arguments
- Re: [PATCH v3 4/6] mllib: modify nsplit to take optional noempty and count arguments