[Factor-talk] Best way to split a fixed locations
Hi, I need to split a string at fixed locations (some of the locations may eventully be calculated, like with a lookup of '/', but at first approximation fixed locations are ok). I came with this example string and quotatiom: NAXIS =3 / number of data axes [ 0 swap 8 swap subseq ] [ 10 swap 30 swap subseq ] [ 33 swap 80 swap subseq ] tri [ [ 32 = ] trim ] tri@ This works, does the split and trim, but I am not too happy with the multiple swaps. I would liek to keep stack manipulation to the minimum for clarity. Any recommendation ? hb9duj -- Managing the Performance of Cloud-Based Applications Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. Read the Whitepaper. http://pubads.g.doubleclick.net/gampad/clk?id=121051231iu=/4140/ostg.clktrk___ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk
Re: [Factor-talk] Best way to split a fixed locations
Maybe something like this: : subseqs ( indices seq -- subseqs ) [ subseq [ CHAR: \s = ] trim ] curry { } assocmap ; You can see it works by returning an array of subseqs: IN: scratchpad NAXIS =3 / number of data axes { { 0 8 } { 10 30 } { 33 80 } } swap subseqs . { NAXIS 3 number of data axes } On Sun, Feb 9, 2014 at 5:41 AM, Jean-Marc Lugrin hb9...@lugrin.ch wrote: Hi, I need to split a string at fixed locations (some of the locations may eventully be calculated, like with a lookup of '/', but at first approximation fixed locations are ok). I came with this example string and quotatiom: NAXIS =3 / number of data axes [ 0 swap 8 swap subseq ] [ 10 swap 30 swap subseq ] [ 33 swap 80 swap subseq ] tri [ [ 32 = ] trim ] tri@ This works, does the split and trim, but I am not too happy with the multiple swaps. I would liek to keep stack manipulation to the minimum for clarity. Any recommendation ? hb9duj -- Managing the Performance of Cloud-Based Applications Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. Read the Whitepaper. http://pubads.g.doubleclick.net/gampad/clk?id=121051231iu=/4140/ostg.clktrk ___ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk -- Managing the Performance of Cloud-Based Applications Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. Read the Whitepaper. http://pubads.g.doubleclick.net/gampad/clk?id=121051231iu=/4140/ostg.clktrk___ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk
Re: [Factor-talk] Best way to split a fixed locations
Or locals: :: foo ( seq -- a b c ) 0 8 seq subseq 10 30 seq subseq 33 80 seq subseq [ CHAR: \s = ] tri@ ; On Sun, Feb 9, 2014 at 7:06 AM, John Benediktsson mrj...@gmail.com wrote: Maybe something like this: : subseqs ( indices seq -- subseqs ) [ subseq [ CHAR: \s = ] trim ] curry { } assocmap ; You can see it works by returning an array of subseqs: IN: scratchpad NAXIS =3 / number of data axes { { 0 8 } { 10 30 } { 33 80 } } swap subseqs . { NAXIS 3 number of data axes } On Sun, Feb 9, 2014 at 5:41 AM, Jean-Marc Lugrin hb9...@lugrin.ch wrote: Hi, I need to split a string at fixed locations (some of the locations may eventully be calculated, like with a lookup of '/', but at first approximation fixed locations are ok). I came with this example string and quotatiom: NAXIS =3 / number of data axes [ 0 swap 8 swap subseq ] [ 10 swap 30 swap subseq ] [ 33 swap 80 swap subseq ] tri [ [ 32 = ] trim ] tri@ This works, does the split and trim, but I am not too happy with the multiple swaps. I would liek to keep stack manipulation to the minimum for clarity. Any recommendation ? hb9duj -- Managing the Performance of Cloud-Based Applications Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. Read the Whitepaper. http://pubads.g.doubleclick.net/gampad/clk?id=121051231iu=/4140/ostg.clktrk ___ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk -- Managing the Performance of Cloud-Based Applications Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. Read the Whitepaper. http://pubads.g.doubleclick.net/gampad/clk?id=121051231iu=/4140/ostg.clktrk___ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk
Re: [Factor-talk] Best way to split a fixed locations
Hi, you can get rid of most of the repetition by defining a word that operates on lists instead of using tri/tri@, for example: : subseqs ( seq indices -- subseqs ) swap [ subseq ] curry { } assocmap ; NAXIS =3 / number of data axes { { 0 8 } { 10 30 } { 33 80 } } subseqs Also, [ 32 = ] trim is maybe more readable written as [ CHAR: space = ] trim, or if you also want to trim tabs and \n\r, the library defines [ blank? ] trim So for example, you can use: NAXIS =3 / number of data axes { { 0 8 } { 10 30 } { 33 80 } } subseqs [ [ blank? ] trim ] map first3 Note that the question you are asking if farily low level, so you might find better methods to extract the info you want (for example split-harvest from splitting.extras could be useful in your case) Cheers, Jon On Sun, Feb 9, 2014 at 2:41 PM, Jean-Marc Lugrin hb9...@lugrin.ch wrote: Hi, I need to split a string at fixed locations (some of the locations may eventully be calculated, like with a lookup of '/', but at first approximation fixed locations are ok). I came with this example string and quotatiom: NAXIS =3 / number of data axes [ 0 swap 8 swap subseq ] [ 10 swap 30 swap subseq ] [ 33 swap 80 swap subseq ] tri [ [ 32 = ] trim ] tri@ This works, does the split and trim, but I am not too happy with the multiple swaps. I would liek to keep stack manipulation to the minimum for clarity. Any recommendation ? hb9duj -- Managing the Performance of Cloud-Based Applications Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. Read the Whitepaper. http://pubads.g.doubleclick.net/gampad/clk?id=121051231iu=/4140/ostg.clktrk ___ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk -- Managing the Performance of Cloud-Based Applications Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. Read the Whitepaper. http://pubads.g.doubleclick.net/gampad/clk?id=121051231iu=/4140/ostg.clktrk___ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk
Re: [Factor-talk] Best way to split a fixed locations
2014-02-09 14:41 GMT+01:00 Jean-Marc Lugrin hb9...@lugrin.ch: Hi, I need to split a string at fixed locations (some of the locations may eventully be calculated, like with a lookup of '/', but at first approximation fixed locations are ok). I came with this example string and quotatiom: NAXIS =3 / number of data axes [ 0 swap 8 swap subseq ] [ 10 swap 30 swap subseq ] [ 33 swap 80 swap subseq ] tri [ [ 32 = ] trim ] tri@ Some more variants for you to consider: Using fry: : subseqs ( indices seq -- subseqs ) '[ first2 _ subseq [ blank? ] trim ] map ; Using with: : subseqs ( seq indices -- subseqs ) [ first2 rot subseq [ blank? ] trim ] with map ; Also Jon's idea of finding higher-level splitting words to work with is really good. E.g. IN: NAXIS =3 / number of data axes IN: /= split [ [ blank? ] trim ] map . { NAXIS 3 number of data axes } -- mvh/best regards Björn Lindqvist -- Managing the Performance of Cloud-Based Applications Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. Read the Whitepaper. http://pubads.g.doubleclick.net/gampad/clk?id=121051231iu=/4140/ostg.clktrk ___ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk