Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

"piping" functions

 [1/15] from: jf_allie::hotmail::com at: 22-Nov-2005 23:42


Hello everyone, I wrote these two functions BUTFIRST and BUTLAST (as in LOGO) butlast: func [series] [ copy/part num (length? num) - 1] butfirst: func [series] [ copy/part next num (length? num) - 1] It seemed to work: >>num: [1 2 3 4] == [1 2 3 4]
>>butfirst num
== [2 3 4]
>>butlast num
== [1 2 3] ... but unfortunately, when I try to compose functions with them as in:
>>butfirst butlast num
== [2 3 4]
>>butlast butfirst num
== [1 2 3] I'm very curious to understand why. many thanks jf

 [2/15] from: volker:nitsch:g:mail at: 23-Nov-2005 0:50


On 11/23/05, Jean-Francois Allie <jf_allie-hotmail.com> wrote:
> Hello everyone, I wrote these two functions BUTFIRST and BUTLAST (as in > LOGO) butlast: func [series] [
<<quoted lines omitted: 11>>
> >>butlast butfirst num > == [1 2 3] I'm very curious to understand why. many thanks jf
Good question, confused me too. Until i discovered you wrote: butlast: func [series] [copy/part num (length? num) - 1] In rebol 'series is the arguement, 'num is the global.
> -- > To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject. >
-- -Volker Any problem in computer science can be solved with another layer of indirection. But that usually will create another problem. David Wheeler

 [3/15] from: jf_allie:h:otmail at: 23-Nov-2005 1:02


Thanks Volker, It's proof I can't see straight anymore and that I should go home. Seems to work now. butlast: func [series] [ copy/part series (length? series) - 1] butfirst: func [series] [ copy/part next series (length? series) - 1]
>>num: [1 2 3 4]
== [1 2 3 4]
>>butlast num
== [1 2 3]
>> butfirst num
== [2 3 4]
>>butfirst butlast num
== [2 3]
>> butlast butfirst num
== [2 3] By the way, are there more Rebolish, efficient or elegant way's to do it?

 [4/15] from: compkarori:g:mail at: 23-Nov-2005 2:12


butfirst: func [ series ][ copy next ] is shorter .. so short, one wonders if it needs to be a function :) On 11/23/05, Jean-Francois Allie <jf_allie-hotmail.com> wrote:
> Thanks Volker, > It's proof I can't see straight anymore and that I should go home.
<<quoted lines omitted: 17>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- Graham Chiu

 [5/15] from: tom:conlin::gmail at: 22-Nov-2005 17:59


