Tech Is Hard

Credibility = Talent x Years of experience + Proven hardcore accomplishment

Tag Archives: rebol

Difference ‘tween skip and pick

REBOL series are the foundation of all data structures, so there are a lot of functions to deal with them. Often, under a given circumstance, more than one will do what’s needed. In the REBOL core series documentation, there’s sort of a fluid discussion of functions like first and next. And in another section, a comparison between first/second/third/etc and pick. I was changing values in a series and encountered the distinction between pick and skip. Some functions, like first and pick, are extraction functions, giving the series item as its own thing, while others, like next and skip, are in the context of the series. Often it might not matter, but due to the general applicability of the change function to all types of series!, it did for me. I want to change the first item in a:

>> a: copy ["abc" 123]
== ["abc" 123]
>> a/1
== "abc"
>> change a/1 "b"
== "bc"
>> a
== ["bbc" 123]
>> change pick a 1 "c"
== "bc"
>> a
== ["cbc" 123]
>> change first a "d"
== "bc"
>> a
== ["dbc" 123]
>> change head a "e"
== [123]
>> a
== ["e" 123]

Ah! Until I used head (or next or skip or back), I am referencing the first item (originally “abc”), but the change function sees change string! string!. Some of the items I need to change are blocks I need to append to:

>> a: copy ["abc" [123]]
== ["abc" [123]]
>> append skip a 1 'foobar
== ["abc" [123] foobar]

Oops, that’s not it. Get rid of that with remove.

>> remove last a
** Script Error: remove expected series argument of type: series port bitset none
** Where: halt-view
** Near: remove last a

Same issue here. Last is giving the word! foobar which isn’t something remove can operate on.

>> remove back tail a
== []
>> a
== ["abc" [123]]
>> append last a 'foobar
== [123 foobar]

There it is. Any of the “extraction” functions will do for the append:

>> a
== ["abc" [123 foobar]]
>> append pick a 2 'foo
== [123 foobar foo]
>> append a/2 'bar
== [123 foobar foo bar]
>> a
== ["abc" [123 foobar foo bar]]
%d bloggers like this: