World: r3wp
[Core] Discuss core issues
older newer | first last |
Gordon 5-Nov-2005 [2682x6] | actually you can. Working example to follow |
foreach [a b c d e f g h i] Datablock [ print [a b c d e f g h i]] | |
This traverses the datablock and sets each of the 9 values and prints them as a record. | |
However, I want to be able to set a variable instead of hardcoding "[a b c d e f g h i]" | |
ex: Record: array 9 | |
then foreach record datablock [ print record] but when I do that, record doesn't contain an array of values | |
DideC 5-Nov-2005 [2688x2] | Sorry I don't understand what you want to do :-\ |
Oups, cross post | |
Volker 5-Nov-2005 [2690x2] | !> w: [a b] == [a b] !> foreach :w [1 2 3 4][? a ? b] A is an integer of value: 1 B is an integer of value: 2 A is an integer of value: 3 B is an integer of value: 4 |
me too, and wrong. | |
Gordon 5-Nov-2005 [2692] | okay just a minute |
DideC 5-Nov-2005 [2693] | Ok, you want dynamic block of word to set, right? |
Sunanda 5-Nov-2005 [2694x2] | Yiu can't have one variable -- you need a set of them, eg: foreach [key data] ["a" 1232 "c" 7737 "z" 777] [print [key data]] But you can fake it something like this...... |
data: ["a" 1232 "c" 7737 "z" 777] for nn 1 length? data 2 [print [pick data nn pick data nn + 1]] | |
Gordon 5-Nov-2005 [2696] | record-sort: func [record [block!] num [integer!]] [ tmp: copy [] new: copy [] foreach [a b c d] record [append/only tmp reduce [a b c d]] sort-method: func [a b] [(at a num) < (at b num)] foreach rec sort/compare tmp :sort-method [append new rec] return new ] Here is the record-sort example given in the docs. Problem is this only works for records that have four items. |
Volker 5-Nov-2005 [2697] | the easier way is forskip. 'foreach needs a binding-trick |
DideC 5-Nov-2005 [2698] | build the code then do it :) |
Gordon 5-Nov-2005 [2699x2] | Sunanda: Thanks for the example. I'll play with your example and see what I can do. |
Volker: Thanks. I'll take a look at using forskip | |
Volker 5-Nov-2005 [2701] | why must it be in words? |
Gordon 5-Nov-2005 [2702x3] | The goal is to rewrite the record-sort function so that the number of elements is not hard coded into the function. |
I want to be able to pass the function another value which equals the number of elements in a record and then do something like Record: array {Elements} | |
within the function. This would make the function more universal. | |
DideC 5-Nov-2005 [2705] | >> print-n: func [record words] [do compose/deep [ foreach [(words)] record [print [(words)]]]] >> print-n [1 2 3 4 5 6 7 8 9] [a b c] 1 2 3 4 5 6 7 8 9 |
Volker 5-Nov-2005 [2706] | forskip record 4 [append/only tmp copy/part record 4] |
Gordon 5-Nov-2005 [2707] | Humm. That looks like it will work. |
Sunanda 5-Nov-2005 [2708] | does sort/skip directly do what you want? |
Gordon 5-Nov-2005 [2709x2] | I'm not sure. I don't think so but I can't remember why not. |
Something about reordering the records makes sort/skip not suitable. | |
BrianH 5-Nov-2005 [2711] | (Back to slicing briefly) REBOL already has functionality equivalent to slicing as copy-of-subsection, so this would be better represented as a very simple mezzanine function that does the work. If you mean slicing as reference-to-subsection-in-place, that would be worth adding new support for. Something like: slice!: make object! [start: end: none] slice: func [[catch] s [series!] /from b [integer!] /to e [integer!] /len l [integer!]] [ b: either from [at :s b] [:s] e: case [ to at :s e len skip :b l true tail :s ] if greater? index? :b index? :e [to: :e e: :b b: :to] make slice! [start: b end: e] ] |
Sunanda 5-Nov-2005 [2712] | sort/skip --- I get you -- like it's not easy to sort on the 2-digit numbers here: sort/skip [99 10 2 98 11 5 97 12 4] 3 But it is possible with /all and a parameter into the function |
Gordon 5-Nov-2005 [2713x2] | Lots to think about and try. Thanks a bunch guys for your help. |
When I get the function rewritten, I will upload it to the library. | |
BrianH 5-Nov-2005 [2715x2] | Sunanda, do you mean sort on the second field? Try passing an integer or block of integers to the compare refinement of sort. |
Rather than a comparator function. | |
Sunanda 5-Nov-2005 [2717] | I mean the middle of the three fields, sorting a 3-field record: sort-func: func [a b] [return a/2 < b/2] sort/skip/all/compare [99 10 2 98 11 5 97 12 4] 3 :sort-func == [99 10 2 98 11 5 97 12 4] |
BrianH 5-Nov-2005 [2718x2] | sort/skip/compare data 3 2 |
The 2 passed to /comapare means the second field. | |
Sunanda 5-Nov-2005 [2720] | Clever -- I didn't know that. Thanks. But I tend to use the compare function to get stable sorts. The [x < y] was just to shorten the example. |
BrianH 5-Nov-2005 [2721x3] | (sorry, /compare) |
What, sort isn't stable? | |
I've always found it to be so. | |
Sunanda 5-Nov-2005 [2724] | Not unless you return +1 ,0, -1 Returning true/false is not guaranteed to be stable |
Gordon 5-Nov-2005 [2725] | Sort hasn't crashed on me if that's what you mean by unstable. |
BrianH 5-Nov-2005 [2726] | It's not. |
Sunanda 5-Nov-2005 [2727x2] | Stable is to do with the order in which duplicate keys are returned. |
Looks like stable camwe with /core 2.5 http://www.rebol.com/docs/core25.html | |
Graham 5-Nov-2005 [2729x3] | Any mathematicians here ? |
On this page : http://www.mja.com.au/public/issues/178_03_030203/sim10248_fm.html#CHDBFAJE There is this formula Absolute CVD risk can be calculated from the formula: Probability of CVD = 1/(1 + e- k ), where k = – 8.65 + 0.057*age – 0.61*sex + 0.749*antihypertensive medication + 0.008*systolic blood pressure + 0.458*smoking + 0.18*cholesterol – 0.234*HDL cholesterol + 0.857*diabetes. | |
what does e stand for ? Is that some type of log calculation ? | |
older newer | first last |