r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Graham
5-Nov-2008
[11252]
this is Gregg's substr function


substring: func [
    [catch]
    source [string!]
    spec [block!]
    /local start stop rule
][
    rule: [set start integer! '.. set stop integer!]
    unless parse spec rule [
        throw make error! "Invalid range spec."
    ]
    copy/part skip source start stop
]
Chris
5-Nov-2008
[11253]
You could produce a 'truncate function that uses a pair! to similar 
effect...

truncate: func [string limit [pair! integer!]][
	if integer? limit [limit: 1x0 * limit]
	string: copy string
	case [
		negative? limit/x [remove/part string skip tail string limit/x]
		positive? limit/x [remove/part string limit/x]
		negative? limit/y [clear skip tail string limit/y]
		positive? limit/x [clear skip string limit/y]
	]
	string
]
Graham
5-Nov-2008
[11254x2]
the ".." is syntactic sugar.
the dialect doesn't cope with negative offsets it seems
Chris
5-Nov-2008
[11256x2]
; This works!:

truncate: func [string limit [pair! integer!]][
	if integer? limit [limit: 1x0 * limit]
	string: copy string
	case/all [
		negative? limit/x [limit/x: limit/x + length? string]
		negative? limit/y [limit/y: limit/y + length? string]
	]
	clear skip string limit/y
	remove/part string limit/x
]
>> truncate "[abc]" 1x-1 
== "abc"
Graham
5-Nov-2008
[11258x2]
cool
I think these sorts of series maniplulations should be native
Chris
5-Nov-2008
[11260x2]
Quite, except with range! instead of pair!
copy/part "[abc]" 1..-1
remove/part "[abcd]" 1..-1
BrianH
5-Nov-2008
[11262]
They are native, just without any unnecessary extra syntax. This 
isn't Perl, you know.
Graham
5-Nov-2008
[11263x2]
but perl rulz!
the power of a language lies partly in it's ability to express things 
concisely
PeterWood
5-Nov-2008
[11265]
>> str: "[abc]"    
 
== "[abc]

>> copy/part skip str 1 back tail str

== 
abc"
Graham
5-Nov-2008
[11266x4]
str appears twice
it's annoying
three times, if you do 

str: copy/part skip str 1 back tail str
instead of 'truncate, how about 'peel ?
PeterWood
5-Nov-2008
[11270]
If you are removing characters from both ends of a string 'strip 
would appear to be a more precise description.
Graham
5-Nov-2008
[11271]
Hmm.  You can strip the inner lining of something, but normally you 
only peel the outer layers :)
PeterWood
5-Nov-2008
[11272]
If that's you definition of stripping it's no wonder you live in 
NZ.
Graham
5-Nov-2008
[11273x3]
However, my preference is to add some dialect to trim ...
rather than to keep adding words
Strip mining?
PeterWood
5-Nov-2008
[11276]
Stripping paint? stripping wallpaper?
Graham
5-Nov-2008
[11277x3]
strip a joint ... = remove the contents
so, peel has a more restrictive meaning being a subset of strip.
I hope we are both joking here :)
Rebolek
5-Nov-2008
[11280x2]
'trim is not good fo this?
>> trim/with "[Something]" "[]"
== "Something"
Graham
5-Nov-2008
[11282x2]
it would have to be trim/head/tail/with to avoid removing characters 
inside ...
but certainly I forgot about trim/with !
Rebolek
5-Nov-2008
[11284x2]
Hm, it doesn't work
>> trim/head/tail/with "[Some[]thing]" "[]"
** Script Error: Invalid path value: with
** Near: trim/head/tail/with "[Some[]thing]" "[]"
Gregg
5-Nov-2008
[11286]
I'm not a big fan of the -n syntax to mean "from the end", but that's 
just me. It's convenient to type, but doesn't provide meaning. My 
BOUNDS/RANGE dialect supports .. notation via tuples so you can't 
use negative numbers.


For the trim/strip stuff, I ended up with a bunch of related funcs 
(begins-with?, ends-with?, enclose, etc.), and it works for more 
than single chars. Not as simple as a standalone func, but more robust 
and flexible for the general case.
Graham
7-Nov-2008
[11287]
Why can't we have functions that return a value to the calling function, 
but then continue to do something else ?  Is this because we lack 
multithreading?
PeterWood
7-Nov-2008
[11288]
If you're not looking for further return values, you could approximate 
this behaviour by using call or launch.
Graham
7-Nov-2008
[11289x2]
too expensive
but yes, that is what we have to do at present
Pekr
8-Nov-2008
[11291]
Graham: how could single threaded code return, and yet continue to 
execute recent function? What usability case do you have in mind 
here?
Graham
8-Nov-2008
[11292]
web servers
BrianH
8-Nov-2008
[11293x2]
Would reentrant functions with static variables do?
For that matter, do you mean continue to do something else while 
the calling function is also doing something with your return value 
(multitasking) or do you mean picking up where you left off, later 
(generators)?
Graham
8-Nov-2008
[11295]
the former
BrianH
8-Nov-2008
[11296]
For REBOL 2, multitasking means multiprocessing, sorry.
Henrik
12-Nov-2008
[11297]
Is there a way to read out the uptime for a rebol process inside 
rebol?
Pekr
12-Nov-2008
[11298]
I think not. The only way might be to set start: now at the beginning, 
and then read it out somehow. If the process crashes though, you 
are lost. You could write it to log from time to time, to see, what 
was happening and when ....
Henrik
12-Nov-2008
[11299x2]
I'm writing a simple log from Cheyenne to see if I can catch a bug 
in the HTML dialect, but it could be useful to read out something 
like system/uptime without relying on someone making a timestamp 
at startup beforehand.
Added to RAMBO.
Pekr
12-Nov-2008
[11301]
maybe now/uptime ?