Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Apply and Named

 [1/1] from: lmecir::mbox::vol::cz at: 9-Mar-2004 18:25


I experimented a bit with Apply and Named functions. The latest versions can be found at http://www.fm.vslib.cz/~ladislav/rebol/apply.r The functions are optimized to work under Core 2.6. Examples: ; Named argument passing without refinements named :subtract [value2: 1 value1: 2] ; Named argument passing with refinements test-fn: func [a b /ref-c c /ref-d d] [ print ["A:" a] print ["B:" b] if ref-c [ print ["C:" c] ] if ref-d [ print ["D:" d] ] ] named :test-fn [ref-d: ref-c: true d: 1 c: 2 b: 3 a: 4] ; Example: apply :subtract [2 1] ; Example: apply :type? [()] ;Joel's examples: bunchanums: [3 1 35 8 4 5 52 42 19 13 32 43 81 2 6 34 46] tally: make object! [ tot: 0 zero: does [tot: 0] up: func [/by n [number!]] [tot: tot + either by [n] [1]] down: func [/by n [number!]] [tot: tot - either by [n] [1]] now?: does [tot] ] ; Joel's example #1: use [evens odds] [ evens: make tally [] odds: make tally [] foreach num bunchanums [ apply [(either even? num [evens] [odds]) up by] [num] ] print ["evens:" apply [evens now?] [] " odds:" apply [odds now?] []] ] ; Result: ; evens: 226 odds: 200 ; Joel's example #2: use [diffs] [ diffs: make tally [] foreach num bunchanums [ apply [diffs (either even? num ['up] ['down]) by] [num] ] print ["net sum:" apply [diffs now?] []] ] ; Result: ; net sum: 26 ; Example #3: apply [(now) time second] [] ; Example #4: probe head apply [insert if find ask "Only?(y/n) " "y" only] [copy [] [1]] ; Example #5: a: [1 2 3] apply [a (1 + 1)] [] Any suggestions welcome -Ladislav