Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #52918 > unrolled thread

:Re: My LOOP is ugly

Started by"Robert L." <No_spamming@noWhere_7073.org>
First post2016-11-28 10:56 +0000
Last post2016-11-30 08:59 +0100
Articles 5 — 4 participants

Back to article view | Back to comp.lang.forth


Contents

  :Re: My LOOP is ugly "Robert L." <No_spamming@noWhere_7073.org> - 2016-11-28 10:56 +0000
    Re: :Re: My LOOP is ugly trans <transfire@gmail.com> - 2016-11-28 06:37 -0800
    Re: :Re: My LOOP is ugly Melzzzzz <mel@zzzzz.com> - 2016-11-30 06:35 +0100
      Re: :Re: My LOOP is ugly Paul Rubin <no.email@nospam.invalid> - 2016-11-29 23:31 -0800
        Re: :Re: My LOOP is ugly Melzzzzz <mel@zzzzz.com> - 2016-11-30 08:59 +0100

#52918 — :Re: My LOOP is ugly

From"Robert L." <No_spamming@noWhere_7073.org>
Date2016-11-28 10:56 +0000
Subject:Re: My LOOP is ugly
Message-ID<o1h2gm$qse$1@gioia.aioe.org>
Kenny Tilton wrote:

> (defun p2b (pairs &key ((:test test) #'eql))
>    "((A 1) (A 2) (B 2) (C 2) (C 3)) ==> ((A 1 2) (B 2) (C 2 3))"
>    (loop with bunch = nil
>          for (one two) in pairs
>          do (push two (cdr (or (assoc one bunch :test test)
>                              (car (push (list one) bunch)))))
>          finally (return bunch)))

Testing:

* (p2b '((A 1) (A 2) (B 2) (C 2) (C 3)))

((C 3 2) (B 2) (A 2 1))

Ruby:

def p2b(pairs)
  pairs.group_by{|k,v|
    block_given? ? yield(k) : k
  }.map{|k,v| [k, *v.map(&:last)]}
end

p2b [[:A,1], [:A,2], [:B,2], [:C,2], [:C,3]]
 ===>
[[:A, 1, 2], [:B, 2], [:C, 2, 3]]


p2b([['Foo',2],['bar',4],['foo',22],['BAR',44]]){|s| s.downcase}
 ===>
[["foo", 2, 22], ["bar", 4, 44]]


In Forth?

-- 
[T]he number of Jews killed there was 800,000,000.... [T]he Talmud makes it all
clear by informing us that the blood of the holocausted Jews ran to the sea in
a huge tidal wave that swept boulders in its path and was so deep that it
reached the nostrils of the Romans' horses.
nationalvanguard.org/2014/09/disillusioned-part-1/

[toc] | [next] | [standalone]


#52923

Fromtrans <transfire@gmail.com>
Date2016-11-28 06:37 -0800
Message-ID<fff1d9fe-301e-468a-a3e4-9a3cb0e8ac57@googlegroups.com>
In reply to#52918
On Monday, November 28, 2016 at 5:56:24 AM UTC-5, Robert L. wrote:
> Kenny Tilton wrote:
> 
> > (defun p2b (pairs &key ((:test test) #'eql))
> >    "((A 1) (A 2) (B 2) (C 2) (C 3)) ==> ((A 1 2) (B 2) (C 2 3))"
> >    (loop with bunch = nil
> >          for (one two) in pairs
> >          do (push two (cdr (or (assoc one bunch :test test)
> >                              (car (push (list one) bunch)))))
> >          finally (return bunch)))
> 
> Testing:
> 
> * (p2b '((A 1) (A 2) (B 2) (C 2) (C 3)))
> 
> ((C 3 2) (B 2) (A 2 1))
> 
> Ruby:
> 
> def p2b(pairs)
>   pairs.group_by{|k,v|
>     block_given? ? yield(k) : k
>   }.map{|k,v| [k, *v.map(&:last)]}
> end
> 
> p2b [[:A,1], [:A,2], [:B,2], [:C,2], [:C,3]]
>  ===>
> [[:A, 1, 2], [:B, 2], [:C, 2, 3]]
> 
> 
> p2b([['Foo',2],['bar',4],['foo',22],['BAR',44]]){|s| s.downcase}
>  ===>
> [["foo", 2, 22], ["bar", 4, 44]]
> 
> 
> In Forth?
> 
> -- 
> [T]he number of Jews killed there was 800,000,000.... [T]he Talmud makes it all
> clear by informing us that the blood of the holocausted Jews ran to the sea in
> a huge tidal wave that swept boulders in its path and was so deep that it
> reached the nostrils of the Romans' horses.
> nationalvanguard.org/2014/09/disillusioned-part-1/

And ban this guy too!

[toc] | [prev] | [next] | [standalone]


#52977

FromMelzzzzz <mel@zzzzz.com>
Date2016-11-30 06:35 +0100
Message-ID<20161130063555.3d621465@maxa-pc>
In reply to#52918
On Mon, 28 Nov 2016 10:56:23 +0000 (UTC)
"Robert L." <No_spamming@noWhere_7073.org> wrote:

> Kenny Tilton wrote:
> 
> > (defun p2b (pairs &key ((:test test) #'eql))
> >    "((A 1) (A 2) (B 2) (C 2) (C 3)) ==> ((A 1 2) (B 2) (C 2 3))"
> >    (loop with bunch = nil
> >          for (one two) in pairs
> >          do (push two (cdr (or (assoc one bunch :test test)
> >                              (car (push (list one) bunch)))))
> >          finally (return bunch)))  
> 
> Testing:
> 
> * (p2b '((A 1) (A 2) (B 2) (C 2) (C 3)))
> 
> ((C 3 2) (B 2) (A 2 1))
> 
> Ruby:
> 
> def p2b(pairs)
>   pairs.group_by{|k,v|
>     block_given? ? yield(k) : k
>   }.map{|k,v| [k, *v.map(&:last)]}
> end
> 
> p2b [[:A,1], [:A,2], [:B,2], [:C,2], [:C,3]]
>  ===>  
> [[:A, 1, 2], [:B, 2], [:C, 2, 3]]
> 
> 
> p2b([['Foo',2],['bar',4],['foo',22],['BAR',44]]){|s| s.downcase}
>  ===>  
> [["foo", 2, 22], ["bar", 4, 44]]
> 
> 
> In Forth?
> 
Haskell:

import Data.List (groupBy,sort)
import Data.Char (toUpper)
p2b :: (Eq a,Ord a,Ord b)=>[(a,b)]->[(a,[b])]
p2b = let f :: [(a,b)] -> (a,[b])
          f x = foldl (\(a,b) (_,y) -> (a,y:b)) (fst.head $ x,[snd.head $ x]) (tail x)
          in map (\x->(fst $ f x,sort.snd $ f x))
            . groupBy (\x y-> fst x == fst y ) . sort 
main = do
    print $ p2b [('C',3),('A',1),('B',2),('A',2),('C',2)]
    print $ p2b $ map (\(x,y)->(map toUpper x,y)) 
                 [("bar",44),("Foo",2),("foo",22),("BAR",4)]

[bmaxa@maxa-pc WJ]$ ./pairs
[('A',[1,2]),('B',[2]),('C',[2,3])]
[("BAR",[4,44]),("FOO",[2,22])]

-- 
press any key to continue or any other to quit

[toc] | [prev] | [next] | [standalone]


#52980

FromPaul Rubin <no.email@nospam.invalid>
Date2016-11-29 23:31 -0800
Message-ID<87eg1t77b5.fsf@nightsong.com>
In reply to#52977
Melzzzzz <mel@zzzzz.com> writes:
> Haskell:
> ....
> p2b = let f :: [(a,b)] -> (a,[b])
>           f x = foldl (\(a,b) (_,y) -> (a,y:b)) (fst.head $ x,[snd.head $ x]) (tail x)
>           in map (\x->(fst $ f x,sort.snd $ f x))
>             . groupBy (\x y-> fst x == fst y ) . sort 
> ...

{-# LANGUAGE TransformListComp #-}
import GHC.Exts (sortWith, groupWith, the)
import Data.Char (toUpper)
import Data.Function (on)

p2b xs = [ (the a, b) | (a,b) <- xs
         , then sortWith by b
         , then group by a using groupWith 
         ]

t1 = [('C',3),('A',1),('B',2),('A',2),('C',2)]
t2 = [("bar",44),("Foo",2),("foo",22),("BAR",4)]

main = do
  print . p2b $ t1
  print . p2b $ [(map toUpper a, b) | (a,b) <- t2]

================================================================

*Main Prelude> main
[('A',[1,2]),('B',[2]),('C',[2,3])]
[("BAR",[4,44]),("FOO",[2,22])]

[toc] | [prev] | [next] | [standalone]


#52982

FromMelzzzzz <mel@zzzzz.com>
Date2016-11-30 08:59 +0100
Message-ID<20161130085951.15a09a6b@maxa-pc>
In reply to#52980
On Tue, 29 Nov 2016 23:31:10 -0800
Paul Rubin <no.email@nospam.invalid> wrote:

> {-# LANGUAGE TransformListComp #-}
> import GHC.Exts (sortWith, groupWith, the)
> import Data.Char (toUpper)
> import Data.Function (on)
> 
> p2b xs = [ (the a, b) | (a,b) <- xs
>          , then sortWith by b
>          , then group by a using groupWith 
>          ]
> 
> t1 = [('C',3),('A',1),('B',2),('A',2),('C',2)]
> t2 = [("bar",44),("Foo",2),("foo",22),("BAR",4)]
> 
> main = do
>   print . p2b $ t1
>   print . p2b $ [(map toUpper a, b) | (a,b) <- t2]

Wow! Really nice!

-- 
press any key to continue or any other to quit

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web