problems creating a path
[1/11] from: antonr:iinet:au at: 2-Jun-2003 0:20
I am trying to call a function with certain
refinements present or not present, depending on
user switches.
This is one example that gives me what I want:
files?: true
join 'my-func/test either files? ['files][[]]
;== my-func/test/files
files?: false
join 'my-func/test either files? ['files][[]]
;== my-func/test
That joins onto the lit-path! 'my-func/test successfully or
leaves it alone.
But this example is difficult to make work, because all the
refinements of the path are optional, so the path starts as
only a single word!:
files?: true
folders?: false
to-path reduce ['my-func either files? ['files][[]] either folders?
['folders][[]]]
;== my-func/files/[]
How to get rid of the brackets?
I tried also:
to-path reduce [to-lit-path 'my-func either files? ['files][[]] either
folders? ['folders][[]]]
;** Script Error: Invalid argument: 'my-func
;** Where: to-path
;** Near: to path! :value
join to-lit-path 'my-func [either files? ['files][[]] either folders?
['folders][[]]]
;== my-func/files/[]
So using a block for the second argument is not going to work.
(Anybody know a way?)
Finally I went back to how it was working to start with:
join join to-path 'my-func either files? ['files][[]] either folders?
['folders][[]]
;== my-func/files
It seems less nice, but it works.
Anton Dale Rolls.
[2/11] from: lmecir:mbox:vol:cz at: 1-Jun-2003 17:20
Hi Anton,
----- Original Message -----
From: "Anton" <[antonr--iinet--net--au]>
To: "rebol-list" <[rebol-list--rebol--com]>
Sent: Sunday, June 01, 2003 4:20 PM
Subject: [REBOL] problems creating a path
> I am trying to call a function with certain
> refinements present or not present, depending on
<<quoted lines omitted: 38>>
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
see http://www.fm.vslib.cz/~ladislav/rebol/apply.r
or have a look at
http://www.fm.vslib.cz/~ladislav/rebol/tfunc.r
-L
[3/11] from: rotenca:telvia:it at: 1-Jun-2003 18:23
Hi Anton,
> How to get rid of the brackets?
compose could help
to-path compose [
my-func
(pick [files []] files?)
(pick [folders []] folders?)
]
---
Ciao
Romano
[4/11] from: antonr:iinet:au at: 2-Jun-2003 12:01
Thanks, Romano.
That's just what I was looking for.
Anton.
[5/11] from: antonr:iinet:au at: 2-Jun-2003 16:43
Thanks Ladislav, these functions are a bit
more complicated than what I want, but they
might be useful for something else, so I'll
keep it in mind.
Anton.
[6/11] from: antonr:iinet:au at: 4-Jun-2003 13:36
Actually, I couldn't quite use 'pick, unless I converted
the values to logic!s, which went above a threshold of
ease-of-use for me.
> > How to get rid of the brackets?
> compose could help
<<quoted lines omitted: 6>>
> Ciao
> Romano
I simplified my example a little too much.
The code needed to also accept none as a value in
files? & folders?, not just true & false, because
the values were coming from check boxes and radios
in a window.
Anyway, the best I can do is this:
blk: do reduce [to-path compose [
traverse
(either folders-chk/data ['show-folders][])
(either files-chk/data ['show-files][])
(either images-chk/data ['show-images][])
(either abs-rl/data ['absolute-paths][])
(either details-chk/data ['details][])
] folder]
I passed through some other alternatives, but this way
seems the best. I was looking for a way that does not
use any extra words, has a small code size, and is easy
to add new refinements.
By chance is there anyone who can do better? I challenge you!
I busted my head on this for some hours.
Anton.
[7/11] from: brett:codeconscious at: 4-Jun-2003 16:31
Well my only suggestion was along the lines of treating your refinements as
data and introducing a loop and then I got carried away...
; --- 8< ---------------------------------------
;
; Our list of refinements.
; Might be good to add labels :)
;
refinements: [
show-folders show-files show-images absolute-paths details
]
;
; Our simulated function.
;
funcspec: copy refinements
forall funcspec [change funcspec to refinement! funcspec/1]
traverse: func head funcspec compose/deep [
reduce [(:refinements)]
]
;
; A generated layout of checkboxes.
; Each with a special refinement facet.
;
layout-spec: copy [
style rcheck check [redisplay]
across
label "path: " txtPath: text 400 return
label "result: " txtResult: text 400 return
]
foreach refinement refinements [
append layout-spec compose/deep [
label 100 (join to-string refinement ":")
rcheck with [
refinement: (to lit-word! :refinement)
]
return
]
]
;
; Computes the path based on the checkboxes found in pane.
; I suggest that this bit is fairly compact while retaining
; the flexibility of adding refinements later. And it does not
; add any new words except the facet refinement which
; I would say is data storage :)
function-path?: func [
pane [block!] /local p fld
] [
p: to path! 'traverse
foreach face pane [
if all [
in face 'refinement
face/data
] [append p face/refinement]
]
:p
]
;
; A function to update the display.
; Assuming our layout is the first in the pane.
;
redisplay: has [path] [
path: function-path? layout-face/pane
txtPath/text: mold path
txtResult/text: mold do reduce [function-path? layout-face/pane]
show layout-face
]
;
; View the layout.
;
view layout-face: layout layout-spec
; ---8<---------------------------------------
Regards,
Brett.
[8/11] from: antonr:iinet:au at: 5-Jun-2003 9:05
Carried away indeed!
Do I detect humour?
Anton.
[9/11] from: rotenca::telvia::it at: 5-Jun-2003 13:09
Hi anton
> I passed through some other alternatives, but this way
> seems the best. I was looking for a way that does not
> use any extra words, has a small code size, and is easy
> to add new refinements.
> By chance is there anyone who can do better? I challenge you!
> I busted my head on this for some hours.
The problems grows if refinements have arguments and in these cases a
function should be the best thing.
What I want to underline:
1) I should like to have some native facilities to pass refinements to
sub-functions.
2) I like to use true/none every where i can use true/false (pick instead of
either in your example). The actual implementation seems to me an
inconstistence.
---
Ciao
Romano
[10/11] from: antonr:iinet:au at: 6-Jun-2003 13:08
Romano, I strongly agree. My recent and past difficulties
forming paths lead me to conclude that: "there must be a
better way".
If pick accepted none for its index argument that would
really help, in this situation, although I don't think
it is all that inconsistent right now.
Well, I got to thinking, a small function to convert none
values to unset! and pass through others could help.
f: func [val][either val [val][]]
to-path compose [hello (f all [none 'there])]
;== hello
; (replace the none with true/false etc.)
('f is not a very good name for it, but I'd want it to
be short).
Anton.
[11/11] from: antonr:iinet:au at: 6-Jun-2003 17:00
After some more thought I think I'd like to use this more:
flag-val: func [flag val][either flag [val][]]
to-path compose [hello (flag-val none 'there) (flag-val true 'someone)]
;== hello/someone
Anton.
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted