Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #20152 > unrolled thread
| Started by | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| First post | 2013-03-02 04:45 +0000 |
| Last post | 2013-07-04 10:03 +0000 |
| Articles | 14 — 9 participants |
Back to article view | Back to comp.lang.forth
Simple problem "WJ" <w_a_x_man@yahoo.com> - 2013-03-02 04:45 +0000
Re: Simple problem Doug Hoffman <glidedog@gmail.com> - 2013-03-02 06:16 -0500
Re: Simple problem "WJ" <w_a_x_man@yahoo.com> - 2013-03-18 05:41 +0000
Re: Simple problem albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-03-18 11:23 +0000
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
Re: Simple problem "WJ" <w_a_x_man@yahoo.com> - 2013-04-02 08:22 +0000
Re: Simple problem "WJ" <w_a_x_man@yahoo.com> - 2013-07-04 03:24 +0000
Re: Simple problem Paul Rubin <no.email@nospam.invalid> - 2013-07-03 21:32 -0700
Re: Simple problem "WJ" <w_a_x_man@yahoo.com> - 2013-07-04 10:03 +0000
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2013-03-02 04:45 +0000 |
| Subject | Simple problem |
| Message-ID | <kgs058018sm@enews4.newsguy.com> |
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 } }
[toc] | [next] | [standalone]
| From | Doug Hoffman <glidedog@gmail.com> |
|---|---|
| Date | 2013-03-02 06:16 -0500 |
| Message-ID | <5131dfa8$0$281$14726298@news.sunsite.dk> |
| In reply to | #20152 |
Given any program, run for an arbitrary interval, track the frequency of use of each message send and display the results. ANS Forth: showUse 231 init: 595 free: 137 !: 135 @: 533 at: 108 to: 164 add: 227 new: 141 size: 137 resize: 108 obj: 367 reset: 198 rem: 89 end: 135 @sub: 136 skipChars: 136 CIword: 136 word: 147 get: 9 p: 35 ptr: 27 @elem: 108 error?: 2 map: 31 start: 243 ?idx: 62 upper: 31 CIsearch: 31 search: 1 open?: 1 name: 1 open: 1 read: 1 close:ok
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2013-03-18 05:41 +0000 |
| Message-ID | <ki69er$51f$1@dont-email.me> |
| In reply to | #20152 |
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.
[toc] | [prev] | [next] | [standalone]
| From | albert@spenarnc.xs4all.nl (Albert van der Horst) |
|---|---|
| Date | 2013-03-18 11:23 +0000 |
| Message-ID | <5146f92d$0$26909$e4fe514c@dreader37.news.xs4all.nl> |
| In reply to | #20770 |
In article <ki69er$51f$1@dont-email.me>, WJ <w_a_x_man@yahoo.com> 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.
Okay, WJ. Now show us how to do this on a real text body like
"Moby Dick" like other posters did. Then tell us some
comparison between Factor and some Forth on you machine
time wise.
Groetjes Albert
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2013-06-15 00:53 +0000 |
| Message-ID | <kpgduo$t7o$1@dont-email.me> |
| In reply to | #20770 |
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?
[toc] | [prev] | [next] | [standalone]
| From | visualforth@rocketmail.com |
|---|---|
| Date | 2013-06-21 20:14 -0700 |
| Message-ID | <d4f34c27-de57-401c-a589-9ac562f0476a@googlegroups.com> |
| In reply to | #23636 |
On Friday, June 14, 2013 8:53:46 PM UTC-4, WJ wrote:
> 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?
What about diamond which has been cut, honed, and polished in the stone-age?
Would you reject such a diamond or would you embrace it?
Forth has been created in the stone-age of programming, in the late sixties.
Ruby came up in 1995, but before 1995 you could use Forth commands that do the job, and they still do it now.
Forth is extensible, so it would be not a big job to create a Forth command which just does this. And there has been published a solution to solve this problem with a few Forth commands IIRC. May be somebody else remembers.
I don't have the time to write this, but one way to do this is to use create does> to define a word which makes each unknown word a variable which counts itself up when used, and INCLUDE the text. When ready, list these variables and their values. Then you have it.
This would be my approach to this problem. Every Forth programmer should be able to write this in a few lines, two or three.
Can you explain how you did this in Ruby to make your lines understandable for somebody who is not knowledgeable of Ruby?
[toc] | [prev] | [next] | [standalone]
| From | glidedog@gmail.com |
|---|---|
| Date | 2013-06-22 06:01 -0700 |
| Message-ID | <44032651-1a89-4461-88b1-2d7be88b2944@googlegroups.com> |
| In reply to | #23850 |
On Friday, June 21, 2013 11:14:27 PM UTC-4, visua...@rocketmail.com wrote: [snip] Responding to him is useless. Despite the numerous excellent Forth examples posted as replies he never acknowledges them and just keeps on disparaging Forth and posting more non-Forth toy examples, many that he has posted previously in comp.lang.lisp and other areas. He is ignored in the other newsgroups. I suspect he has Asperger's syndrome. -Doug
[toc] | [prev] | [next] | [standalone]
| From | rickman <gnuarm@gmail.com> |
|---|---|
| Date | 2013-06-22 09:48 -0400 |
| Message-ID | <kq49lh$d17$1@dont-email.me> |
| In reply to | #23852 |
On 6/22/2013 9:01 AM, glidedog@gmail.com wrote: > On Friday, June 21, 2013 11:14:27 PM UTC-4, visua...@rocketmail.com wrote: > > [snip] > > Responding to him is useless. Despite the numerous excellent > Forth examples posted as replies he never acknowledges them > and just keeps on disparaging Forth and posting more non-Forth > toy examples, many that he has posted previously in comp.lang.lisp > and other areas. He is ignored in the other newsgroups. > > I suspect he has Asperger's syndrome. I don't know much about Asperger's, but he seems to want to take issue with other people's posts, mostly in regard to the rules of usenet. He posted this exact text in response to at least two posts... I broke your lines for you. Anyone who knows anything whatsoever about usenet knows that he must limit the length of his lines. In one case he was responding to my post where I showed a code segment and he responded with a code segment in ruby. I see I responded to what I perceived as rudeness with more rudeness. His code segment was there to solve my problem. Now I feel bad... -- Rick
[toc] | [prev] | [next] | [standalone]
| From | Bernd Paysan <bernd.paysan@gmx.de> |
|---|---|
| Date | 2013-06-22 22:55 +0200 |
| Message-ID | <kq52vh$8sl$1@online.de> |
| In reply to | #23852 |
glidedog@gmail.com wrote: > I suspect he has Asperger's syndrome. He would post much more brilliant code if he did; Asperger is a mild form of autism, and comes with some of the boost of specialist brain capabilities. I think he just has asshole syndrome. -- Bernd Paysan "If you want it done right, you have to do it yourself" http://bernd-paysan.de/
[toc] | [prev] | [next] | [standalone]
| From | "Elizabeth D. Rather" <erather@forth.com> |
|---|---|
| Date | 2013-06-22 11:47 -1000 |
| Message-ID | <BfmdnW3eJ9bEhlvMnZ2dnUVZ_o-dnZ2d@supernews.com> |
| In reply to | #23858 |
On 6/22/13 10:55 AM, Bernd Paysan wrote: > glidedog@gmail.com wrote: >> I suspect he has Asperger's syndrome. > > He would post much more brilliant code if he did; Asperger is a mild form of > autism, and comes with some of the boost of specialist brain capabilities. > I think he just has asshole syndrome. > Meh, it's not always specialized *brilliance* that results, sometimes just obsessive behavior. I can think of at least one other probable case here. Cheers, Elizabeth -- ================================================== Elizabeth D. Rather (US & Canada) 800-55-FORTH FORTH Inc. +1 310.999.6784 5959 West Century Blvd. Suite 700 Los Angeles, CA 90045 http://www.forth.com "Forth-based products and Services for real-time applications since 1973." ==================================================
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2013-04-02 08:22 +0000 |
| Message-ID | <kje4go$56m$1@dont-email.me> |
| In reply to | #20152 |
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 } }
USING: math.statistics ;
: counts ( seq -- seq )
[ ] collect-by >alist
[ unclip-last length suffix ] map
[ [ last ] bi@ >=< ] sort
;
{ "c" "c" "aa" "b" "b" "c" } counts .
{ { "c" 3 } { "b" 2 } { "aa" 1 } }
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2013-07-04 03:24 +0000 |
| Message-ID | <kr2pt0$e2q$1@dont-email.me> |
| In reply to | #20152 |
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 } }
OCaml:
let update_tbl tbl s =
if Hashtbl.mem tbl s then
Hashtbl.replace tbl s ((Hashtbl.find tbl s) + 1)
else
Hashtbl.add tbl s 1 ;;
let counts s_list =
let tbl = Hashtbl.create 99 in
( List.iter (fun s -> update_tbl tbl s) s_list;
List.sort (fun x y -> (snd y) - (snd x))
(Hashtbl.fold (fun k v acc -> (k,v):: acc) tbl [])
) ;;
counts ["a";"b";"a";"c";"c";"c"];;
==> [("c", 3); ("a", 2); ("b", 1)]
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2013-07-03 21:32 -0700 |
| Message-ID | <7xr4ffx7uz.fsf@ruckus.brouhaha.com> |
| In reply to | #24137 |
"WJ" <w_a_x_man@yahoo.com> writes:
> OCaml:
> let update_tbl tbl s =
> if Hashtbl.mem tbl s then ...
That is horrendous, Java code written in Ocaml. Haskell:
import Data.Map(insertWith,empty,toList)
counts = toList . foldr (\x->insertWith (+) x 1) empty
Output:
> counts ["a","b","a","c","c","c"]
==> [("a",2),("b",1),("c",3)]
Alternatively:
import Data.List(group,sort)
import Control.Arrow((&&&))
counts = map (head &&& length) . group . sort
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2013-07-04 10:03 +0000 |
| Message-ID | <kr3h9l$egs$1@dont-email.me> |
| In reply to | #20152 |
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 } }
Clojure:
(sort-by (comp - last)
(into () (frequencies ["a" "b" "ccc" "a" "b" "a"])))
==> (["a" 3] ["b" 2] ["ccc" 1])
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.forth
csiph-web