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

reduction

 [1/12] from: limhk::acm::org at: 6-Oct-2001 1:30


hi, i have a function like this: replacepl: func [str [string!]] [replace str "^-12^-" "^":^"" replace str "^-F7^-" "^":^"" replace str "^-B8^-" "^":^"" replace str "^-AT^-" ^ :^""] may i know how to reduce it? thanks. regards hk

 [2/12] from: ryanc:iesco-dms at: 5-Oct-2001 11:29


A tad shorter... replacepl: func [str [string!]] [replace replace replace replace str ^-12^- "^":^"" "^-F7^-" "^":^" "^-B8^-" "^":^"" "^-AT^-" "^":^""] --Ryan Lim Hwee Kwang wrote:
> hi, > i have a function like this:
<<quoted lines omitted: 9>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Ryan Cole Programmer Analyst www.iesco-dms.com 707-468-5400

 [3/12] from: ryanc:iesco-dms at: 5-Oct-2001 11:43


And arguably slightly more reduction... replacepl: func [str [string!]] [foreach [a b] ["^-12^-" {":"} "^-F7^-" {":"} "^-B8^-" {":"} "^-AT^-" {":"}] [replace str a b]] --Ryan Ryan Cole wrote:
> A tad shorter... > replacepl: func [str [string!]] [replace replace replace replace str
<<quoted lines omitted: 29>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Ryan Cole Programmer Analyst www.iesco-dms.com 707-468-5400

 [4/12] from: carl:cybercraft at: 6-Oct-2001 10:25


How about... replacepl: func [str [string!] /local r][r: func [s][replace str rejoin ["^-" s "^-"] "^":^""]r "12" r "F7" r "B8" r "AT"] (And what's with the no indenting? :) On 06-Oct-01, Ryan Cole wrote:
> And arguably slightly more reduction... > replacepl: func [str [string!]] [foreach [a b] ["^-12^-" {":"}
<<quoted lines omitted: 24>>
>>> regards >>> hk
-- Carl Read

 [5/12] from: limhk:acm at: 6-Oct-2001 8:36


i use it with map replacepl b to bring into another program with ":" as list concatenator this doesn't work with map. thanks anyway :) hk Carl Read wrote:

 [6/12] from: limhk:acm at: 6-Oct-2001 8:20


thanks. much appreciated. hk Ryan Cole wrote:

 [7/12] from: carl:cybercraft at: 6-Oct-2001 14:15


On 06-Oct-01, Lim Hwee Kwang wrote:
> i use it with map replacepl b to bring into another program with > ":" as list concatenator > this doesn't work with map.
map ? Is that a REBOL/Command word? View says it knows nothing about it. -- Carl Read

 [8/12] from: greggirwin:starband at: 6-Oct-2001 11:38


Hi Carl, << map ? Is that a REBOL/Command word? View says it knows nothing about it. >> Larry Palmiter, and I think Ladislav Mecir, have written map functions which may be what Hwee Kwang was referring to. Here is Larry's: ; Larry Palmiter map: func [fn blk args /local result][ result: copy [] repeat el blk [append/only result fn :el args] return result ] ; Use it like this for a named function (note the : prefix is needed to delay ; full evaluation). ; ; >> map :to-integer ["123" "45667"] ; == [123 45667] ; ; If the named function needs a refinement switch you delay evaluation with a ; leading ' , like this: ; ; >> map 'sine/radians [0 30 90] ; == [0 -0.988031624092862 0.893996663600556] ; ; You can also use it with an anonymous function (defined on the fly) like ; this: ; ; >> map func [x][x * x] [1 2 3] ; == [1 4 9] --Gregg

 [9/12] from: al:bri:xtra at: 7-Oct-2001 8:10


> > map ? Is that a REBOL/Command word? View says it knows nothing about
it.
> Larry Palmiter, and I think Ladislav Mecir, have written map functions
which may be what Hwee Kwang was referring to. And mine is at: http://valley.150m.com/Rebol/Values/Map.r Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [10/12] from: limhk:acm at: 7-Oct-2001 9:36


hi, the one i use has a deep refinement: ===============CODE=============== REBOL [ Title: "Map Function" Date: "24-November-1999" Author: "Larry Palmiter" Purpose: { Maps a function of one arg onto a (nested) block of values. } ] map: func [ Maps a function onto elements of a block :f "function (use parens for literal)" b [block!] "block of values" /deep "maps function into nested blocks" /local out ][ out: make block! length? b if paren? :f [f: f] foreach el b [ either block? el [ either deep [ append/only out map/deep f el ][ append/only out el ] ][ append out f el ] ] ] ===========END OF CODE============= works beautifully. many thanks to Larry Palmiter for a great function! regards hk Gregg Irwin wrote:

 [11/12] from: joel:neely:fedex at: 7-Oct-2001 7:48


Hi, Carl, PMJI... Carl Read wrote:
> map ? Is that a REBOL/Command word? View says it knows nothing > about it. >
We had a long discussion about MAP and other higher-order functions almost exactly a year ago. One other demonstration of MAP that I had fun with (pardon the pun! ;-) is given in http://www.escribe.com/internet/rebol/m2959.html I highly recommend Ladislav's "highfun.r" page, at http://www.sweb.cz/LMecir/highfun.r which contains a generous collection of related REBOL code. -jn- -- ; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677 REBOL [] foreach [order string] sort/skip reduce [ true "!" false head reverse "rekcah" none "REBOL " prin "Just " "another " ] 2 [prin string] print ""

 [12/12] from: joel:neely:fedex at: 7-Oct-2001 8:17


Hi, again, Carl, OBTW... I gave the link to the end of the thread. If you'd like to see how we got there, you can start with http://www.escribe.com/internet/rebol/m2952.html It's a great example of how the REBOL user community can jump in and help refine something into a much better state than it began! To atone for my omission, let me pitch in another higher-order function that can be quite handy. (This name is taken from the corresponding idea in Smalltalk, but the concept has been all over the place in the comp sci world...) The simplest version is: inject: func [ fun [any-function!] val [any-type!] blk [block!] ][ foreach itm blk [val: fun val itm] val ] which "chains" the values in the block onto a starting value via the supplied function. For example:
>> balance: 123.45 == 123.45 >> credits: [15.24 37.46 93.84] == [15.24 37.46 93.84] >> debits: [35.44 13.22 1.95] == [35.44 13.22 1.95]
after which
>> inject :subtract inject :add balance credits debits == 219.38 >> inject :add balance credits == 269.99 >> inject :subtract balance debits == 72.84
and finally, the ambitious
>> inject :subtract inject :add balance credits debits == 219.38
Of course, INJECT and MAP can be combined in fun ways...
>> bbllkk: [
[ [1 3 5 7 9] [ [0 2 4 6 8] [ [0 1 4 9] [ [0 1 8] [ ] == [ [1 3 5 7 9] [0 2 4 6 8] [0 1 4 9] [0 1 8] ]
>> map :length? bbllkk
== [5 5 4 3]
>> inject :+ 0 map :length? bbllkk
== 17 with many more variations possible. Have fun! -jn- Joel Neely wrote:
> Hi, Carl, > PMJI...
<<quoted lines omitted: 20>>
> [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- ; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677 REBOL [] foreach [order string] sort/skip reduce [ true "!" false head reverse "rekcah" none "REBOL " prin "Just " "another " ] 2 [prin string] print ""

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted