.Q.trp and bt handling - kdb

I saw (in .Q.fpn) the following technique to parse and show the bt object passed to .Q.trp function:
q)f:{x+1}
q).Q.trp[f;`a;{'(x;y)}]
'type
[4] f:{x+1}
^
[3] (.Q.trp)
[2] .Q.trp[f;`a;{'(x;y)}]
^
[0] .Q.trp[f;`a;{'(x;y)}]
^
'(x;y) seems like an exception building construction, - but Kx documentation says that there are only two ways of exception building: from symbol and from string. It is looks like we can built an exception from a list of (symbol; bt object).
So what the construction '(x;y) stands for?
Can we build something different than exception with '(x;y)?

My guess is that this is a specific signal recently allowed along with the addition of the .Q.trp/.Q.bt functionality. It looks like it works only for (symbol;bt object) or (string;bt object), anything else is unrecognized.
q).Q.trp[{1+x};`a;{'(x;y;1)}]
'stype
The output can be stored if returned without the signal:
q)r:.Q.trp[{1+x};`a;{(x;y)}]
and this type of signal seems to work in any context, not just within .Q.trp:
q)'("other";last r)
'other
[2] {1+x}
^
[1] (.Q.trp)
[0] r:.Q.trp[{1+x};`a;{(x;y)}]
I suspect the last r has a very specific format/shape that one could fabricate but it seems like an unnecessary use-case.
Bonus oddities:
This works:
q)'("other";())
'other
[0] '("other";())
^
but other things I've tried show up weird errors:
q)'("other";(();()))
pl0
pl0
q)
q)'("other";"abc")
srr

Related

How to encode normalized(A,B) properly?

