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

World: r3wp

[Core] Discuss core issues

amacleod
16-Oct-2008
[11132]
THat was my next strp. I thought I was missing some simple conversion.
Anton
16-Oct-2008
[11133]
That's simpler to write, but two passes. The parse I show above is 
more complex to write, but one pass.
amacleod
16-Oct-2008
[11134x2]
s2: copy a parse/all s2 [some [p: "^/" (change/part p "^^/" 1) skip 
| "^-" (chang
e/part p "^^-" 1) skip | skip]]
== true
>> s2
== {a^^/^^-b^^/^^-c}
>> print s2
a^/^-b^/^-c


Anton, It looks like what I want but if aI print it the newlines 
are not reconized???
But if I do it manually: 
>> z: {a^/^-b^/^-^-c}
== {a
^-b
^-^-c}
>> probe z
{a
^-b
^-^-c}
== {a
^-b
^-^-c}

I do not get it. Are there hidden characters?
Gregg
16-Oct-2008
[11136]
By adding the secon ^, it escapes the ^ that marks the newline, so 
it is no longer a newline. When you write the string out, then it 
will write out as "a^/^-b^/^-c" which will convert to newlines again 
when loaded. If you're writing out and loading strings, you may have 
to convert the data back yourself when you load it, because REBOL 
won't parse into it to do so. e.g., if you mold an object spec to 
be on a single line like that, REBOL will convert them on load, but 
if you just read a file as a string, it won't.
Anton
16-Oct-2008
[11137x4]
The confusing thing is, rebol molds short strings and longer strings 
differently. In short strings, the newlines are molded as ^^/ , but 
in longer strings (like yours above) the newlines are "evaluated" 
(moving the cursor to next line).
Check it out:
>> "a^/b"
== "a^/b"
>> "a^/^-b"
== "a^/^-b"
>> "a^/^-b^/"
== "a^/^-b^/"
>> "a^/^-b^/^-^-c"
== {a
^-b
^-^-c}
boom!  That last line is long enough that the newlines are "evaluated".
(It's more complicated than just string length, actually. Exact algorithm 
unknown.)
amacleod
16-Oct-2008
[11141]
Greg, I tried wrote it out ot a file and loaded it but it still does 
not print properly

Is there a work around?
Anton
16-Oct-2008
[11142x3]
amacleod, I think Greg and mine solution both work. They produce 
a string molded so that the two control characters (newline and tab) 
are escaped. You just need to ... ahh... here's where I was going 
to say "just LOAD it." but I get an error message.

>> load "^^/"
** Syntax Error: Invalid path -- ^/
** Near: (line 1) ^/

So maybe do this instead:

	s: "^/"  ; <-- input string

 s: "^^/"  ; <--- specially "molded" string as produced by PARSE or 
 REPLACE above.
	load rejoin ["{" s "}"]  ; <-- specially "loaded" string

the result of which should be the same as the input.
The longer example:
>> s: "a^/^-b^/^-^-c"
== {a
^-b
^-^-c}


>> s2: copy s parse/all s2 [some [p: "^/" (change/part p "^^/" 1) 
skip | "^-" (change/part p "^^-" 1) skip | skip]]
== true

>> s3: load rejoin ["{" s "}"]
== {a
^-b
^-^-c}

>> s = s3
== true
amacleod
16-Oct-2008
[11145]
Did you mean: 
s3: load rejoin ["{" s2"}"]  ?
with s2 in the rejoin and not s ?

That seems to work and produce what I'm tring to get...

A little awkward but only two lines for what I want...

Thanks again,  Anton.


I can use this in the short time but I may need to just use SQLite 
or my own solution for storage as Paul is no longer developing Tretbase...
Geomol
16-Oct-2008
[11146]
amacleod, can you use NicomDB?
http://www.fys.ku.dk/~niclasen/nicomdb/index.html


NicomDB is the result of an education, I took some years ago. More 
info:
http://www.fys.ku.dk/~niclasen/nicomdb/thesis.pdf

