Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Ruby is beautiful

Name: Anonymous 2009-08-26 15:25

class Cycle
    def cycle(*args)
      args[((@_cycles ||= Hash.new(-1))[args.hash] += 1) % args.size]
    end
end

More beautiful code plees?

Name: Anonymous 2009-08-26 15:27

I want to see the same in Python.
In Perl you dont need a shit class for that

Name: Anonymous 2009-08-26 15:28

>>2

Don't need a class. I just put it there.

Name: Haxus the Beautiful 2009-08-26 15:39

mov ax, 4C00
int 21

Name: Anonymous 2009-08-26 15:42

args[((@_cycles ||= Hash.new(-1))[args.hash] += 1) % args.size]
That looks terrible!

IHBT?

Name: Anonymous 2009-08-26 15:46

>>5

I would say it's quite pretty for what it does.

Name: Anonymous 2009-08-26 16:33

pretty
beatiful

lawd

Name: Anonymous 2009-08-26 16:41

This is unfunctional and ultimately destructive. And here's hoping you have a good hash function, moron.

Name: Anonymous 2009-08-26 18:31

Sorry but no self-respecting rubyist would write discuss beautiful code today, we are still mourning _why.

Name: Anonymous 2009-08-26 19:44

>>9
BUMP FOR MENTIONING _WHY BECAUSE IT'S COOL TO MENTION _WHY AND IF I BUMP THIS THREAD THEN I CAN BE COOL TOO





This is what ruby hipsters actually believe

Name: Anonymous 2009-08-26 21:00

>>10
That's not funny. He even deleted his livejournal.

Name: UMH memesmith !gNlkr4vCuc 2009-08-26 22:22

# I documented this just for you, /prog/.  What now, >>1?
class Cycle
  def cycle(*args)      # All args are passed as an array object
    # Assign a new hashtable to the @cycles instance variable if it
    # doesn't already exist.  If you try to access @cycles[x] for any
    # key x that is not already in @cycles, a default value of -1 will
    # automatically be inserted.  By the way, there's absolutely no
    # reason to use an underscore at the beginning of an ivar name.
    # The @ already marks it as an ivar.
    @cycles ||= Hash.new(-1)

    h = args.hash       # Hashcode, not array-as-hashtable
    @cycles[h] += 1     # Tragically, there's no ++ in ruby

    # Have you studied your modular arithmetic today?  By the way,
    # this is an implicit return statement
    args[@cycles[h] % args.size]
  end
end

# Now to demonstrate what it actually does
foo = Cycle.new
foo.cycle :hax, :my, :anus # => :hax
foo.cycle :hax, :my, :anus # => :my
foo.cycle :achieve, :satori # => :achieve
foo.cycle :hax, :my, :anus # => :anus
foo.cycle :hax, :my, :anus # => :hax

Name: Anonymous 2009-08-26 22:42

>>12
Um.... Thanks?

Name: Anonymous 2009-08-27 0:51

>>1
If what >>12 said was right, here's how to do it in CL:

