How to append a new data block with new-line in red? - rebol

I tried:
data: [a b c]
new-line tail data true
append data [d e f]
I get
[a b c d e f]
not what I expect:
[a b c
d e f]

Newline marker is a property of a value slot, not of a series. new-line tail data true didn't set this marker, because tail of a series does not contain any value slot (but back tail does).
>> head new-line tail [a b c] on
== [a b c]
>> head new-line back tail [a b c] on
== [a b
c
]
>> append [a b c] new-line [d e f] on
== [a b c
d e f
]

Related

How to evaluate refinement of a function when calling this function in Red/Rebol

>> f: func [x /a][either a [x + 2] [x + 1]]
== func [x /a][either a [x + 2] [x + 1]]
>> b: /a
== /a
>> f/b 1
*** Script Error: f has no refinement called b
*** Where: f
*** Stack: f
>> f/:b 1
*** Script Error: f has no refinement called :b
*** Where: f
*** Stack: f
You can see that the function f has a refinement a, and I bind /a to b. When calling f with its refinement /a by b, it fails.
What is the correct way to pass a refinement which needs to be evaluated before to its function? Or, is there a way to convert a path! to function!?
Refinements are limited, in a way that they can be passed only by being listed literally in a function call. On the other hand, you can construct such function call (a path! value followed by all the arguments) whichever way you want:
>> b: 'a
== a
>> do probe reduce [to path! reduce ['f b] 1]
[f/a 1]
== 3
Note that elements of the path in this case are word!s, not refinement!s. In general, such use-case is covered by apply. However, only Rebol has it natively, and with an awkward calling convention:
>> f: func [x /a][either a [x + 2] [x + 1]]
>> b: yes
== true
>> apply :f [1 b]
== 3
>> apply :f [1 no]
== 2
You can easily write your own high-level version of it if that's what you need.

copy/part with pair in REBOL 3

help copy has the following to say about copy:
USAGE:
COPY value /part length /deep /types kinds
DESCRIPTION:
Copies a series, object, or other value.
COPY is an action value.
ARGUMENTS:
value -- At position (series! port! map! object! bitset! any-function!)
REFINEMENTS:
/part -- Limits to a given length or position
length (number! series! pair!)
/deep -- Also copies series values within the block
/types -- What datatypes to copy
kinds (typeset! datatype!)
The /part refinement can take a number!, series! or pair!. I have not been able to get pair! to work. (I haven't tried series! yet.) Is this not implemented? If it is, how does it work?
The /part pair! refinement works with images. The pair relates to the x/y coordinates as in
>> img: load %image.png
== make image! [519x391 #{
1D2F9F1D2F9F1C2E9E1C2E9E1B2D9D1B2D9D1B2D9D1B2D9D1D2F9F1C2E9E
1A2C9C192B9B192B9B1A2C9C1B2D9D1C2E9E1D2EA01...
>> copy/part img 2x2
== make image! [2x2 #{
1D2F9F1D2F9F1D2F9F1D2F9F
}]
REBOL/View Image Datatype
And here an example how /part series! is working
>> s: [a b c d e f g]
== [a b c d e f g]
>> ser: skip s 3
== [d e f g]
>> copy/part s ser
== [a b c]

How to iterate over the product of several ranges or iterators?

Is there a natural way in Rust to iterate over the "product" of several ranges or iterators?
This comes up when you're iterating over a multidimensional array, or perhaps some state space. For instance, I want to consider all possible values of a boolean tuple with 5 elements. Nesting 5 for loops is a bit unwieldy.
Here is a macro that does the job:
macro_rules! product {
($first:ident, $($next:ident),*) => (
$first.iter() $(
.flat_map(|e| std::iter::repeat(e)
.zip($next.iter()))
)*
);
}
fn main() {
let a = ['A', 'B', 'C'];
let b = [1, 4];
let c = [true, false];
let d = ['x', 'y'];
for (((a, b), c), d) in product![a, b, c, d] {
println!("{} {} {} {}", a, b, c, d);
}
}
Output:
A 1 true x
A 1 true y
A 1 false x
A 1 false y
A 4 true x
A 4 true y
etc...
Playpen example
The macro expands to the following
a.iter()
.flat_map(|e| std::iter::repeat(e).zip(b.iter()))
.flat_map(|e| std::iter::repeat(e).zip(c.iter()))
.flat_map(|e| std::iter::repeat(e).zip(d.iter()))
flat_map(|e| ... ) combines a sequence of iterators into an iterator. The e is an element yielded by an iterator.
std::iter::repeat(e) creates an iterator that repeats e.
.zip( ... ) iterates over two iterators simultaneously, yielding the elements of both as a pair.
Macros are a bit longer to explain, so it's best to read the macro chapter in the book
The itertools crate has a very ergonomic macro (iproduct!) for iterating over the product of iterators. Here is an example:
pub fn main() {
let a = ['A', 'B', 'C'];
let b = [1, 4];
let c = [true, false];
let d = ['x', 'y'];
for (a, b, c, d) in itertools::iproduct!(&a, &b, &c, &d) {
println!("{} {} {} {}", a, b, c, d);
}
}

variables/arrays from tcl procedure

How could i pass some variables/ arrays outside of procedure?
Lets say I've my procedure 'myproc' with inputparameters {a b c d e}, e.g.
myproc {a b c d e} {
... do something
(calculate arrays, lists and new variables)
}
Inside this procedure I want to calculate an array phiN(1),phiN(2),...phiN(18) out of the variables a-e which itself is a list, e.g.
set phiN(1) [list 1 2 3 4 5 6 7 8 9];
(lets say the values 1-9 had been calculated out of the input variables a-e). And I want to calculate some other parameter alpha and beta
set alpha [expr a+b];
set beta [expr c+d];
Anyway no I want to pass these new calculated variables outside of my procedure. Compare to matlab I simply would write sg like to get these variables outside of the 'function'.
[phiN,alpha,beta] = myproc{a b c d e}
Has anybody an idea how I can deal in tcl?? Thanks!
There are several options:
Return a list and use lassign outside
Example:
proc myproc {a b c d e} {
set alpha [expr {$a+$b}]
set beta [expr {$c+$d}]
return [list $alpha $beta]
}
lassign [myproc 1 2 3 4 5] alpha beta
This is fine if you return values, but not arrays.
Use upvar and provide the name of the array/variable as argument
Example:
proc myproc {phiNVar a b c d e} {
upvar 1 $phiNVar phiN
# Now use phiN as local variable
set phiN(1) [list 1 2 3 4 5 6 7 8 9]
}
# Usage
myproc foo 1 2 3 4 5
foreach i $foo(1) {
puts $i
}
Use a combination of both
Example:
proc myproc {phiNVar a b c d e} {
uplevel 1 $phiNVar phiN
set alpha [expr {$a+$b}]
set beta [expr {$c+$d}]
set phiN(1) [list 1 2 3 4 5 6 7 8 9]
return [list $alpha $beta]
}
lassign [myproc bar 1 2 3 4 5] alpha beta
foreach i $bar(1) {
puts $i
}
Edit: As Donal suggested, is is also possible to return a dict:
A dict is a Tcl list where the odd elements are the keys and the even elements are the values. You can convert an array to a dict with array get and convert a dict back to an array with array set. You can also use the dict itself.
Example
proc myproc {a b c d e} {
set alpha [expr {$a+$b}]
set beta [expr {$c+$d}]
set phiN(1) [list 1 2 3 4 5 6 7 8 9]
return [list [array get phiN] $alpha $beta]
}
lassign [myproc 1 2 3 4 5] phiNDict alpha beta
array set bar $phiNDict
foreach i $bar(1) {
puts $i
}
# Use the [dict] command to manipulate the dict directly
puts [dict get $phiNDict 1]
For more ideas (this is about arrays, but could apply to values as well) see this wiki entry.

How to obtain a minimal key from functional dependencies?

I need some help and guidelines.
I have the following relation: R = {A, B, C, D, E, F} and the set of functional dependencies
F = {
{AB -> C};
{A -> D};
{D -> AE};
{E -> F};
}
What is the primary key for R ?
If i apply inference rules i get these additional Function dependencies:
D -> A
D -> E
D -> F
D -> AEF
A -> E
A -> F
A -> DEF
How do I continue?
There is a well known algorithm to do this. I don't remember it, but the excercise seems to be simple enough not to use it.
I think this is all about transitivity:
CurrentKey = {A, B, C, D, E, F}
You know D determines E and E determines F. Hence, D determines F by transitivity. As F doesn't determine anything, we can remove it and as E can be obtained from D we can remove it as well:
CurrentKey = {A, B, C, D}
As AB determines C and C doesn't determine anything we know it can't be part of the key, so we remove it:
CurrentKey = {A, B, D}
Finally we know A determines D so we can remove the latter from the key:
CurrentKey = {A, B}
If once you have this possible key, you can recreate all functional dependencies it is a possible key.
PS: If you happen to have the algorithm handy, please post it as I'd be glad to re-learn that :)
Algorithm: Key computation (call with x = βˆ…)
procedure key(x;A;F)
foreach ! B 2 F do
if x and B 2 x and B ΜΈ2 then
return; /* x not minimal */
fi
od
if x+ = A then
print x; /* found a minimal key x */
else
X any element of A τ€€€ x+;
key(x [ fXg;A;F);
foreach ! X 2 F do
key(x [ ;A;F);
od
fi