to make them more flexible you might add a number to skip butfirst: func [series length] [copy skip series length] butlast: func [series length] [copy/part series (length? series) - length]
>> s: [1 2 3 4 5 6 7]
== [1 2 3 4 5 6 7]
>> butlast butfirst s 2 2
== [3 4 5] On 11/22/05, Graham Chiu <compkarori-gmail.com> wrote:
> butfirst: func [ series ][ > copy next
<<quoted lines omitted: 39>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- ... nice weather eh

 [6/15] from: jf_allie::hotmail::com at: 23-Nov-2005 22:28


Thanks Graham, it is very short indeed! butfirst: func [series][ copy next series ] Your version made me realise I had misinterpreted the effect of the "index" functions like "back" and "next". You see I thought they returned the index position, when in fact, they return the whole series but only show its valuesstarting at the position of the index. I think a sliding window would be a better metaphor then the arrow pointing at the position. Unfortunately, this "sliding window" model breaks down in the following function where "back tail series" seems to return an index. But when you check the type of "back tail series" it answers "block". Is it a case where we will need both the wave and the particule models to understand the phenomenon? What's going on? many thanks butlast: func [series][ copy/part series back tail series ]
>>num: [1 2 3 4]
== [1 2 3 4]
>>butlast num
== [1 2 3]

 [7/15] from: compkarori:gma:il at: 24-Nov-2005 2:35


back tail series returns the series one back from the end so, you should be looking for butlast: func [series][ copy/part series index? back tail series ] On 11/24/05, Jean-Francois Allie <jf_allie-hotmail.com> wrote:
> Thanks Graham, it is very short indeed! > butfirst: func [series][
<<quoted lines omitted: 21>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- Graham Chiu

 [8/15] from: gabriele:colellachiara at: 23-Nov-2005 23:44


Hi Graham, On Wednesday, November 23, 2005, 11:41:55 PM, you wrote: GC> "back tail series" returns the series one back from the end GC> so, you should be looking for GC> butlast: func [series][ GC> copy/part series index? back tail series GC> ] Actually, INDEX? is not needed.
>> b: [1 2 3 4]
== [1 2 3 4]
>> copy/part b back tail b
== [1 2 3] Regards, Gabriele. -- Gabriele Santilli <gabriele-rebol.com> --- http://www.rebol.com/ Colella Chiara software division --- http://www.colellachiara.com/

 [9/15] from: jf_allie::hotmail at: 24-Nov-2005 1:42


Graham, GC> "back tail series" returns the series one back from the end My understanding is that your statement isn't exactly correct since the wholeseries is in fact returned even if you only see it starting from the INDEX position.
>>b: [1 2 3 4]
== [1 2 3 4]
>>c: back tail b
== [4]
>>head? c
== false
>>index? c
== 4
>>c: head c
== [1 2 3 4]
>>index? c
== 1 ... but if you do a copy then you do end up with a series that actualy startsat the INDEX.
>>d: copy back tail b
== [4]
>>head? d
== true
>>index? d
== 1 This isn't to be nitpicking, but to understand these functions. Right now, with what I know, BACK TAIL B in this function:
>>COPY/PART B BACK TAIL B
doesn't return the same thing as in:
>>BACK TAIL B
why? Gabriele,
>>Actually, INDEX? is not needed. >>>>b: [1 2 3 4]
<<quoted lines omitted: 3>>
>>Regards, >>Gabriele.
Yes precisely, but how come this function works without INDEX? as in Graham'sversion? cheers, JF

 [10/15] from: volker::nitsch::gmail::com at: 24-Nov-2005 3:12


On 11/24/05, Jean-Francois Allie <jf_allie-hotmail.com> wrote:
> > Graham, > > GC> "back tail series" returns the series one back from the end > > My understanding is that your statement isn't exactly correct since the > wholeseries is in fact returned even if you only see it starting from the > INDEX position. >
Graham said "the series", not "a copy of the series". He could be more verbose, "a reference to the series at the position one back from the end", but it means the same.
> >>b: [1 2 3 4] > == [1 2 3 4]
<<quoted lines omitted: 22>>
> >>BACK TAIL B > why?
Hard to understand what you do not understand, i hope i do it: The focus is here copy/part B back tail b not here: copy/part b BACK TAIL B copy B return a copy of the series on position b. copy/part B back tail b returns a copy of the series on the position b, but not upto end, but upto back tail b
> Gabriele, > >>Actually, INDEX? is not needed.
<<quoted lines omitted: 11>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- -Volker Any problem in computer science can be solved with another layer of indirection. But that usually will create another problem. David Wheeler

 [11/15] from: jf_allie:h:otmail at: 24-Nov-2005 3:45


Volker,
>>Hard to understand what you do not understand, i hope i do it: >>The focus is here
<<quoted lines omitted: 6>>
>>returns a copy of the series on the position b, but not upto end, but upto >>back tail b
... yes , and what does the last BACK TAIL B evaluates to? an INDEX position? Simply restated, I would like to understand why BACK TAIL B used alone like here:
>>BACK TAIL B
returns a series while when used in:
>>copy/part b BACK TAIL B
it returns a number. (I assume it returns a number because this is what COPY/PART needs as a second argument) Many thanks, JF

 [12/15] from: Izkata::Comcast::net at: 23-Nov-2005 22:09


> Volker, >>>Hard to understand what you do not understand, i hope i do it:
<<quoted lines omitted: 19>>
> it returns a number. (I assume it returns a number because this is what > COPY/PART needs as a second argument)
It doesn't return a number -
>> ? copy
USAGE: COPY value /part range /deep DESCRIPTION: Returns a copy of a value. COPY is an action value. ARGUMENTS: value -- Usually a series (Type: series port bitset) REFINEMENTS: /part -- Limits to a given length or position. range -- (Type: number series port pair) <-- /deep -- Also copies series values within the block. It returns a the same series at a different index. The error message that copy/part gives isn't completely correct... count (below) gives the impression that it needs to be a number, but it doesn't.
>> copy/part {a b c} {b} ;It looks like this should work...
** Script Error: Invalid /part count: b ** Near: copy/part "a b c" "b"
>> X: "a b c"
== "a b c"
>> copy/part X find X {b} ;But what it wants is part of the same series
== "a "

 [13/15] from: gabriele::colellachiara::com at: 24-Nov-2005 12:40


Hi Jean-Francois, On Thursday, November 24, 2005, 2:42:04 AM, you wrote:
>>>COPY/PART B BACK TAIL B
JFA> doesn't return the same thing as in:
>>>BACK TAIL B
JFA> why? Because a SERIES holds a sequence of values (rebol values, characters, and so on), AND a position in the sequence.
>> b: [1 2 3 4]
== [1 2 3 4]
>> mold/all b
== "[1 2 3 4]"
>> c: next b
== [2 3 4]
>> mold/all c
== "#[block![1 2 3 4]2]" C and B share the same sequence of values, but they have a different position. COPY, instead, creates a new sequence copying from the source series. I'll find many discussions on this list about series... :) Regards, Gabriele. -- Gabriele Santilli <gabriele-rebol.com> --- http://www.rebol.com/ Colella Chiara software division --- http://www.colellachiara.com/

 [14/15] from: jf_allie::hotmail::com at: 24-Nov-2005 22:04

Copy/part series series (WAS :"piping" functions)


Thanks everyone for your help, I think I got it now. Here is a note explaining how I understand it at this point:
>>? copy
USAGE: COPY value /part range /deep DESCRIPTION: Returns a copy of a value. COPY is an action value. ARGUMENTS: value -- Usually a series (Type: series port bitset) REFINEMENTS: /part -- Limits to a given length or position. range -- (Type: number series** port pair) <-- /deep -- Also copies series values within the block. Notes: ** A SERIES holds a sequence of values (rebol values, characters, and so on), AND a position in the sequence. When copying part of a series, the RANGE argument can be this same series. COPY/PART will then copy starting at the INDEX position of the first series and up to the INDEX position of the second series. In pictures: 1st argument a[b c d e] tail index-at-2 INDEX position ^ 2nd argument a b c d[e] tail index-at-5 INDEX position ^ COPY/PART 1st-arg 2nd-arg a[b c d e] tail -> [b c d] tail index-at-1 ^ ^ example B:
>>a: [1 2 3 4 5]
== [1 2 3 4 5]
>>butlast: func [series][
[ copy/part series back tail series] ==[1 2 3 4]
>>
example B:
>>life: [10 20 30 40 50 60 70]
== [10 20 30 40 50 60 70]
>>party: func [life n] [
[ copy/part skip life n skip tail life negate n]
>>party life 1
== [20 30 40 50 60]
>>party life 2
== [30 40 50]
>>party life 3
== [40]

 [15/15] from: antonr::lexicon::net at: 25-Nov-2005 14:49

Re: "piping" functions


Look at the help for COPY. ? copy There you can see: /part -- Limits to a given length or position. range -- (Type: number series port pair) When you use INDEX?, you get a number, and COPY uses that as a length. When you use a series directly, COPY uses its index to specify the position of the end of the copy. In other words: - A number specifies a length, so the end position is always relative to the start position. - A series specifies an absolute ending position. Anton.

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted