Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #20156 > unrolled thread
| Started by | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| First post | 2013-03-02 07:48 +0000 |
| Last post | 2013-03-03 22:11 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.forth
Filtering "WJ" <w_a_x_man@yahoo.com> - 2013-03-02 07:48 +0000
Re: Filtering mhx@iae.nl (Marcel Hendrix) - 2013-03-02 11:17 +0200
Re: Filtering "Charles Childers" <crc@retroforth.org> - 2013-03-02 11:18 -0500
Re: Filtering "A. K." <akk@nospam.org> - 2013-03-02 17:55 +0100
Re: Filtering Marc Olschok <nobody@nowhere.invalid> - 2013-03-03 22:11 +0000
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2013-03-02 07:48 +0000 |
| Subject | Filtering |
| Message-ID | <kgsasr0ibq@enews6.newsguy.com> |
Given a sequence of numbers, keep those that are odd and
multiply them by 100.
Factor:
USE: sequences.extras
{ 1 1 2 3 5 8 13 21 } [ odd? ] [ 100 * ] filter-map
{ 100 100 300 500 1300 2100 }
[toc] | [next] | [standalone]
| From | mhx@iae.nl (Marcel Hendrix) |
|---|---|
| Date | 2013-03-02 11:17 +0200 |
| Message-ID | <19189201008434@frunobulax.edu> |
| In reply to | #20156 |
"WJ" <w_a_x_man@yahoo.com> writes Re: Filtering
> Given a sequence of numbers, keep those that are odd and
> multiply them by 100.
> Factor:
> USE: sequences.extras
> { 1 1 2 3 5 8 13 21 } [ odd? ] [ 100 * ] filter-map
> { 100 100 300 500 1300 2100 }
8 integer array x{ 1 1 2 3 5 8 13 21 x{ #=>
: fm ( x -- ) dup cdim 0 ?do dup i } @ dup 1 and if 100 * . else drop endif loop drop ;
x{ fm 100 100 300 500 1300 2100 ok
Or:
: fm BEGIN bl word dup C@
WHILE count evaluate dup 1 and IF 100 * . ELSE drop ENDIF
REPEAT drop ;
FORTH> fm 1 1 2 3 5 8 13 21 100 100 300 500 1300 2100 ok
FORTH> hex fm 1 f 2e 333 331 21fe 64 5DC 13FEC 13F24 ok
FORTH> fm %01 %1 #10 %11 %1001 %1000 #13 $15 100 100 300 900 1300 2100 ok
-marcel
[toc] | [prev] | [next] | [standalone]
| From | "Charles Childers" <crc@retroforth.org> |
|---|---|
| Date | 2013-03-02 11:18 -0500 |
| Message-ID | <op.wtbt9eof6ef20b@denney-2466a6df> |
| In reply to | #20156 |
On Sat, 02 Mar 2013 02:48:43 -0500, WJ <w_a_x_man@yahoo.com> wrote:
> Given a sequence of numbers, keep those that are odd and
> multiply them by 100.
>
> Factor:
>
>
> USE: sequences.extras
>
> { 1 1 2 3 5 8 13 21 } [ odd? ] [ 100 * ] filter-map
> { 100 100 300 500 1300 2100 }
Parable:
[ #1 #1 #2 #3 #5 #8 #13 #21 ] array-from-quote
[ odd? ] array-filter [ #10 * ] array-map
Retro:
needs array'
needs math'
with| array' math' |
create results 100 allot
new{ 1 1 2 3 5 8 13 21 }
[ dup odd? [ 100 * @results results + 1+ ! results ++ ] ifTrue ] apply
results display
-- crc
[toc] | [prev] | [next] | [standalone]
| From | "A. K." <akk@nospam.org> |
|---|---|
| Date | 2013-03-02 17:55 +0100 |
| Message-ID | <51322f08$0$9517$9b4e6d93@newsspool1.arcor-online.net> |
| In reply to | #20174 |
On 02.03.2013 17:18, Charles Childers wrote:
> On Sat, 02 Mar 2013 02:48:43 -0500, WJ <w_a_x_man@yahoo.com> wrote:
>
>> Given a sequence of numbers, keep those that are odd and
>> multiply them by 100.
>>
>> Factor:
>>
>>
>> USE: sequences.extras
>>
>> { 1 1 2 3 5 8 13 21 } [ odd? ] [ 100 * ] filter-map
>> { 100 100 300 500 1300 2100 }
>
>
> Parable:
>
> [ #1 #1 #2 #3 #5 #8 #13 #21 ] array-from-quote
> [ odd? ] array-filter [ #10 * ] array-map
>
>
> Retro:
>
> needs array'
> needs math'
> with| array' math' |
>
> create results 100 allot
> new{ 1 1 2 3 5 8 13 21 }
> [ dup odd? [ 100 * @results results + 1+ ! results ++ ] ifTrue ] apply
> results display
>
>
> -- crc
I am pretty sure Ruby or Haskell could do it even shorter
AND !!!
more readable. ;-)))
[toc] | [prev] | [next] | [standalone]
| From | Marc Olschok <nobody@nowhere.invalid> |
|---|---|
| Date | 2013-03-03 22:11 +0000 |
| Message-ID | <kh0hq4$7av$1@news.albasani.net> |
| In reply to | #20176 |
A. K. <akk@nospam.org> wrote:
> On 02.03.2013 17:18, Charles Childers wrote:
> > On Sat, 02 Mar 2013 02:48:43 -0500, WJ <w_a_x_man@yahoo.com> wrote:
> >
> >> Given a sequence of numbers, keep those that are odd and
> >> multiply them by 100.
> >>
> >> Factor:
> >>
> >>
> >> USE: sequences.extras
> >>
> >> { 1 1 2 3 5 8 13 21 } [ odd? ] [ 100 * ] filter-map
> >> { 100 100 300 500 1300 2100 }
> >
> >
> > Parable:
> >
> > [ #1 #1 #2 #3 #5 #8 #13 #21 ] array-from-quote
> > [ odd? ] array-filter [ #10 * ] array-map
> >
> >
> > Retro:
> >
> > needs array'
> > needs math'
> > with| array' math' |
> >
> > create results 100 allot
> > new{ 1 1 2 3 5 8 13 21 }
> > [ dup odd? [ 100 * @results results + 1+ ! results ++ ] ifTrue ] apply
> > results display
> >
> >
> > -- crc
>
> I am pretty sure Ruby or Haskell could do it even shorter
>
> AND !!!
>
> more readable. ;-)))
Not much shorter I guess. E.g. in Haskell you could use
map (100*) $ filter odd [1,1,2,3,5,8,13,21]
which is quite close to the Factor example, or you could use
[ 100*x | x <- [1,1,2,3,5,8,13,21], odd x ]
which is perhaps the most readable to non-programmers.
And of course there is always Postscript:
[ [1 1 2 3 5 8 13 21] {dup 2 mod 0 eq {pop} {100 mul} ifelse} forall ]
But I do not see much difference in terms of readability, except that
Forth solutions may need to set up the data structures explicitly.
And I was surprised that Retroforth now has an array notation similar
to Postcript; I did not have time to follow its development in recent
years, it looks very interesting.
--
Marc
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.forth
csiph-web