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.

Name: Anonymous 2009-08-27 22:08

>>program,
>>real language
>>Perl.

Anon you are so funny tonight

Name: Anonymous 2009-08-27 22:09

>>40
use a real language
perl
ಠ_ಠ

Name: Anonymous 2009-08-27 22:15

>>40
I'll post whatever the fuck I want, you whiny faggot. You fear the monads because you lack the gonads. Bitch.

Name: Anonymous 2009-08-27 23:11

>>41-42
Perl is immensely more useful than HASKAL, you festering cumshots. Even accounting for how amazingly useless it is compared to C.

Name: Anonymous 2009-08-27 23:12

>>40
It's not used only for fibs. It's also used for scalable web applications.

Name: Anonymous 2009-08-27 23:17

>>45
You're thinking of Erlang

Name: Anonymous 2009-08-27 23:18

>>46
You're thinking of Java servlets

Name: Anonymous 2009-08-27 23:20

>>47
You're thinking of ASP.NET

Name: Anonymous 2009-08-27 23:22

>>48
You're thinking of My United States of whatever

Name: Anonymous 2009-08-27 23:50

>>49
You're thinking of the game

Name: Anonymous 2009-08-28 0:33

>>50
back to /b/

Name: Anonymous 2009-08-28 0:39

>>51
You just stopped thinking.

Name: Anonymous 2009-08-28 0:40

You are now thinking manually

Name: Anonymous 2009-08-28 1:30

I fucked you're touhou

Name: Anonymous 2009-08-28 3:58

>>53
proove it numbnuts

Name: Anonymous 2009-08-28 4:31

>>55
Cogito ergo your gay

Name: Anonymous 2009-08-28 4:36

>>56
Back to middle school, please

Name: Anonymous 2009-08-28 4:44

>>57
i lold at >>56

Name: Anonymous 2009-08-28 5:08

>>58
see
>>57

Name: Anonymous 2009-08-30 1:51

>>41
>>42
You script kiddies, I bet you even think "Perl is dead"

Name: SAGE SAGE SAGE SAGE SAGE 2009-08-31 2:10

Bumping because this thread contains actual programming

Name: Anonymous 2009-08-31 9:33

Saging because I'd go work if I wanted actual programming.

Name: Anonymous 2011-02-04 18:46

Name: 2012-01-25 7:21

Name: Anonymous 2012-01-25 7:45

>>44
Haskell is the best choice for functional programming.
Perl is the best choice for nothing because ruby is better in every way at the exact same thing.

Name: Anonymous 2012-01-25 10:55

>>62

>implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying >implying

Name: 67 2012-01-25 10:56

i normally always sage, but i must bump programming related threads in the presence of such spam.

Name: Anonymous 2012-01-25 11:01

>>1
Hi I don't even know what this is trying to do

Name: Anonymous 2012-01-25 14:04

>>69

you are stupid

Name: Anonymous 2012-01-25 16:57

>>69
Try reading the thread with your eyes open.

Name: 2012-01-25 22:54

Name: Anonymous 2012-01-26 13:01

>>70
>>71
nope still not got it . Something to do with hashes. Why's it called cycle?

Name: Anonymous 2012-01-26 13:06

Someone's a mental midget.

Name: Anonymous 2012-01-26 14:30

>>1
Or you could use Array.cycle ?

Name: Anonymous 2012-01-26 14:32

>>75
I MENA Array.rotate / rotate!

Name: Anonymous 2012-01-26 14:32

Also cycle my dubs!

Name: Anonymous 2012-01-26 14:33

Check 'em

Name: Anonymous 2012-01-26 14:35

rotate my cycle until closure

Name: Anonymous 2012-01-26 18:22


#!/usr/bin/env ruby1.9
N=ARGV[0].to_i
B=ARGV[1].to_i
Array.new(N<B ? N : B) {|i|N.to_s(i+2)}.each_with_index{|e,i|e.match(/(\d)\1$/)&&printf("[%02d] %s\n",i+2,e) }


Ah the pleasure of being cummed inside

Name: Anonymous 2012-01-26 18:23

