Opening menu in a MultiColumnList in Spec gives MNU - menuitem

In Pharo 3 right-clicking in a menu using a MultiColumnListModel gives a MessageNotUnderstood, as shown in this example code:
| specModel list itemMenu group1 item1 |
itemMenu := MenuModel new.
item1 := MenuItemModel new
name: 'Browse';
shortcut: $b command;
enabled: true;
action: [ self halt ];
yourself.
group1 := MenuGroupModel new
addMenuItem: item1;
autoRefresh: true;
yourself.
itemMenu addMenuGroup: group1.
specModel := DynamicComposableModel new
instantiateModels: #(list MultiColumnListModel);
yourself.
list := specModel list
items: {$a. $b. $c. $d. $f.};
displayBlock: [:e | {e asString. e isVowel asString} ];
menu: itemMenu;
yourself.
specModel
openWithSpecLayout: (SpecLayout composed
newRow: [: r | r add: #list ];
yourself).
Is this a bug in Spec or I am missing something?

That is a well-known current bug in Spec. There are several places where right-clicks MNU

Related

Define a closure variable in buckle script

I'm trying to convert the following ES6 script to bucklescript and I cannot for the life of me figure out how to create a "closure" in bucklescript
import {Socket, Presence} from "phoenix"
let socket = new Socket("/socket", {
params: {user_id: window.location.search.split("=")[1]}
})
let channel = socket.channel("room:lobby", {})
let presence = new Presence(channel)
function renderOnlineUsers(presence) {
let response = ""
presence.list((id, {metas: [first, ...rest]}) => {
let count = rest.length + 1
response += `<br>${id} (count: ${count})</br>`
})
document.querySelector("main[role=main]").innerHTML = response
}
socket.connect()
presence.onSync(() => renderOnlineUsers(presence))
channel.join()
the part I cant figure out specifically is let response = "" (or var in this case as bucklescript always uses vars):
function renderOnlineUsers(presence) {
let response = ""
presence.list((id, {metas: [first, ...rest]}) => {
let count = rest.length + 1
response += `<br>${id} (count: ${count})</br>`
})
document.querySelector("main[role=main]").innerHTML = response
}
the closest I've gotten so far excludes the result declaration
...
...
let onPresenceSync ev =
let result = "" in
let listFunc = [%raw begin
{|
(id, {metas: [first, ...rest]}) => {
let count = rest.length + 1
result += `${id} (count: ${count})\n`
}
|}
end
] in
let _ =
presence |. listPresence (listFunc) in
[%raw {| console.log(result) |} ]
...
...
compiles to:
function onPresenceSync(ev) {
var listFunc = (
(id, {metas: [first, ...rest]}) => {
let count = rest.length + 1
result += `${id} (count: ${count})\n`
}
);
presence.list(listFunc);
return ( console.log(result) );
}
result is removed as an optimization beacuse it is considered unused. It is generally not a good idea to use raw code that depends on code generated by BuckleScript, as there's quite a few surprises you can encounter in the generated code.
It is also not a great idea to mutate variables considered immutable by the compiler, as it will perform optimizations based on the assumption that the value will never change.
The simplest fix here is to just replace [%raw {| console.log(result) |} ] with Js.log result, but it might be enlightening to see how listFunc could be written in OCaml:
let onPresenceSync ev =
let result = ref "" in
let listFunc = fun [#bs] id item ->
let count = Js.Array.length item##meta in
result := {j|$id (count: $count)\n|j}
in
let _ = presence |. (listPresence listFunc) in
Js.log !result
Note that result is now a ref cell, which is how you specify a mutable variable in OCaml. ref cells are updated using := and the value it contains is retrieved using !. Note also the [#bs] annotation used to specify an uncurried function needed on functions passed to external higher-order functions. And the string interpolation syntax used: {j| ... |j}

Creating Dictionary from nth list items

I have a list in which each item in the list is further split into 3 fields, separated by a '| '
Suppose my list is :
[‘North America | 23 | United States’, ’South America | 12 | Brazil’,
‘Europe | 51 | Greece’………] and so on
Using this list, I want to create a dictionary that would make the first field in each item the value, and the second field in each item the key.
How can I add these list items to a dictionary using a for loop?
My expected outcome would be
{’23’:’North America’, ’12’:’South America’, ’51’:’Europe’}
How about something like this:
var myList = new List<string>() { "North America | 23 | United States", "South America | 12 | Brazil", "Europe | 51 | Greece" };
var myDict = myList.Select(x => x.Split('|')).ToDictionary(a => a[1], a => a[0]);
Assuming you're in Python, if you know what the delimiter is, you can use string.split() to break the string up into a list then go from there.
my_dict = {}
for val in my_list:
words = val.split(" | ")
my_dict[words[1]] = words[0]
For other languages, you can take an index of the first "|", substring from the beginning to give you your value, then take the index of the second line to give you the key. In Java this would look like:
Map<String, String> myDict = new DopeDataStructure<String, String>();
for(String s : myArray){
int pos = s.indexOf("|");
String val = s.substring(0, pos - 1);
String rest = s.substring(pos + 2);
String key = rest.substring(0, rest.indexOf("|") - 1);
myDict.put(key, val);
}
EDIT: there very well could be more efficient ways of solving the problem in other languages, that's just the simplest method I know off the top of my head
Using Python dict comphrehension
data = ['North America | 23 | United States', 'South America | 12 | Brazil',]
# Spliting each string in list by "|" and setting its 1st index value to dict key and 0th index value to dict value
res = {i.split(" | ")[1]: i.split(" | ")[0] for i in data}
print (res)
I hope this helps and counts!

Slick: Is there a way to create a WHERE clause with a regex?

I look for a slick equivalient to
select * from users where last_name ~* '[\w]*son';
So for example when having the following names in the database:
first_name | last_name
----------------------
Tore | Isakson
John | Smith
Solveig | Larsson
Marc | Finnigan
The result would be
first_name | last_name
----------------------
Tore | Isakson
Solveig | Larsson
My current solution is to interpolate this with an SQLActionBuilder like
val pattern = "[\\w]*son"
val action = sql""" SELECT * FROM users WHERE last_name ~* ${pattern}; """.as[User]
But this is not the way I would like to have it. I would prefer something like
users.filter(_.last_name matchRegex "[\\w]*son") // <- This does not exist
In case it is relevant: I use Postgres.
(This answer is following the question in Slick: How can I combine a SQL LIKE statement with a SQL IN statement)
Although Slick doesn't support the ~* operator out of the box, you can add it yourself. That would give you a way to execute the query using the lifted embedded style of Slick query.
To do that, you can use the SimpleExpression builder. There's not much documentation on it, but the jumping off point would be the Scalar Database Functions page of the reference manual.
What we want to do is write a method along these lines:
def find(names: Seq[String]): DBIO[Seq[String]] = {
val pattern = names.mkString("|")
users.filter(_.lastName regexLike pattern).map(_.lastName).result
}
To get regexLike we can use a enrich (enhance, "pimp") a string column to have the regexLike method:
implicit class RegexLikeOps(s: Rep[String]) {
def regexLike(p: Rep[String]): Rep[Boolean] = {
val expr = SimpleExpression.binary[String,String,Boolean] { (s, p, qb) =>
qb.expr(s)
qb.sqlBuilder += " ~* "
qb.expr(p)
}
expr.apply(s,p)
}
}
The implicit class part is allow the compiler to construct the RegexLikeOps class anytime it has a Rep[String] that calls a method that Rep[String] doesn't already have (i.e., when regexLike is asked for).
Our regexLike method takes another Rep[String] argument as the pattern, and then uses SimpleExpression builder to safely construct the SQL we want to use.
Putting it all together we can write:
val program = for {
_ <- users.schema.create
_ <- users ++= User("foo") :: User("baz") :: User("bar") :: Nil
result <- find( Seq("baz","bar") )
} yield result
println( Await.result(db.run(program), 2.seconds) )
The SQL generated (in my test with H2) is:
select "last_name" from "app_user" where "last_name" ~* 'baz|bar'
The full code is: https://github.com/d6y/so46199828

Select RDF collection/list and iterate result with Jena

For some RDF like this:
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:blah="http://www.something.org/stuff#">
<rdf:Description rdf:about="http://www.something.org/stuff/some_entity1">
<blah:stringid>string1</blah:stringid>
<blah:uid>1</blah:uid>
<blah:myitems rdf:parseType="Collection">
<blah:myitem>
<blah:myitemvalue1>7</blah:myitemvalue1>
<blah:myitemvalue2>8</blah:myitemvalue2>
</blah:myitem>
...
<blah:myitem>
<blah:myitemvalue1>7</blah:myitemvalue1>
<blah:myitemvalue2>8</blah:myitemvalue2>
</blah:myitem>
</blah:myitems>
</rdf:Description>
<rdf:Description rdf:about="http://www.something.org/stuff/some__other_entity2">
<blah:stringid>string2</blah:stringid>
<blah:uid>2</blah:uid>
<blah:myitems rdf:parseType="Collection">
<blah:myitem>
<blah:myitemvalue1>7</blah:myitemvalue1>
<blah:myitemvalue2>8</blah:myitemvalue2>
</blah:myitem>
....
<blah:myitem>
<blah:myitemvalue1>7</blah:myitemvalue1>
<blah:myitemvalue2>8</blah:myitemvalue2>
</blah:myitem>
</blah:myitems>
</rdf:Description>
</rdf:RDF>
I'm using Jena/SPARQL and I'd like to be able to use a SELECT query to retrieve the myitems node for an entity with a particular stringid and then extract it from the resultset and iterate through and get the values for each myitem nodes. Order isn't important.
So I have two questions:
Do I need to specify in my query that blah:myitems is a list?
How can I parse a list in a ResultSet?
Selecting Lists (and Elements) in SPARQL
Let's address the SPARQL issue first. I've modified your data just a little bit so that the elements have different values, so it will be easier to see them in the output. Here's the data in N3 format, which is a bit more concise, especially when representing lists:
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix blah: <http://www.something.org/stuff#> .
<http://www.something.org/stuff/some_entity1>
blah:myitems ([ a blah:myitem ;
blah:myitemvalue1 "1" ;
blah:myitemvalue2 "2"
] [ a blah:myitem ;
blah:myitemvalue1 "3" ;
blah:myitemvalue2 "4"
]) ;
blah:stringid "string1" ;
blah:uid "1" .
<http://www.something.org/stuff/some__other_entity2>
blah:myitems ([ a blah:myitem ;
blah:myitemvalue1 "5" ;
blah:myitemvalue2 "6"
] [ a blah:myitem ;
blah:myitemvalue1 "7" ;
blah:myitemvalue2 "8"
]) ;
blah:stringid "string2" ;
blah:uid "2" .
You mentioned in the question selecting the myitems node, but myitems is actually the property that relates the entity to the list. You can select properties in SPARQL, but I'm guessing that you actually want to select the head of the list, i.e., the value of the myitems property. That's straightforward. You don't need to specify that it's an rdf:List, but if the value of myitems could also be a non-list, then you should specify that you're only looking for rdf:Lists. (For developing the SPARQL queries, I'll just run them using Jena's ARQ command line tools, because we can move them to the Java code easily enough afterward.)
prefix blah: <http://www.something.org/stuff#>
select ?list where {
[] blah:myitems ?list .
}
$ arq --data data.n3 --query items.sparql
--------
| list |
========
| _:b0 |
| _:b1 |
--------
The heads of the lists are blank nodes, so this is the sort of result that we're expecting. From these results, you could get the resource from a result set and then start walking down the list, but since you don't care about the order of the nodes in the list, you might as well just select them in the SPARQL query, and then iterate through the result set, getting each item. It also seems likely that you might be interested in the entity whose items you're retrieving, so that's in this query too.
prefix blah: <http://www.something.org/stuff#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?entity ?list ?item ?value1 ?value2 where {
?entity blah:myitems ?list .
?list rdf:rest* [ rdf:first ?item ] .
?item a blah:myitem ;
blah:myitemvalue1 ?value1 ;
blah:myitemvalue2 ?value2 .
}
order by ?entity ?list
$ arq --data data.n3 --query items.sparql
----------------------------------------------------------------------------------------
| entity | list | item | value1 | value2 |
========================================================================================
| <http://www.something.org/stuff/some__other_entity2> | _:b0 | _:b1 | "7" | "8" |
| <http://www.something.org/stuff/some__other_entity2> | _:b0 | _:b2 | "5" | "6" |
| <http://www.something.org/stuff/some_entity1> | _:b3 | _:b4 | "3" | "4" |
| <http://www.something.org/stuff/some_entity1> | _:b3 | _:b5 | "1" | "2" |
----------------------------------------------------------------------------------------
By ordering the results by entity and by list (in case some entity has multiple values for the myitems property), you can iterate through the result set and be assured of getting, in order, all the elements in a list for an entity. Since your question was about lists in result sets, and not about how to work with result sets, I'll assume that iterating through the results isn't a problem.
Working with Lists in Jena
The following example shows how you can work with lists in Java. The first part of the code is just the boilerplate to load the model and run the SPARQL query. Once you're getting the results of the query back, you can either treat the resource as the head of a linked list and use the rdf:first and rdf:rest properties to iterate manually, or you can cast the resource to Jena's RDFList and get an iterator out of it.
import java.io.IOException;
import java.io.InputStream;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.RDF;
public class SPARQLListExample {
public static void main(String[] args) throws IOException {
// Create a model and load the data
Model model = ModelFactory.createDefaultModel();
try ( InputStream in = SPARQLListExample.class.getClassLoader().getResourceAsStream( "SPARQLListExampleData.rdf" ) ) {
model.read( in, null );
}
String blah = "http://www.something.org/stuff#";
Property myitemvalue1 = model.createProperty( blah + "myitemvalue1" );
Property myitemvalue2 = model.createProperty( blah + "myitemvalue2" );
// Run the SPARQL query and get some results
String getItemsLists = "" +
"prefix blah: <http://www.something.org/stuff#>\n" +
"\n" +
"select ?list where {\n" +
" [] blah:myitems ?list .\n" +
"}";
ResultSet results = QueryExecutionFactory.create( getItemsLists, model ).execSelect();
// For each solution in the result set
while ( results.hasNext() ) {
QuerySolution qs = results.next();
Resource list = qs.getResource( "list" ).asResource();
// Once you've got the head of the list, you can either process it manually
// as a linked list, using RDF.first to get elements and RDF.rest to get
// the rest of the list...
for ( Resource curr = list;
!RDF.nil.equals( curr );
curr = curr.getRequiredProperty( RDF.rest ).getObject().asResource() ) {
Resource item = curr.getRequiredProperty( RDF.first ).getObject().asResource();
RDFNode value1 = item.getRequiredProperty( myitemvalue1 ).getObject();
RDFNode value2 = item.getRequiredProperty( myitemvalue2 ).getObject();
System.out.println( item+" has:\n\tvalue1: "+value1+"\n\tvalue2: "+value2 );
}
// ...or you can make it into a Jena RDFList that can give you an iterator
RDFList rdfList = list.as( RDFList.class );
ExtendedIterator<RDFNode> items = rdfList.iterator();
while ( items.hasNext() ) {
Resource item = items.next().asResource();
RDFNode value1 = item.getRequiredProperty( myitemvalue1 ).getObject();
RDFNode value2 = item.getRequiredProperty( myitemvalue2 ).getObject();
System.out.println( item+" has:\n\tvalue1: "+value1+"\n\tvalue2: "+value2 );
}
}
}
}

Mathematica - StringMatch Elements Within a List?

I have a functions that returns cases from a table that match specific strings.
Once I get all the cases that match those strings, I need to search each case (which is its own list) for specific strings and do a Which command. But all I know how to do is turn the whole big list of lists into one string, and then I only get one result (when I need a result for each case).
UC#EncodeTable;
EncodeTable[id_?PersonnelQ, f___] :=
Cases[#,
x_List /;
MemberQ[x,
s_String /;
StringMatchQ[
s, ("*ah*" | "*bh*" | "*gh*" | "*kf*" |
"*mn*"), IgnoreCase -> True]], {1}] &#
Cases[MemoizeTable["PersonnelTable.txt"], {_, id, __}]
That function is returning cases from the table
Which[(StringMatchQ[
ToString#
EncodeTable[11282], ("*bh*" | "*ah*" |
"*gh*" ), IgnoreCase -> True]) == True, 1,
(StringMatchQ[
ToString#
EncodeTable[11282], ("*bh*" | "*ah*" |
"*gh*" ), IgnoreCase -> True]) == False, 0]
That function is SUPPOSED to return a 1 or 0 for each case returned by the first function, but I don't know how to search within lists without making them all one string and return a result for each list.
Well, you probaby want Map, but it's hard to say without seeing what the structure of the data to be operated upon is. Perhaps you can provide an example.
EDIT: In the comment, an example result was given as
dat = {{204424, 11111, SQLDateTime[{1989, 4, 4, 0, 0, 0.}], Null,
"Parthom, Mary, MP", Null, 4147,
"T-00010 AH BH UI", {"T-00010 AH BH UI", "M-14007 LL GG",
"F-Y3710 AH LL UI GG"}, "REMOVED."}, {2040, 11111,
SQLDateTime[{1989, 4, 13, 0, 1, 0.}], Null, "KEVIN, Stevens, STK",
Null, 81238,
"T-00010 ah gh mn", {"T-00010 mn", "M-00100 dd", "P-02320 sd",
"M-14003 ed", "T-Y8800 kf", "kj"}}};
(actually the example had a syntax error so I fixed it in what I hope is the right way).
Now, if I define a function
func = Which[(StringMatchQ[#[[8]], ("*bh*" | "*ah*" | "*gh*"),
IgnoreCase -> True]) == True, 1, True, 0] &;
(note the second condition to be matched may be written as True, see the documentation of Which) which does this
func[dat[[1]]]
(*
-> 1
*)
(note that I've slightly changed func from what you have, in order for it to do what I assume you wanted it to actually do). This can then be applied to dat, of which the elements have the form you gave, as follows:
Map[func, dat]
(*
-> {1, 1}
*)
I'm not sure if this is what you want, I did my best guessing.
EDIT2: In response to the comment about the position of the element to be matched being variable, here is one way:
ClearAll[funcel]
funcel[p_String] :=
Which[StringMatchQ[p, ("*bh*" | "*ah*" | "*gh*"),
IgnoreCase -> True], 1, True, 0];
funcel[___] := 0;
ClearAll[func];
func[lst_List] := Which[MemberQ[Map[funcel, lst], 1], 1, True, 0]
so that
Map[func, dat]
gives {1,1}