difference between tcl list of length one and a scalar? - list

I have a c function (dbread) that reads 'fields' from a 'database'. Most of those fields are single valued; but sometimes they are multi-valued. So I had c code that said
if valcount == 1
return string
else
make list
foreach item in vals
append to list
return list
Because i thought most of the time people want a scalar.
However doing this leads to some odd parsing errors. Specifically if I want to add a value
set l [dbread x] # get current c value
lappend l "extra value" # add a value
dbwrite x {*}$l # set it back to db
If x has single value and that value contains spaces the lappend parses wrong. I get a list with 3 items not 2. I see that this is because it is passed something that is not a list and it parses it to a list and sees 2 items.
set l "foo bar"
lappend l "next val" # string l is parsed into list -> [list foo bar]
so I end up with [list foo bar {next val}]
Anyway, the solution is to make dbread always return a list - even if there is only one item. My question is - is there any downside to this? Are there surprises lurking for the 90% case where people would expect a scalar
The alternative would be to do my own lappend that checks for llength == 1 and special cases it

I think it's cleaner to have an API which always returns a list of results, be it one result or many. Then there's no special casing needed.
No downside, only upside.
Think about it, what if you move away from returning a single scalar and have a case in the future where you're returning a single value that happens to be a string with a space in it. If you didn't construct a list of that single value, you'd treat it as two values (because Tcl would shimmer the string into a list of two things). By always constructing a list of return values, all the code using your API will handle this correctly.
Just because Tcl doesn't have strict typing doesn't mean it's good style to return different types at different times.

One of the approaches I have taken in the past (when the data for each row could contain nulls or empty strings), was to use a list of lists of list:
{{a b} {c d}} ;# two rows, each with two elements
{{{} b} {c d}} ;# two rows, first element of first row is null
;# llength [lindex [lindex {{{} b} {c d}} 0] 0] -> 0
{ { {{}} b } { c d } }
;# two rows, first element of first row is the empty string
;# llength [lindex [lindex {{{{}} b} {c d}} 0] 0] -> 1
It looks complicated, but it's really not if you treat the actual data items as an opaque data structure and add accessors to use it:
foreach row $db_result {
foreach element $row {
if {[db_isnull $element]} {
puts "null"
} elseif {![string length [db_value $element]]} {
puts "empty string"
} else {
puts [db_value $element]
}
}
}
Admittedly, far more complicated than you're looking for, but I thought it worth mentioning.

Related

What is a simple and elegant way to flatten lists or arrays of arbitrary depths? [duplicate]