<<< CHECK MY DUBS IN THE FOLLOWING BASES
[03] 10000
[05] 311
[07] 144
[09] 100
[26] 33

Name: Anonymous 2012-01-26 18:29

>>80,81
Why haven't I written this myself yet?

Name: Anonymous 2012-01-26 18:31

>>82
BECAUSE YOU HAVEN'T REACHED SATORI!!!

[02] 1010011

Name: Anonymous 2012-01-26 18:33

>>83
I was going to say because this person is a mental midget.

Name: >>82 2012-01-26 18:34

>>84
Stop throwing that word around.
It's mena ;_;

Name: Anonymous 2012-01-26 18:35

>>84
Nice dubs bro!

[02] 1010100
[11] 77
[13] 66
[20] 44
[27] 33

Name: Anonymous 2012-01-26 21:00

sdgfsdgfsgdf

Name: Anonymous 2012-01-27 0:41

is this what op is doing? or am I wrong?


function cycle(...)
  local index = 1
  local array = {...}
  local length = table.getn(array)
  return function()
    local value = array[index]
    index = index + 1
    if index > length then index = 1 end -- indecies starting at one kind of blows for this.
    return value
  end
end

for v in cycle('the', 'other', 'day', 'I', 'was', 'saying') do
  print(v)
end

Name: Anonymous 2012-01-27 0:46

>>88
>table.getn

Name: Anonymous 2012-01-27 0:46

And now with no mutations!


function cycle(...)
  local array = {...}
  local length = table.getn(array)
  local loop
  loop = function(index)
    coroutine.yield(array[index])
    if index < length then
      return loop(index + 1)
    else
      return loop(1)
    end
  end
  return coroutine.wrap(function()
    loop(1)
  end)
end

for v in cycle('yaba', 'daba', 'doo') do
  print(v)
end

Name: Anonymous 2012-01-27 0:47

>>89

I always forget to use #.

Name: Anonymous 2012-01-27 3:03