I am using clingo to solve a homework problem and stumbled upon something I can't explain:
normalized(0,0).
normalized(A,1) :-
A != 0.
normalized(10).
In my opinion, normalized should be 0 when the first parameter is 0 or 1 in every other case.
Running clingo on that, however, produces the following:
test.pl:2:1-3:12: error: unsafe variables in:
normalized(A,1):-[#inc_base];A!=0.
test.pl:2:12-13: note: 'A' is unsafe
Why is A unsafe here?
According to Programming with CLINGO
Some error messages say that the program
has “unsafe variables.” Such a message usually indicates that the head of one of
the rules includes a variable that does not occur in its body; stable models of such
programs may be infinite.
But in this example A is present in the body.
Will clingo produce an infinite set consisting of answers for all numbers here?
I tried adding number(_) around the first parameter and pattern matching on it to avoid this situation but with the same result:
normalized(number(0),0).
normalized(A,1) :-
A=number(B),
B != 0.
normalized(number(10)).
How would I write normalized properly?
With "variables occuring in the body" actually means in a positive literal in the body. I can recommend the official guide: https://github.com/potassco/guide/releases/
The second thing, ASP is not prolog. Your rules get grounded, i.e. each first order variable is replaced with its domain. In your case A has no domain.
What would be the expected outcome of your program ?
normalized(12351,1).
normalized(my_mom,1).
would all be valid replacements for A so you create an infinite program. This is why 'A' has to be bounded by a domain. For example:
dom(a). dom(b). dom(c). dom(100).
normalized(0,0).
normalized(A,1) :- dom(A).
would produce
normalize(0,0).
normalize(a,1).
normalize(b,1).
normalize(c,1).
normalize(100,1).
Also note that there is no such thing as number/1. ASP is a typefree language.
Also,
normalized(10).
is a different predicate with only one parameter, I do not know how this will fit in your program.
Maybe your are looking for something like this:
dom(1..100).
normalize(0,0).
normalize(X,1) :- dom(X).
foo(43).
bar(Y) :- normalize(X,Y), foo(X).

What is causing the error between these two .bindPopup?

The below popup works properly displaying "Neigh_Name" (a name such as "Main") and "2020 Total Population" (a string comprised of numbers '234273' etc).
layer.bindPopup(feature.properties.Neigh_Name+"<br>"+feature.properties["2020 Total Population"])
However, when using the below..
layer.bindPopup(feature.properties["2020 Total Population"])
I get the error: Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
Can someone explain what is happening in the backend and why these two lines of code differ and the latter ultimately fails? I'm trying to learn why certain things occur to avoid future issues.
Thanks!
Very probably the bindPopup method behaves unexpectedly when passing a number as content (your 2nd line), whereas it happily accepts a string argument (as in your 1st line).
Simply make sure to convert your argument into string, e.g. with "" + feature.properties["2020 Total Population"] or
`${feature.properties["2020 Total Population"]}`

Elixir Ecto where Query Throwing `Protocol not implemented Error`

I have what I believe to be a relatively simple Ecto query in Elixir and it is throwing an error "protocol not implemented for" and I am very confused as to why.
So here is the query:
head_children_list = Comments
|> where([c], c.inserted_at in ^head_children)
|> Enum.sort_by(& &1["votetotal"])
So head_children is a list with one string timestamp value in it. This is the confirmed terminal output:
value of head_children
["2018-10-11 14:08:15.021033"]
So I know for a fact that this is a list. However when I try and perform the above query I get the following:
[info] Sent 500 in 168ms
[error] #PID<0.401.0> running AlbatrossWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: POST /voteComment
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Enumerable not implemented for #Ecto.Query<from c in Albatross.Comments, where: c.inserted_at in ^[["2018-10-11 14:08:15.021033"]]>. This protocol is implemented for: DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Postgrex.Stream, Range, Stream
Notice that this says protocol is implemented for lists. But this is a list! So I am very confused. I have tried using ^[head_children], which just wraps it in another list, but this also fails with the same error.
Does anyone know what is going on?
where(Comments, [c], c.inserted_at in ^head_children) returns an %Ecto.Query{}, which is piped into Enum.sort_by/2. %Ecto.Query{} is a struct not a list, which is not Enumerable.
You probably want
head_children_list =
Comments
|> where([c], c.inserted_at in ^head_children)
|> Repo.all()
|> Enum.sort_by(& &1["votetotal"])

Function which takes a string as parameter

In kdb I would like to have a function which takes a string as a parameter.
myfunc: {[strParam]
....
}
However when I tried to invoke the function.
q) myfunc["test"]
It complains of length error. It seems that function sees "test" as passing 4 char parameters. How can I make the function expect a string?
A string in kdb is defined as a list of characters and so functions using them have to be able to deal with this.
q)count "test"
4
You can also use a symbol instead casting from a string using `symbol$"test". A symbol is atomic and fixed width so can be useful to use as keys to a dictionary or in a table. Some functions for strings will still work on symbols e.g
q)upper `test
`TEST
while list operation will not work and you will have to turn it back into a string using string `test before using those operations.
When a length error is thrown and you go into the debug mode as shown by the q prompt having two brackets q)), you can use the functions .z.ex to show the failed function and .z.ey to see the failed arguments to narrow down which part is throwing the error.
The error can appear due to multiple reasons.
q)f:{[p] show p} //it works fine with any number of elements
q)f["abc"]
"abc"
f:{[p] enlist[`k]!p} //needs a list of single element
f["abc"]
'length
f[enlist "abc"]
(enlist `k)!enlist "abc"
q)f:{[p] 1 2 3 4!p} //the length of values is different from the length of keys
q)f["abc"]
'length
q)f["abcd"]
(1j, 2j, 3j, 4j)!"abcd"

Problem with the deprecation of the postgresql XML2 module 'xml_is_well_formed' function

We need to make extensive use of the 'xml_is_well_formed' function provided by the XML2 module.
Yet the documentation says that the xml2 module will be deprecated since "XML syntax checking and XPath queries"
is covered by the XML-related functionality based on the SQL/XML standard in the core server from PostgreSQL 8.3 onwards.
However, the core function XMLPARSE does not provide equivalent functionality since when it detects an invalid XML document,
it throws an error rather than returning a truth value (which is what we need and currently have with the 'xml_is_well_formed' function).
For example:
select xml_is_well_formed('<br></br2>');
xml_is_well_formed
--------------------
f
(1 row)
select XMLPARSE( DOCUMENT '<br></br2>' );
ERROR: invalid XML document
DETAIL: Entity: line 1: parser error : expected '>'
<br></br2>
^
Entity: line 1: parser error : Extra content at the end of the document
<br></br2>
^
Is there some way to use the new, core XML functionality to simply return a truth value
in the way that we need?.
Thanks,
-- Mike Berrow
After asking about this on the pgsql-hackers e-mail list, I am happy to report that the guys there agreed that it was still needed and they have now moved this function to the core.
See:
http://web.archiveorange.com/archive/v/alpsnGpFlZa76Oz8DjLs
and
http://postgresql.1045698.n5.nabble.com/review-xml-is-well-formed-td2258322.html