I was wondering about how I could completely flatten lists and things that contain them. Among other things, I came up with this solution that slips things that have more than one element and puts them back, or takes things with one element after slipping it.
This is a bit different than How do I “flatten” a list of lists in perl 6?, which doesn't completely flat because the task is to restructure.
But, maybe there's a better way.
my #a = 'a', ('b', 'c' );
my #b = ('d',), 'e', 'f', #a;
my #c = 'x', $( 'y', 'z' ), 'w';
my #ab = #a, #b, #c;
say "ab: ", #ab;
my #f = #ab;
#f = gather {
while #f {
#f[0].elems == 1 ??
take #f.shift.Slip
!!
#f.unshift( #f.shift.Slip )
}
}
say "f: ", #f;
This gives:
ab: [[a (b c)] [(d) e f [a (b c)]] [x (y z) w]]
f: [a b c d e f a b c x y z w]
Curiously, I also read some python answers:
Making a flat list out of list of lists in Python
How flatten a list of lists one step
flatten list of lists of lists to a list of lists
itertools.chain(*sublist) look interesting, but the answers were either recursive or limited to two levels from hard-coding. The functional languages were recursive in the source code, but I expected that.
Unfortunately there's no direct built-in that completely flattens a data structure even when sub-lists are wrapped in item containers.
Some possible solutions:
Gather/take
You've already come up with a solution like this, but deepmap can take care of all the tree iteration logic to simplify it. Its callback is called once for every leaf node of the data structure, so using take as the callback means that gather will collect a flat list of the leaf values:
sub reallyflat (+#list) { gather #list.deepmap: *.take }
Custom recursive function
You could use a subroutine like this to recursively slip lists into their parent:
multi reallyflat (#list) { #list.map: { slip reallyflat $_ } }
multi reallyflat (\leaf) { leaf }
Another approach would be to recursively apply <> to sub-lists to free them of any item containers they're wrapped in, and then call flat on the result:
sub reallyflat (+#list) {
flat do for #list {
when Iterable { reallyflat $_<> }
default { $_ }
}
}
Multi-dimensional array indexing
The postcircumfix [ ] operator can be used with a multi-dimensional subscript to get a flat list of leaf nodes up to a certain depth, though unfortunately the "infinite depth" version is not yet implemented:
say #ab[*;*]; # (a (b c) (d) e f [a (b c)] x (y z) w)
say #ab[*;*;*]; # (a b c d e f a (b c) x y z w)
say #ab[*;*;*;*]; # (a b c d e f a b c x y z w)
say #ab[**]; # HyperWhatever in array index not yet implemented. Sorry.
Still, if you know the maximum depth of your data structure this is a viable solution.
Avoiding containerization
The built-in flat function can flatten a deeply nested lists of lists just fine. The problem is just that it doesn't descend into item containers (Scalars). Common sources of unintentional item containers in nested lists are:
An Array (but not List) wraps each of its elements in a fresh item container, no matter if it had one before.
How to avoid: Use Lists of Lists instead of Arrays of Arrays, if you don't need the mutability that Array provides. Binding with := can be used instead of assignment, to store a List in a # variable without turning it into an Array:
my #a := 'a', ('b', 'c' );
my #b := ('d',), 'e', 'f', #a;
say flat #b; # (d e f a b c)
$ variables are item containers.
How to avoid: When storing a list in a $ variable and then inserting it as an element into another list, use <> to decontainerize it. The parent list's container can also be bypassed using | when passing it to flat:
my $a = (3, 4, 5);
my $b = (1, 2, $a<>, 6);
say flat |$b; # (1 2 3 4 5 6)
I'm unaware of a built-in way to do so, though there very well might be (and if not, there probably should be).
The best I could come up with on short notice is this:
gather #ab.deepmap(*.take)
I'm not sure how gather/take interacts with the potentially parallelized evaluation of hyper operators, so the following alternative might not be safe to use, in particular if you care about element order:
gather #ab>>.take
You can put the code into square brackets if you need an array or reify it into a list via .list.
Lastly, this is the first solution rewitten as a retro-style subroutine:
sub deepflat { gather deepmap &take, #_ }

How can I completely flatten a list (of lists (of lists) ... )

I was wondering about how I could completely flatten lists and things that contain them. Among other things, I came up with this solution that slips things that have more than one element and puts them back, or takes things with one element after slipping it.
This is a bit different than How do I “flatten” a list of lists in perl 6?, which doesn't completely flat because the task is to restructure.
But, maybe there's a better way.
my #a = 'a', ('b', 'c' );
my #b = ('d',), 'e', 'f', #a;
my #c = 'x', $( 'y', 'z' ), 'w';
my #ab = #a, #b, #c;
say "ab: ", #ab;
my #f = #ab;
#f = gather {
while #f {
#f[0].elems == 1 ??
take #f.shift.Slip
!!
#f.unshift( #f.shift.Slip )
}
}
say "f: ", #f;
This gives:
ab: [[a (b c)] [(d) e f [a (b c)]] [x (y z) w]]
f: [a b c d e f a b c x y z w]
Curiously, I also read some python answers:
Making a flat list out of list of lists in Python
How flatten a list of lists one step
flatten list of lists of lists to a list of lists
itertools.chain(*sublist) look interesting, but the answers were either recursive or limited to two levels from hard-coding. The functional languages were recursive in the source code, but I expected that.
Unfortunately there's no direct built-in that completely flattens a data structure even when sub-lists are wrapped in item containers.
Some possible solutions:
Gather/take
You've already come up with a solution like this, but deepmap can take care of all the tree iteration logic to simplify it. Its callback is called once for every leaf node of the data structure, so using take as the callback means that gather will collect a flat list of the leaf values:
sub reallyflat (+#list) { gather #list.deepmap: *.take }
Custom recursive function
You could use a subroutine like this to recursively slip lists into their parent:
multi reallyflat (#list) { #list.map: { slip reallyflat $_ } }
multi reallyflat (\leaf) { leaf }
Another approach would be to recursively apply <> to sub-lists to free them of any item containers they're wrapped in, and then call flat on the result:
sub reallyflat (+#list) {
flat do for #list {
when Iterable { reallyflat $_<> }
default { $_ }
}
}
Multi-dimensional array indexing
The postcircumfix [ ] operator can be used with a multi-dimensional subscript to get a flat list of leaf nodes up to a certain depth, though unfortunately the "infinite depth" version is not yet implemented:
say #ab[*;*]; # (a (b c) (d) e f [a (b c)] x (y z) w)
say #ab[*;*;*]; # (a b c d e f a (b c) x y z w)
say #ab[*;*;*;*]; # (a b c d e f a b c x y z w)
say #ab[**]; # HyperWhatever in array index not yet implemented. Sorry.
Still, if you know the maximum depth of your data structure this is a viable solution.
Avoiding containerization
The built-in flat function can flatten a deeply nested lists of lists just fine. The problem is just that it doesn't descend into item containers (Scalars). Common sources of unintentional item containers in nested lists are:
An Array (but not List) wraps each of its elements in a fresh item container, no matter if it had one before.
How to avoid: Use Lists of Lists instead of Arrays of Arrays, if you don't need the mutability that Array provides. Binding with := can be used instead of assignment, to store a List in a # variable without turning it into an Array:
my #a := 'a', ('b', 'c' );
my #b := ('d',), 'e', 'f', #a;
say flat #b; # (d e f a b c)
$ variables are item containers.
How to avoid: When storing a list in a $ variable and then inserting it as an element into another list, use <> to decontainerize it. The parent list's container can also be bypassed using | when passing it to flat:
my $a = (3, 4, 5);
my $b = (1, 2, $a<>, 6);
say flat |$b; # (1 2 3 4 5 6)
I'm unaware of a built-in way to do so, though there very well might be (and if not, there probably should be).
The best I could come up with on short notice is this:
gather #ab.deepmap(*.take)
I'm not sure how gather/take interacts with the potentially parallelized evaluation of hyper operators, so the following alternative might not be safe to use, in particular if you care about element order:
gather #ab>>.take
You can put the code into square brackets if you need an array or reify it into a list via .list.
Lastly, this is the first solution rewitten as a retro-style subroutine:
sub deepflat { gather deepmap &take, #_ }

tcl flattens 3rd level of list, w/o explanation

See code below:
set k [list]
foreach i [list 1 2] {
lappend k [ list "hey" [ list "ho" [ list $i ] ] ]
}
puts [ join $k ",and,"]
exit
The result is:
hey {ho 1},and,hey {ho 2}
But I expected the result to look like:
hey {ho {1}},and,hey {ho {2}}
Any ideas why is that so?
Thanks.
If anyone of the list command's arguments are more than elements one, then only that corresponding indexed element's return value will have the braced list form.
% list a b c; # All list args are having only single element
a b c
% list "a" "b" "c"; # Same as above
a b c
% list {a} {b} {c}; # Same again...
a b c
% list "a b" c d; # Here, 1st arg is having 2 elements.
{a b} c d
%
Tcl's wiki page already mentioned about bizarre behavior of the nested lists in only one case, which is
% list [list [list x]]
x
It means that Tcl lists alone cannot be used to represent ALL kinds of data structures, as Tcl lists magically collapse when it's a series of nested lists with the terminal list having only a single bare word that requires no escaping.
Update :
More importantly, if the arg is having a space in it,
% list "x "
{x }
% list "x"
x
%
Since the space has to be considered as well, Tcl has no other way, but to enclose the braces.

Most efficient way to search a tcl list

i have a tcl list as below.
set mylist [list a b c d e]; # could be more
Now i am doing some processing if the list contains the items "c", "d", "e". But i need to skip that processing if and only if the list has either of the below values:
set mylist [list a];
OR
set mylist [list b];
OR
set mylist [list a b];
So if mylist is any of the above three, i skip the processing. But lets say if the list has any values other than the above three combinations, i do the processing.
What is the most efficient way of searching if the list has any of the three combinations.
I have the basic code which is fulfilling my requirement, but i was looking for more efficient way as i am not much familiar with tcl containers.
set mylist [list a];
if {[llength $mylist] == 2 && ([lindex $mylist 0] eq "a" || [lindex $mylist 0] eq "b") && ([lindex $mylist 1] eq "a" || [lindex $mylist 1] eq "b")} {
puts "1. skip the processing"
} elseif {[llength $mylist] == 1 && ([lindex $mylist 0] eq "a" || [lindex $mylist 0] eq "b")} {
puts "2. skip the processing"
} else {
puts "Do the processing"
}
I was wondering if there is any other efficient way to perform the same.
if {$mylist in {a b {a b}}} {
puts "skip the processing"
}
A list isn't a string, but we can usually compare lists to strings for equality and order. A list with a single element "a" is comparable to the string "a". If you want to know if a given string is equal to any of the lists in the question, the easiest way is to check if the value of the list is a member of the list {a b {a b}}.
Note: This particular solution does not solve all aspects of list equality in general. It works in those cases where it works.
Efficiency
Is it really efficient to compare a list to a string when this will cause automatic, repeated reconstruction of the internal representation of the data ("shimmering"). Actually, it is. If one compares the procedures
proc foo1 mylist {
set a 0
if {$mylist in {a b {a b}}} {set a 92}
return $a
}
proc foo2 mylist {
set a 0
if {$mylist in [list [list a] [list b] [list a b]]} {set a 92}
return $a
}
then foo1 seems to be faster than foo2 (different machines may produce different results).
Constructing a list inside the condition evaluation code does not seem to add very much time. This procedure
proc foo3 mylist {
set a 0
set x [list [list a] [list b] [list a b]]
if {$mylist in $x} {set a 92}
return $a
}
is somewhere in between foo1 and foo2 in speed, but not significantly faster than foo2.
One can also do this by invoking lsearch:
proc foo4 mylist {
set a 0
set x [list [list a] [list b] [list a b]]
if {[lsearch $x $mylist] >= 0} {set a 92}
return $a
}
proc foo5 mylist {
set a 0
set x [list [list a] [list b] [list a b]]
set i [lsearch $x $mylist]
if {$i >= 0} {set a 92}
return $a
}
which is comparable to foo2 and foo3.
(In case it needs to be said, lsearch is more versatile than the in operator, offering e.g. case insensitive lookup, regex lookup, etc. If you need such things, lsearch is the best option.)
I've deleted most of my observations and theories on speed after timing the procedures on another machine, which showed quite different results. foo1 was consistently faster on both machines, though. Since that code is simpler than the other alternatives, I would say this is the way to do it. But to be sure, one needs to time the procedure with one's own machine, whitelist, and code to be performed.
Finally, none of this really matters if I/O occurs inside the procedures, since the I/O will be so much slower than anything else.
Documentation: if, list, lsearch, proc, puts, return, set

How to check a list contains substring from other list using scala?

I have following lists-
A = List(("192.168.20.1", "WinInfra", List("naa.6d867d9c7ac")),
("192.168.20.1", "TriSQLFreshInstall", List("naa.6d867d",
"naa.42704fdc4")),
("192.168.20.1", "redHat7", List("naa.4270cdf",
"naa.427045dc")))
B = List("4270cdf", "427045dc", "42704fdc4")
I want to check if last element of list A (it is a list of strings) contains any substring from list B and get output as unmatched elements only.
Edit: I want to check if any element of list B is exist in list A and collect only such list elements from list A which do not contains list B elements.
I want following output-
List(("192.168.20.1","WinInfra",List( "naa.6d867d9c7ac")))
How do I get above output using scala??
I think something like this:
A.filterNot(a => B.exists(b => a._3.exists(str => str.contains(b))))
or
A.filterNot(a => a._3.exists(str => B.exists(b => str.contains(b))))
or shorter, but less readable
A.filterNot(_._3 exists (B exists _.contains))
First, I wouldn't pass around tuples. It would be a lot easier if you would put this data structure into an object and work with that. However, it would be easier start by finding matches first. So you'll start out by applying a filter on List A:
A.filter { (ip, disc, sublist) => .... }
Where items in your sublist items are in List B:
sublist.exists(sublistItem => b.contains(sublistItem.replaceAll("naa.", "")))
This returns:
res1: List[(String, String, List[String])] = List((192.168.20.1,TriSQLFreshInstall,List(naa.6d867d, naa.42704fdc4)), (192.168.20.1,redHat7,List(naa.4270cdf, naa.427045dc)))
Which is the opposite of what you want. This is easy to correct by saying filterNot:
A.filterNot { (ip, disc, sublist) => sublist.exists(sublistItem => b.contains(sublistItem.replaceAll("naa.", ""))) }