copy/part with pair in REBOL 3 - rebol

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]

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.

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

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
]

Convert string elements in series to floats in Red language

I want to convert many decimal numbers available as strings to floats in one go. I am trying following code which combines these strings into a series and then convert them to floats. This works all right but fails if there is an error:
a: "1.5"
b: ""
c: "3.7"
invars: [a b c]
print a
print type? a
set invars foreach x invars [append [] to-float reduce x] ; code to convert string series to float series;
print a
print type? a
The error is:
*** Script Error: cannot MAKE/TO float! from: ""
*** Where: to
*** Stack: to-float
For error correction, I tried following code:
temp: []
foreach x invars [
y: copy ""
either error? [set [y] to-float reduce x]
[append temp reduce x] ; put original value if not convertable
[append temp reduce y] ]
print temp
set invars temp
print a
print type? a
But this is also not working. Where is the problem and how can this be corrected?
forall invars [invars/1: load get invars/1]
>> invars
== [1.5 [] 3.7]
and if you want to get rid of the empty blocks
>> replace/all invars block! 0
== [1.5 0 3.7]
and if you really want to do all in (:less:) steps
forall invars [invars/1: either empty? invars/1: get invars/1 [0.0] [load invars/1]]
After you can set your variables again.
But if you want just set your variables, you have to do
foreach x invars [set :x load get x]
with to-float
foreach x invars [either empty? get x [set :x 0] [set :x to-float get x]]
and finally an all error safe version with to-float
foreach x invars [attempt [set :x to-float get x]]
== 3.7
>> a
== 1.5
>> b
== ""
>> c
== 3.7

How to properly initialize a pointer to a double value within a struct?

I'm trying to get a pointer to a double value within a struct that I can pass to a routine, but having trouble getting any value other than 0. The following has worked for other struct! values I've used (such as one containing only binary!), but not in this case:
Rebol []
speed1: make struct! [
d [double]
] [0.0]
speed1*-struct: make struct! [i [int]] third speed1
speed1*: speed1*-struct/i
Here is the evaluation:
>> speed1: make struct! [
[ d [double]
[ ] [0.0]
>> speed1*-struct: make struct! [i [int]] third speed1
>> speed1*: speed1*-struct/i
== 0
Here is the working equivalent with a binary! struct:
>> binary: make struct! [bytes [binary!]] reduce [head insert/dup copy #{} 0 8]
>> binary*-struct: make struct! [i [int]] third binary
>> binary*: binary*-struct/i
== 39654648
I can see the difference in the third function:
>> third speed1
== #{0000000000000840}
>> third binary
== #{F8145D02}
However, I am not really sure what the difference in length means in this case. What am I doing wrong? Is there a different way to pass pointers to a decimal value?
Here is a way to get a pointer to a double value:
speed1-struct: make struct! [s [struct! [d [double]]]] [0.0]
speed1*-struct: make struct! [i [int]] third speed1-struct
speed1*: speed1*-struct/i
And the evaluation showing it returns a pointer (shown as int):
>> speed1-struct: make struct! [s [struct! [d [double]]]] [0.0]
>> speed1*-struct: make struct! [i [int]] third speed1-struct
>> speed1*: speed1*-struct/i
== 37329176

Set Difference Operation between an Object Body and a Block definition in Rebol

I want to be able to modify Object dynamically by adding / removing properties or methods on the fly. For Adding no problem, for Removing I thought about using Set Difference Math Operator but it behaves weirdly as far as I can see when removing a method from the object.
For example if I have
O: make object! [
a: 1
f: func [][]
b: 1
]
I can substract [a: 1 b: 1] with no problem
>> difference third O [b: 1 a: 1]
== [f: func [][]]
But I cannot substract f: func[][]:
>> difference third O [f: func[][]]
== [a: 1 b: func [][] func []]
>>
Output is weird (I put strange maybe it doesn't sound english as I'm not english native :) )
Why and what should I do instead ?
Thanks.
Issue #1: Difference Discards Duplicates From Both Inputs
Firstly, difference shouldn't be thought of as a "subtraction" operator. It gives you one of each element that is unique in each block:
>> difference [1 1 2 2] [2 2 2 3 3 3]
== [1 3]
>> difference [2 2 2 3 3 3] [1 1 2 2]
== [3 1]
So you'd get an equivalent set by differencing with [a: 1 b: 1] and [1 a: b:]. This is why the second 1 is missing from your final output. Even differencing with the empty set will remove any duplicate items:
>> difference [a: 1 b: 1] []
== [a: 1 b:]
If you're looking to actually search and replace a known sequential pattern, then what you want is more likely replace with your replacement as the empty set:
>> replace [a: 1 b: 1] [b: 1] []
== [a: 1]
Issue #2: Function Equality Is Based On Identity
Two separate functions with the same definition will evaluate to two distinct function objects. For instance, these two functions both take no parameters and have no body, but when you use a get-word! to fetch them and compare they are not equal:
>> foo: func [] []
>> bar: func [] []
>> :foo == :bar
== false
So another factor in your odd result is that f: is being subtracted out of the set, and the two (different) empty functions are unique and thus both members of the differenced set.
R2 is a little weirder than R3 and I can't get :o/f to work. But the following is a way to get an ''artificially correct-looking version'' of the difference you are trying to achieve:
>> foo: func [] []
>> o: make object! [a: 1 f: :foo b: 2]
>> difference third o compose [f: (:foo)]
== [a: 1 b: 2]
Here you're using the same function identity that you put in the object in the block you are subtracting.
In R3, difference does not support function values in this way. It may relate to the underlying implementation being based on map! which cannot have ''function values'' as keys. Also in Rebol 3, using difference on an object is not legal. So even your first case won't work. :(
Issue #3: This isn't how to add and remove properties
In Rebol 3 you can add properties to an object dynamically with no problems.
>> obj: object [a: 1]
== make object! [
a: 1
]
>> append obj [b: 2]
== make object! [
a: 1
b: 2
]
But as far as I know of, you cannot remove them once they have been added. You can set them to none of course, but the reflection APIs will still report them as being there.
If you want to make trying to read them throw an error you can set it to an error object and then protect them from reads. A variant of this also works in R2:
>> attempt [obj/b: to-error "invalid member"]
== none
>> probe obj
== make object! [
a: 1
b: make error! [
code: 800
type: 'User
id: 'message
arg1: "invalid member"
arg2: none
arg3: none
near: none
where: none
]
]
>> obj/b
** User error: "invalid member"
R3 takes this one step further and lets you protect the member from writes, and even hide the member from having any new bindings made to it.
>> protect 'obj/b
== obj/b
>> obj/b: 100
** Script error: protected variable - cannot modify: b
>> protect/hide 'obj/b
== obj/b
>> obj
== make object! [
a: 1
]
If you need to dynamically add and remove members in R2, you might also consider a data member in your object which is a block. Blocks and objects are interchangeable for many operations, e.g:
>> data: [a: 1 b: 2]
== [a: 1 b: 2]
>> data/a
== 1
>> data/b
== 2
And you can remove things from them...
>> remove/part (find data (to-set-word 'a)) 2
== [b: 2]
It all depends on your application. The main thing object! has going over block! is the ability to serve as a context for binding words...
You cannot dynamically add or remove words from an object in Rebol 2. If you wish to simulate this behaviour you need to create and return a new object.