Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

:Re: My LOOP is ugly

33 views
Skip to first unread message

Robert L.

unread,
Nov 28, 2016, 5:56:24 AM11/28/16
to
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/

trans

unread,
Nov 28, 2016, 9:37:17 AM11/28/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
And ban this guy too!

Melzzzzz

unread,
Nov 30, 2016, 12:35:57 AM11/30/16
to
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

Paul Rubin

unread,
Nov 30, 2016, 2:31:15 AM11/30/16
to
Melzzzzz <m...@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

Melzzzzz

unread,
Nov 30, 2016, 2:59:52 AM11/30/16
to
On Tue, 29 Nov 2016 23:31:10 -0800
Paul Rubin <no.e...@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!
0 new messages