Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #23636
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: Simple problem |
| Date | 2013-06-15 00:53 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <kpgduo$t7o$1@dont-email.me> (permalink) |
| References | <kgs058018sm@enews4.newsguy.com> <ki69er$51f$1@dont-email.me> |
WJ wrote:
> WJ wrote:
>
> >
> > Given a list of strings, count the number of times each string
> > occurs. Sort the result from most to least common.
> >
> > Factor:
> >
> >
> > USING: locals ;
> >
> > :: counts ( seq -- seq )
> > H{ } clone :> table
> > seq [ table inc-at ] each
> > table >alist [ [ last ] bi@ swap <=> ] sort
> > dup .
> > ;
> >
> > { "c" "c" "a" "b" "b" "c" } counts
> >
> > { { "c" 3 } { "b" 2 } { "a" 1 } }
>
> Made shorter by using >=< instead of <=>.
>
> USING: locals ;
>
> :: counts ( seq -- seq )
> H{ } clone :> table
> seq [ table inc-at ] each
> table >alist [ [ last ] bi@ >=< ] sort
> ;
>
> The doc. for <=> says:
>
> Compares two objects using an intrinsic linear order, for
> example, the natural order for real numbers and lexicographic
> order for strings.
>
> The output value is one of the following:
> +lt+ - indicating that obj1 precedes obj2
> +eq+ - indicating that obj1 is equal to obj2
> +gt+ - indicating that obj1 follows obj2
>
> The doc. for >=< says:
>
> Compares two objects using the <=> comparator and inverts the
> output.
Ruby:
["c", "c", "a", "b", "b", "c"].group_by{|x| x}.map{|k,v| [k,v.size]}.
sort_by{|k,v| -v}
==>[["c", 3], ["b", 2], ["a", 1]]
Can this be done in Forth? Or is Forth merely a stone-age toy?
Back to comp.lang.forth | Previous | Next — Next in thread | Find similar
Re: Simple problem "WJ" <w_a_x_man@yahoo.com> - 2013-06-15 00:53 +0000
Re: Simple problem visualforth@rocketmail.com - 2013-06-21 20:14 -0700
Re: Simple problem glidedog@gmail.com - 2013-06-22 06:01 -0700
Re: Simple problem rickman <gnuarm@gmail.com> - 2013-06-22 09:48 -0400
Re: Simple problem Bernd Paysan <bernd.paysan@gmx.de> - 2013-06-22 22:55 +0200
Re: Simple problem "Elizabeth D. Rather" <erather@forth.com> - 2013-06-22 11:47 -1000
csiph-web