- Ruby indulges obfuscation: Ruby has no keyword/optional arguments, so you'll have to use hash parameters as a substitute. This is an idiom that comes from Perl. Ugly, Perl-looking code, like proc {|obj, *args| obj.send(self, *args)} or (0..127).each { |n| p n.chr }, considered beautiful. Another confusing Perl borrowing are postfix `if` and `while` (line = file.readline while line != "needle" if valid line) and quirky variable names (partially due to naive environment design): @instance_var, @@class_var, CONSTANT_VAR, $global_var, :sym, &proc, $~[1], $!, $>, $@, $&, $+, $0, $~, $’, $`, $:, $., $* and $?. If A is [1,2,3] and B is [10,20,30], then A+B is [1,2,3,10,20,30], when you probably wanted [11,22,33]. A good amount of your code will consist of begin end begin begin end end...
- Faulty syntax. Ruby cant distinguishing a method call from an operator: "a +b" can be both "a(+b)" and "a + b" - remove the space to the left of "+" or add a space to the right of "+", and it will be parsed as an addition. Same with "puts s *10", which is parsed as puts(s(*10)). Ruby's expressions terminate by a newline and you have to implicitly state that the expression is not over, using trailing + or \. That makes it easy to make a dumb syntactic mistake by forgeting to continue line. It also encourages putting everything onto a single line, producing messy looking code.
- Slow: JIT-compiling implementations exist, but they're still slow and incomplete, due to Ruby's complexity and bad design, which make Ruby difficult to optimize compared to other dynamic languages, like Lisp. For example, Ruby has to accomodate for somebody in another thread changing the definition of a class spontaneously, forcing compiler to be very conservative. Compiler hints, like `int X` from C/C++ or `declare (int X)` from Lisp, arent possible either.
- Ruby's GC is a naive mark-and-sweep implementation, which stores the mark bit directly inside objects, a GC cycle will thus result in all objects being written to, making their memory pages `dirty` and Ruby's speed proportional to the number of allocated objects. Ruby simply was not designed to support hundred thousand objects allocation per second. Unfortunately, that’s exactly what frameworks like Ruby on Rails do. The more objects you allocate, the more time you "lose" at code execution. For instance something as simple as 100.times{ ‘foo’ } allocates 100 string objects, because strings are mutable and therefore each version requires its own copy. A simple Ruby on Rails 'hello world' already uses around 332000 objects.
- OOP: Matz had a bit too much of the "OOP is the light and the way" philosophy in him, in effect Ruby doesn't have stand-alone functions and Ruby's blocks can't be used in exactly the same way as usual closures. Even high-order functions are attached to objects and produce verbose code: "names.map { |name| name.upcase }", instead of simple "map upcase names".
- Ruby (like most other scripting languages) does not require variables to be declared, as (let (x 123) ...) in Lisp or int x = 123 in C/C++. If you want a variable private to a block, you need to pick an unique variable name, holding the entire symbol table in your head. Ruby  introduces new variables by just parsing their assignements, meaning "a = 1 if false; a" wont raise an error. All that means Ruby can't detect even a trivial typo - it will produce a program, which will continue working for hours until it reaches the typo. Local and global scopes are unintuitive. Certain operations (like regular expression operator) create implicit local variables for even more confusion.
- Non-othogonal: {|bar| bar.foo}, proc {|bar| bar.foo}, lambda {|bar| bar.foo}, def baz(bar) bar.foo end - all copy the same functionality, where Lisp gets along with only `lambda`. Some Ruby's features duplicate each other: print "Hello", puts "Hello",  $stdout<<"Hello", printf "Hello", p "Hello", write "Hello" and putc "Hello" -- all output text to stdout; there is also sprintf, which duplicates functionality of printf and string splicing. begin/do/then/end, {} and `:` also play role in bloating syntax, however, in some cases, precedence issues cause do/end and {} to act differently ({} binds more tightly than a do/end). More bloat comes from || and `or`, which serve the same purpose.
- Ruby as a language supports continuations via callcc keyword. Ruby's callcc is incredibly slow, implemented via stack copying. JRuby and IronRuby don't have continuations at all, and it's quite unlikely they will ever get them. There were also support breaches in mainline Ruby, where Ruby 1.9 has not supported continuations for a while. If you want your code to be portable, I'd suggest not using Ruby.
- Ruby was created "because there was no good scripting language that could handle Japanese text". Today it's mostly Rails hype and no outstanding feature, that makes the language, like the brevity of APL or simplicity and macros of Lisp. "There is some truth in the claim that Ruby doesn’t really give us anything that wasn’t there long ago in Lisp and Smalltalk, but they weren’t bad languages." -- Matthew Huntbach

Name: Anonymous 2012-01-27 19:25

ruby moe desu kawaii

Name: Anonymous 2012-01-27 21:37

>>90
if return
else return

hiss

Name: Anonymous 2012-01-27 22:40

>>94

indeed. it kind of sucks


function a(n) return if true then 1 else 2 end end
stdin:1: unexpected symbol near 'if'


oh no, wait:


function a(n)
>> local results = {[true] = 1, [false] = 54}
>> return results[n < 5]
>> end
print(a(3))
1
print(a(4))
1
print(a(5))
54
print(a(8))
54

Name: Anonymous 2012-01-28 1:28

ruby is fine art

Proc.send(:define_method, :-@) {self}
class Bow;def >>(v);@arrow = v;end;def shoot(arg);@arrow.call(arg);end;end;bow=Bow.new
class Person;def initialize;@profession=:adventurer;end;def wound;@profession=:guard;end;end;adventurer=Person.new

bow >>--->(target) { target.wound }
bow.shoot(adventurer)

Name: Anonymous 2012-01-28 1:32

>>95
if index < length then
    return loop(index + 1)
end
return loop(1)

Name: Anonymous 2012-01-28 1:46

>>97

oh. hissssss

Name: Anonymous 2012-01-28 6:29

These dubs are as slow as fuck

Name: Anonymous 2012-01-28 8:25

>>100
Can anyone answer this question to me?

Also, nice dubs bro.

Name: Anonymous 2012-01-28 9:09

>>100
Yes.

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