(let ((ht (make-hash-table :test #'equal)))
  (defun cycle (&rest args)
    (unless (gethash args ht)
      (setf (gethash args ht) args))
    (pop (gethash args ht))))


And test run:

CL-USER> (cycle :a :b :c) ; => :A
CL-USER> (cycle :a :b :c) ; => :B
(cycle :a :b :c) ; => :C
(cycle :a :b :c) ; => :A
(cycle :a :b :c) ; => :B
(cycle :a :b :c) ; => :C
(cycle :a :b :c :d) ; => :A
(cycle :a :b :c :d) ; => :B
(cycle :a :b :c :d) ; => :C
(cycle :a :b :c :d) ; => :D
(cycle :a :b :c :d) ; => :A
(cycle :a :b :c :d) ; => :B
...

Name: Anonymous 2009-08-27 1:02

And a version with less repetition:

(let ((ht (make-hash-table :test #'equal)))
  (defun cycle (&rest args)
    (symbol-macrolet ((h (gethash args ht)))
      (unless h    (setf h args))
      (pop h))))

Name: Anonymous 2009-08-27 1:23

>>15
Still longer than >>1-chan's Ruby version. I'm masturbating to it as we speak!

Name: David Caruso 2009-08-27 1:24

>>14
If what >>12 said was right
UMH memesmith....

*puts on sunglasses*

...is always right.

YYYYEEEAAAAAH

Name: Anonymous 2009-08-27 1:31

>>16
It does take slightly longer to digest for someone not familiar with Ruby, but the same is probably true for my CL version if people don't know CL.

Name: Anonymous 2009-08-27 3:55

>>15

I just threw up my breakfast

Name: Anonymous 2009-08-27 4:12

>>19
Because you were so surprised by Haskell's elegance

Name: Anonymous 2009-08-27 4:21

I'm curious how would the Haskell version be written, especially since OP's problem requires having state implicitly (no passing around a state object).

Name: Anonymous 2009-08-27 8:19

>>21
cycle :: [a] -> State [([a], Int)] a

Name: Anonymous 2009-08-27 12:58

What >>22 is trying to say is that this is pointless to even try in Haskell. The type system forces you to either write a special monad specifically for the purpose of calling cycle[/cycle], or fuck around with the IO monad so at least you can intersperse it with IO calls.

Oh, and all the arguments have to be of the same type....Actually, it's even worse than that. Since you need a way to compare argument lists to each other, the type signature should say [code](Eq a) => stuff
or (Ord a) => stuff, which means that a can't be a function type, which means you can't even use a list like [print 4, print 2]. So basically this (toy) function is more useless than ever in Haskell.

There is a way around this, but it means using separate functions for setting and getting the argument list (and still using a special monad). If you have setArgs :: [a] -> State [a] () and getArg :: State [a] a, the implementation is trivial. The catch is that you can only access the last arglist you set with setArgs, and if you've got different types of arglists you want to use, you have to actually thread them through different instances of State [a].

Name: Anonymous 2009-08-27 13:18

>>23
Wow, HASKAL is even more useless than I thought. Even C is better about this.

Name: Anonymous 2009-08-27 13:21

``Even C'' is better about everything.  HASKAL is a theoretical toy.

Name: Anonymous 2009-08-27 13:58

>>23
After reading this, I can see very clearly (without knowing anything else about it) why nobody has ever written a real program in Haskell.

Name: Anonymous 2009-08-27 15:06

>>24-26
You're all mad. As if varargs abuse is a reasonable test of a language's practicality.

Especially, you, 25-chan. I know where you live :3

Name: Anonymous 2009-08-27 15:54

>>27
As a matter of principle I never use a language that doesn't properly support &optional, &key, and &rest.

Name: Anonymous 2009-08-27 16:13

>>28
I haven't seen a language other than CL with proper support of lambda-list-like features. I've seen some scripting languages try and hack around support for them, but not as good, and I'm not even saying anything about defmacro's arguments being passed as if by destructuring-bind. Yes, there are some insignificant (these days) performance costs to using complex key args, but it makes functions/code much more manageable/generic.

>>25
Yet it would take a lot more code than >>1's or >>14-15's to write the equivalent functionality, and passing variable length args would be awkward unless you passed argument count or just used a list or null-terminated array to represent them. You will have to write a fairly generic hash table implementation(or use one already available), and write a bunch of other boiler-plate code, unless you already have some architecture behind you on which you can lean.

Name: Anonymous 2009-08-27 16:25

from collections import defaultdict

class Cycle(object):
        def __init__(self):
                self.cycles = defaultdict(int)
 
        def cycle(self, *args):
                r = self.cycles[args]
                self.cycles[args] += 1
                return args[r % len(args)]

c = Cycle()
for i in xrange(6):
        print c.cycle("this","is","shit")

Name: Anonymous 2009-08-27 16:53

import Data.Map
import Data.IORef

newCycle =
   do state <- newIORef empty
      let memoCycle args =
             do modifyIORef state $ alter (Just . maybe (cycle args) tail) args
                fmap (head . (!args)) $ readIORef state
      return memoCycle

main =
   do foo <- newCycle
      putStrLn =<< foo ["hax","my","anus"]
      putStrLn =<< foo ["hax","my","anus"]
      putStrLn =<< foo ["achieve","satori"]
      putStrLn =<< foo ["hax","my","anus"]
      putStrLn =<< foo ["hax","my","anus"]

Name: Anonymous 2009-08-27 17:00

>>31
I just thought about writing something using IORefs just to show >>24-26 that it's possible. Good thing you were faster though, as I've never used IORefs before.

Name: Anonymous 2009-08-27 18:00

modifyIORef state $ alter (Just . maybe (cycle args) tail) args
fmap (head . (!args)) $ readIORef state
god, why

Name: Anonymous 2009-08-27 18:04

>>33
Because you're stupid.

Name: Anonymous 2009-08-27 18:05

>>34
What about my am stupid?

Name: Anonymous 2009-08-27 18:58

>>31
Couldn't you at least have given nice names like pop and peek to those gruesome HOFs?

Name: Anonymous 2009-08-27 19:04

>>31
Oh please. U MENA let {f' = foo ["hax", "my", "anus"]; f'' = ["achieve", "satori"]}; mapM_ (putStrLn =<<) [f',f',f'',f',f']

Name: Anonymous 2009-08-27 19:14

>>37
main = do
  foo <- newCycle
  let fooSharp = (putStrLn =<<) . foo . words
      [x,y] = fooSharp `map` ["hax my anus", "achieve satori"]
  sequence_ [x, x, y, x, x]

Name: Anonymous 2009-08-27 19:49

>>36
I don't know, couldn't you at least try not being a faggot?

>>37,38
main =
   do foo <- newCycle
      let (x, y) = ("hax my anus","achieve satori")
      mapM_ ((putStrLn =<<) . foo . words) [x, x, y, x, x]

Name: Anonymous 2009-08-27 21:59

>>31-39
I don't ever want to see this toy fibonacci language on my /prog/ ever again. Stop posting.

And if you want to program, use a real language like C or Perl.

Jesus.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List