There's also a group about it here in the REBOL3 world. See group 
!NicomDB
Anton
17-Oct-2008
[11147]
amacleod, ah yes, small typo there.
Chris
17-Oct-2008
[11148x2]
On 'bind, I use 'with to make some bind situations more readable:

	other-context: context [
		print: [statement][probe head reverse copy statement]
	]

	with other-context [print "Text"]
with: func [object [any-word! object! port!] block [any-block!] /only][
	block: bind block object
	either only [block] :block
]
amacleod
17-Oct-2008
[11150x3]
I was looking into sqlite but reluctantly...
Idid a quick glance through your paper and it had a lot of good iseas 
that might fit my needs. I'm going to play around with it.
Are you still developing it?
iseas = ideas
Geomol
18-Oct-2008
[11153]
amacleod, I use NicomDB myself in different projects and is developing 
it further, when needs arise.
Gregg
18-Oct-2008
[11154]
From !REBOL3 group, following MattAnton's fbionacci func.


Matt, it's a good func, but there are some things to watch out for 
in REBOL, which are different from many other languages.


1) Undeclared vars in func become global. Use the /local refinement 
to declare them.


2) Series values in funcs (e.g. your starting block of [0.0 1.0] 
maintain their value between calls if you don't use COPY. Run your 
function multiple times to see what happens. It may be that you wanted 
this to be a memoizing function, but then why UNSET 'fibonacci-block?.


I think you also mentioned that the challenge was to do it recursively, 
which this isn't. That's a case where you would definitely want to 
memoize. :-)


In any case, this is always fun stuff to think about.  Here's a modified 
version for you to play with. Look at some of the other REBOL funcs 
used, see if you find any bugs, or maybe it will give you ideas for 
other ways to solve the problem.

fibonacci: func [

    "Returns a list of fibonacci numbers, up to the specified count."
	count [integer!] "Number of iterations to run"
	/trace
	/local res n-1 n-2 incr step
] [
    incr: func [word] [set word 1 + get word]
    step: does [incr 'n-1  incr 'n-2]
	res: copy [0.0 1.0]
	set [n-1 n-2] [1 2]
	repeat i count [
		append res add pick res n-1 pick res n-2
		step
		if trace [print [i last res]]
    ]
    res
]
print mold fibonacci 3
print mold fibonacci 46
Dockimbel
18-Oct-2008
[11155x2]
>> print mold fibonacci 3
[0.0 1.0 1.0 2.0 3.0]

Doesn't it supposed to return : [0.0 1.0 1.0 2.0] ?
Here's my attempt with a caching and fully recursive version :

fibonacci: func [n /local f][
	f: [0.0 1.0]
	either f/(n + 1) [copy/part f n + 1][
		fibonacci n - 1
		append f f/:n + f/(n - 1)
	]
]

probe fibonacci 3	
probe fibonacci 46
probe fibonacci 8

Note that :


1) The last call with 8 value is just an extraction of the pre-computed 
cached sequence (cached values up to 46th by the previous call), 
so it executes in 0(1).


2) References to 'n (except fibonacci n - 1) are incremented by 1 
to account for REBOL series 1-based indexes. If we could switch to 
0-based indexes, the function source would be more readable (closer 
to the pure algorithm).
Robert
19-Oct-2008
[11157x3]
Can someone help me with this?

>> open tcp://:12345
>> open tcp://:12345
** Access Error: Error opening socket listen port
** Near: open tcp://:12345
>> a: 12345
== 12345
>> open tcp://:a
>> open tcp://:a
>>
How can I use 'a as a reference to port 12345 as well?
Seems to be handled differently.
Graham
19-Oct-2008
[11160]
>> a: 1234
== 1234
>> p: open join tcp://: a
>> close p
>> p: open join tcp://: a
>> p: open join tcp://: a
** Access Error: Error opening socket listen port
** Near: p: open join tcp://: a
>> close p
Robert
19-Oct-2008
[11161]
Ah, thanks. I was irritaded by the concatenation of : and 12345 So 
I can write tcp://: 12345 as well.
Graham
19-Oct-2008
[11162]
p: open tcp://::a
Graham
25-Oct-2008
[11163x2]
I need to form the date in UTC coordinates....  eg. 2008-10-25T08:33:0.4Z

Anyone got something more elegant than this?

form-utc: func [ d [date!]
    /local 
][
    ; convert to GMT
    d: d - d/5 
    rejoin [ 
        d/year "-" 
        either d/month < 10 [ join "0" d/month] [ d/month ] "-" 
        either d/day < 10 [ join "0" d/day ][ d/day ] "T"
        either d/time/1 < 10 [ join "0" d/time/1][ d/time/1 ] ":"
        either d/time/2 < 10 [ join "0" d/time/2][ d/time/2 ] ":"
        round/to d/time/3 .1 "Z"
    ]
]
perhaps we can get a /utc option for date types??
Anton
25-Oct-2008
[11165x4]
either d/month < 10 [join "0" d/month][d/month]
becomes
either d/month < 10 ["0"][""] d/month
d/5  ->  d/zone
etc.
'd is not specified local
Gregg
25-Oct-2008
[11169x4]
'd is the param name.
REBOL.org has a couple ISO date formatting funcs, though I think 
a lot of us roll our own, sometimes ad hoc. It depends, too, on how 
flexible--or accepting of various inputs--you want it to be.
as-utc: func [date] [
    if all [date/zone  0:00 <> date/zone] [
        date: add date negate date/zone
    ]
    date/zone: none
    if none? date/time [date/time: 0:0:0]
    date
]

to-ISO8601-date: func [
    "Converts a date! value to an ISO 8601 format string."
    date [date!] "The date to format"

    /T           {Use T to delimit time value, rather than a space}
    /no-zone     "Don't include the timezone"
    /local pad z
][
    pad: func [val /to len] [
        val: form val
        head insert/dup val #"0" ((any [len 2]) - length? val)
    ]

    rejoin [
        pad/to date/year 4 "-" pad date/month "-" pad date/day
        either T ["T"] [" "]

        either none? t: date/time ["00:00:00Z"] [   ;<< reusing 'T here!
            rejoin [

                pad t/hour ":" pad t/minute ":" pad round t/second
                either no-zone [""] [

                    either 0:00 = z: date/zone ["Z"] [  ;<< setting 'z here!
                        rejoin [
                            pick ["+" "-"] z/hour > 0
                            pad abs z/hour pad abs z/minute
                        ]
                    ]
                ]
            ]
        ]
    ]
]
>> to-ISO8601-date/T as-utc now
== "2008-10-25T18:41:13Z"
>> to-ISO8601-date/T as-utc now/date
== "2008-10-25T00:00:00Z"
Graham
25-Oct-2008
[11173x2]
format I have is SS.FZ
so pad here will not pad 5.2Z to 05.2Z
Gregg
25-Oct-2008
[11175x2]
I looked at some of my stuff but, for some reason, I don't seem to 
have one that does the 0.0 format for seconds. Even my FORMAT func 
doesn't work for that, though it would shorten the rejoins a bit. 
e.g.

form-as-utc: func [date] [
    format as-utc date "yyyy-mm-dd\Thhh:mm:ss\Z"
]


Just have to change that last part for the seconds. But I can't remember 
if I've published FORMAT.
Yeah, I've seen that format as a standard, which is why I don't know 
why I don't have that. Must not have needed it. :-\
Graham
25-Oct-2008
[11177x5]
I ended up just by factoring out my formatting to
format-10: func [ d [integer! decimal!]
][
    either d < 10 [ join "0" d ]
    [ form d ]
]

form-utc: func [ d [date!]
    /local 
][
    ; convert to GMT
    d: d - d/5 
    rejoin [ 
        d/year "-" 
        format-10 d/month "-" 
        format-10 d/day "T"
        format-10 d/time/1 ":"
        format-10 d/time/2  ":"
        format-10 round/to d/time/3 .1 "Z"
    ]
]
I can understand that one :)
why is your as-utc so complicated ? What case am I missing?
can date/zone be none?