World: r3wp
[Core] Discuss core issues
older newer | first last |
Chris 9-Jan-2007 [6582] | Except the second one doesn't work :( |
Anton 9-Jan-2007 [6583x3] | Tricky isn't it ? |
Not bad ideas though. I think I prefer to use SET, rather than FOREACH, to set the record values, to keep the variable length ability. | |
Mmm... maybe USE-FOREACH better deserves your functionality, and USE-FORALL could be a function which steps through the DATA block, allowing access to it. | |
Anton 10-Jan-2007 [6586x2] | This works: use-forall: func [ words data body ][ use words compose/deep [ forall data [ set [(words)] bind data/1 'data (bind body 'data) ] ] ] ; test data: [ [code description] ["CC" "Crazy Cuts"] ["DD" "Dreadful Dingo"] ] use-forall data/1 next data [ print index? data if code = "CC" [print description] ] |
>> use-forall [leg arm hair] [["long" "skinny" black]["short" "fat" blonde]][?? arm] arm: "skinny" arm: "fat" == "fat" | |
Chris 10-Jan-2007 [6588] | I think this is pretty sturdy -- an interesting exercise in context wrestling: use-foreach: func [words records body][ bind body first words: use words reduce [words] foreach record records [set words record do body] ] |
Anton 10-Jan-2007 [6589] | That's Brazilian bindology, Chris. |
Ladislav 10-Jan-2007 [6590] | why Brazilian, Anton? |
Gabriele 10-Jan-2007 [6591] | anton, chris' way is how i'd probably do it. |
Anton 10-Jan-2007 [6592x8] | Ladislav, a reference to Brazilian ju-jitsu, which is wrestling. |
Wait, I haven't shown you this yet. ; my second version, more like Chris's version, using FORALL use-foreach: func [ words [block! word!] data [series!] "The series to traverse" body [block!] "Block to evaluate each time" ][ use words compose/deep [ forall data [ set [(words)] data/1 (body) ] ] ] | |
It uses less words, is easier to understand, but it has a more brittle dependency chain via FORALL mezzanine which uses FORSKIP mezzanine which uses WHILE native. | |
Oh, and although it uses less words, it spreads them out across more lines. | |
Chris' version does not support WORDS argument as a word! - but I don't think it's that important to support. | |
Gabriele, since you say that, Chris' version does remind me of your style of coding. | |
- Nested blocks in the user code might require BIND/COPY. - [throw] (?) - [catch] (?) | |
- RETURN and BREAK ... | |
Volker 10-Jan-2007 [6600] | i opt for chris version. its a loop, foreach is faster. but add a version with more lines, for explanation^^. |
Chris 10-Jan-2007 [6601x3] | Anton, would it add too much to change the 'forall loop to a 'while loop? |
Fundamentally, I don't see much difference in our separate approaches. Using 'compose certainly makes it easier to read. (I'm not sure why, I seem to have an inherent aversion to 'compose) | |
Using compose, I could get the word! argument working: bind body first words: use words compose/deep [[(words)]] | |
Anton 10-Jan-2007 [6604x5] | Volker, yep, documentation last, as usual ! |
Chris, no, I would do it: | |
; my fourth version, using WHILE use-foreach: func [ words [block! word!] data [series!] "The series to traverse" body [block!] "Block to evaluate each time" ][ use words compose/deep [ while [not tail? data][ set [(words)] data/1 (body) data: next data ] ] ] | |
It's a pity that it uses five more words, but the extra stability and simplicity of going direct to a native seems worth it. | |
(FORALL will be ok, it gets enough usage in normal code. ;-) | |
Volker 10-Jan-2007 [6609] | ; more commented: use-foreach: func [words records body /local fresh-locals][ ; make fresh locals for the args fresh-locals: use words reduce [words] ; trick ; bind body to one of the new locals bind body first fresh-locals ; now loop foreach record records [ set words record do body ] ] |
Anton 10-Jan-2007 [6610] | Comments good, but I think, don't add them yet. There are still some code changes to make yet, I think. |
Volker 10-Jan-2007 [6611] | Agreed. Tried to make it more readable. The original looked like magic, but somewhat formatted i find it clear. |
Anton 10-Jan-2007 [6612] | (Also, I think adding FRESH-LOCALS is not a good move.) |
Volker 10-Jan-2007 [6613] | Was for readibility-demonstration.Would notdo that in the working version. but somewhere in the docu. |
Anton 10-Jan-2007 [6614] | Agreed, the original did look like magic. |
Joe 12-Jan-2007 [6615x2] | has anybody used core async dns ? I am trying to resolve domains using a port per domain but I get a sequential behaviour. Ideally, this should be done with a single port, sending requests and getting replies in an async fashion. snippet below |
f-awake: func [ port /local res ][ res: copy port print [port/user-data/host res] remove find system/ports/wait-list port false ] b-hosts: [ "www.rebol.net" "www.google.com" "www.yahoo.com" "www.microsoft.com" "xxafda" "www.ebay.com" "www.amazon.com" ] foreach h b-hosts [ p: open/no-wait make port! [ scheme: 'dns host: "/async" user-data: reduce ['host h] awake: :f-awake ] insert tail system/ports/wait-list p insert p h ] wait [] | |
Gabriele 12-Jan-2007 [6617x2] | although dns:///async is asyncronous, it probably can't do multiple host resolution at the same time. |
i never used it this way, so i can't say if it can be made to work. but by thinking about how this is implemented on unix (a second process that calls gethostbyname), i'd guess that it can only do one host resolution at a time. | |
Ladislav 12-Jan-2007 [6619] | only now I noticed, that: even? 0.1 ; == true my understanding differs slightly, I would more likely expect a test like: zero? 0.1 // 2 any opinions? |
Robert 12-Jan-2007 [6620] | I totaly agree. |
Joe 12-Jan-2007 [6621] | gabriele, thanks, does your async library handle async dns ? |
Gabriele 12-Jan-2007 [6622x6] | yes, when you use the async:// protocol the host name is resolved asynchronously. |
but i never tried what happens if i try to resolve many hosts at the same time | |
(btw, my code is very similar to yours above, so i think you're using it correctly.) | |
http://www.colellachiara.com/soft/libs/async-protocol.r | |
ladislav, i guess even? and odd? just convert everything to integer? | |
with your test above, would any non-integer number be odd? | |
Ladislav 13-Jan-2007 [6628] | odd - well, another possibility is to say that ods is not even |
Rebolek 13-Jan-2007 [6629] | ladislav yes, ods is not even, ods is odd, but i think that not-czechs can't understand ;-] |
Pekr 13-Jan-2007 [6630] | :-)) |
Ladislav 13-Jan-2007 [6631] | sorry for the typo |
older newer | first last |