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

[REBOL] Re: Cunning use of [], () & {} in R# and in future? Rebol versions?

From: greggirwin:mindspring at: 18-Oct-2003 13:02

Max et al, I agree that something more concise than "raw REBOL" (copy/part at...) would be great, but the slice notation as used in Python and Ruby isn't grabbing me--not for general use anyway. MOA> I still prefer this type of syntax: MOA> blk/[1 3:5] MOA> it is clear, and uses the least typing each space defines a block to MOA> extract it also looks most like spreadsheets syntax when specifying MOA> ranges. ... MOA> blk/[ 5: ~10 ] ; everything starting at 5 but not the tenth element. It may be clear to programmers that are familiar with that notation, but I'm not sure it would be clear to normal people (spreadsheet people are in-between and would understand the basic range notation, but would have to learn any extensions to it). The n:m notation would conflict with time! values as well. General clarity aside, my gut reaction to the path/block/slice notation isn't positive. I don't think it buys us anything in the long run, and it could have a pretty serious impact on things if included as a base syntactical change (particularly if it doesn't fit within REBOL's current lexicon). MOA> because the colon is already used, maybe we could simply use another MOA> character: If you "simply use another character", then you: a) lose what I see as the most positive aspect of it--that it's a known range notation for some people, and b) fall into a "what fits" approach, rather than designing for clear meaning and intent. Now, since I disagree, it's only fair that I offer up a solution. :) At the end of this message is a function called EXCERPT, that uses a simple dialect. I originally called it CUT (as in the *nix utility that does the same kind of thing), which also maps closely to the slice concept in other languages. The problem I saw was that we're not really cutting anything out, we're copying things out, so it isn't really an accurate name (IIRC, Python and Ruby have different semantics for slicing). COPY, SELECT, and PICK are already taken, as is EXTRACT; though EXTRACT is a mezzanine that we could enhance, which might be a better bet. EXCERPT implies the correct meaning to me, but it isn't grabbing me as a great word so far. :) Let me know what you think. -- Gregg ; The dialect allows you to use commas in the block, but how they ; are interpreted is not how you might think. Coming after a number, ; they are a valid lexical form, but they denote a decimal! rather ; than being seen as a separator, which means you can't use them too ; flexibly. excerpt: func [ {Returns the specified items and/or ranges from the series.} series [series!] offsets [block!] {Offsets of the items to extract; dialected.} /only "return sub-block ranges as blocks" /local emit emit-range rules from* to* index* ; parse vars result ][ emit: func [value] [ either only [append/only result value][append result value] ] emit-range: func [start end] [ start: to integer! start if number? end [end: to integer! end - start + 1] emit either end = 'end [copy at series start][ copy/part at series start end ] ] rules: [ some [ opt 'from set from* number! 'to set to* number! ( emit-range from* to* ) | opt 'from set from* number! 'to 'end (emit-range from* 'end) | 'to set to* number! (emit-range 1 to*) | set index* number! (emit pick series index*) | into rules ] ] ; Return a block. Easy enough for them to REJOIN if they want. result: make block! length? series parse offsets rules result ] b: [1 2 3 4 5 6 7 8 9 10 11 12 13 14] excerpt b [1 3 5] excerpt b [1 3 to 6 8] excerpt/only b [1, 3 to 6, 8] excerpt b [1 [5 to 7] 8] excerpt/only b [1 (from 5 to 7) 8] excerpt b [(to 2) [4 to 6] 8, 10, from 12 to end] excerpt/only b [to 2, 4 to 6, 8, 10, (12 to end)] ; Can't use a comma after 'end excerpt/only b [to 2 to 6 8 10 to end 12 to end] excerpt/only b [to 2, to 6, 8 [10 to end] 12 to end] excerpt/only trim { REBOL is my favorite language } [ to 5, 10 to 11, 13, 14, 15, 22 to end ]