Erlang: how to spawn a node and link it to a mnesia table - tuples

I have to implement tuple spaces using mnesia in a distibuted and cuncurrent environment. The interface provides these functions:
new(name): creates a new TS (a new mnesia table) named with name
in(TS, Pattern): returns a tuple matching the pattern in the TS and deletes it from the TS. It blocks if there is no tuple matching
rd(TS, Pattern)r: eturns a tuple matching the pattern in the TS, but the tuple remains in the TS.
It blocks if there is no tuple matching
out(TS, Tuple): puts the tuple Tuple in the TS
in(TS, Pattern, Timeout): ss in(TS, Pattern) but returns after Timetout returns {ok,Tuple} or {err, timeout}
rd(TS, Pattern, Timeout): as rd(TS,Pattern) but return after Timout returns {ok,Tuple} or {err, timeout}
addNode(TS, Node): adds the Node to the TS, so Node can access to all the tuples of TS
removeNode(TS,Node): removes a node from the TS
nodes(TS): tells the nodes on which the TS is visible/replicated
I have no idea (at all) on how how to implement the addNode. Is it possible to use something like this?
register(node_name,spawn_link(fun()-> mnesia:add_table_copy(tab_name, node_name, disc_copies) end)

Related

ocaml remove duplicates in a list

Hey guys I am having a bit of trouble i have a general idea what the code should be but not to sure what to do from here.
The question is: Define a function remove_elts taking a list of items as well as a list of items to
remove from the first list, and returning a list with all occurrences of the items
to be removed gone.
My code is :
let remove_elts l reml =
match l with
[] -> false
| hd :: tl -> if hd = l then reml
else hd :: remove_elts l tl;;
Any filtering function, i.e., a function that takes a container and returns another container that contains elements of the input container that satisfy some property, is implemented using the following algorithm:
output-sequence = []
foreach element in the input-sequeunce:
if element satisfies condition:
output-sequence := output-sequence + element
This iteration has two elements which differ with each step, the element variable that takes in order the elements of the input-sequence and the output-sequence that grows every time, the <element> satisfies the condition.
In functional programming, iteration is commonly represented with recursion which is more natural from the mathematical point of view. Anything that changes in the iteration will become a parameter of the recursive function, and each step is represented as a recursive call with the new values of the variables. So the same iteration in the functional style (pseudocode)
filter input-sequence output-sequence =
if is-empty input-sequence then output-sequence
else
let element = first-element-of input-sequence in
let rest = drop-first-element input-sequence in
if element satisfies condition
then filter rest (output-sequence + element)
else filter rest output-sequence
And the iteration is called as
filter input-sequence []
So it is a little bit more verbose then the foreach but this is because foreach is basically a syntactic sugar.
Now, your task is to implement this in OCaml, keeping in mind two things:
1) In OCaml you can use pattern matching, instead of is-empty,first-element-of, and drop-first-element primitives.
2) You're using a singly-linked list as the sequence, so appending an element to the end of it is very expensive, it is O(N) (as you have to go through all elements, until you reach the end), and doing this in cycle will make your algorithm O(N^2), so instead you should prepend to the beginning (which is O(1)) and at the end of recursion, reverse the list.

What is this syntax: template tplname{op(id,[id2])}(params)

In the json module:
template simpleGetOrDefault*{`{}`(node, [key])}(node: JsonNode, key: string): JsonNode = node.getOrDefault(key)
What's up with the curly braces (and what's that in them) ?
This is an example of a "term-rewriting macro".
A bit earlier in the json module, you'll find the definition of the {} operator with the following signature:
proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =
## Traverses the node and gets the given value. If any of the
## keys do not exist, returns ``nil``. Also returns ``nil`` if one of the
## intermediate data structures is not an object.
The goal of the term-rewriting macro is to intercept the case where only a single string is given as argument to the operator and to turn this into a simple call to getOrDefault.

How drools to iterate over a list

I'm new to Drools6.4.0.FINAL and want to use it to iterate over a list of items and process my business logic
my business data return List ,I want insert it into KieSession
List<MyObject> list = service.queryList(Map<String,Object> param);
kSession.insert(list);
kSession.fireAllRules();
my drl file like this :
import java.util.List;
import xxx.xxx.MyObject;
rule "rule 1"
salience 1
activation-group "ctoc_order_rule"
when
$mo:MyObject(orgunitid_lev1 == 58094);
then
$mo.setBusiness_type_id(201);
$mo.setBusiness_type_name("business201");
update($mo);
end
But this not fire my rules,How can I do to fire a List ? thanks
If you want to match a java.util.List, insert a List and write a pattern List(...).
If you want to match a xxx.xxx.MyObject, insert a MyObject and write a pattern MyObject(...).
If you insert a List and have pattern MyObject(...), it cannot match. It would be possible to match a List and extract the elements (using from), but matching (generic) container classes is somewhat of an anti-pattern. What if you have several kinds of List containing various object?

difference between [] and list() in python3

I thought that [] and list() were two equal ways to create a list. But if you want a list with dictionnary keys,
var = [a_dict.keys()]
doesn't work since type(var) is [dict_keys], correct syntax is :
var = list(a_dict.keys())
I couldn't find an good explanation on this behaviour. Do you have one ?
TL;DR:
list() is the same as []
list(obj) is not the same as [obj]
a_dict.keys() is a dictionary view object, it returns an object which can be iterated to yield the keys of a_dict. So this line:
[a_dict.keys()]
is saying in python "I'm making a list with one element in it" and that one element is the dict keys iterator. It's a list literal in the syntax.
Now this line:
list(a_dict.keys())
is a call to the list builtin function. This function list attempts to iterate the argument and produce a list. It's a function call in the grammar.
The equivalent list literal (actually list comprehension) would be instead:
[key for key in a_dict.keys()]
Finally, note that dictionary objects iterate by keys anyway,
list(a_dict.keys()) would usually be written more simply as as list(a_dict) instead.
Hope this helps.
[a_dict.keys()]
This one puts a single element in the list. Just as if you were to write [1]. In this case that one element is going to be a list.
list(a_dict.keys())
The constructor accepts a sequence and will add all elements of the sequence to the container.

list of lists to either autovivify or recursive data structure

I have a list of lists in the format below. This is data coming from a csv and I am trying to emulate the data review function that excel has in python. The only reason I can't do it directly in excel is this document is almost 1GB and has 1.1 mil row.
((a1,b1,c1,d1,e1),(a1,b2,c1,d2,e2),(a1,b1,c2,d3,e3),(a2,b1,c1,d3,e4),(a2,b2,c2,d3,e5)...)
I want to convert it into a single data structure something like a multidimensional array. like below
((a1:(b1:(c1:(),c2:()),b2:(),b3:()),a2:(b1:(c1:()),b2:(c2:()),b3:())))
I use autovivify class for other purposes but I can't use it here because some of the keys I want to use are strings. Appreciate help here.
If I understand your question correctly, you want to transform that list into a tree-like structure, where each tuple in the list represents one path down the tree. You can do this using nested dictionaries:
def add_to_dict(d, t):
if t:
first, rest = t[0], t[1:]
nested = d.setdefault(first, {})
add_to_dict(nested, rest)
Given a dictionary d (initially empty) and one of those tuples t, if that tuple is not empty, it takes the first element from the tuple, adds a nested dictionary to the original dictionary using this element as key (or takes one that already exists in this place), and adds the rest of the tuple to that dictionary in the same way.
Example using your data:
data = (('a1','b1','c1','d1','e1'),
('a1','b2','c1','d2','e2'),
('a1','b1','c2','d3','e3'),
('a2','b1','c1','d3','e4'),
('a2','b2','c2','d3','e5'))
d = {}
for t in data:
add_to_dict(d, t)
The resulting dictionary d looks like this:
{'a1': {'b1': {'c1': {'d1': {'e1': {}}},
'c2': {'d3': {'e3': {}}}},
'b2': {'c1': {'d2': {'e2': {}}}}},
'a2': {'b1': {'c1': {'d3': {'e4': {}}}},
'b2': {'c2': {'d3': {'e5': {}}}}}}