d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Make Me A Better Programmer - From Step 1
Prev1394041424356Next
Add Reply New Topic New Poll
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 28 2014 10:42pm
Quote (AbDuCt @ Sep 28 2014 11:37pm)
Code
gets.each_char {|char| puts char}


Mmm doing someones entire assignment in one logical line.


But that is two statements o.o

1. gets_char method call
2. block statement functor


:D

This post was edited by Minkomonster on Sep 28 2014 10:43pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 28 2014 10:44pm
Quote (Minkomonster @ Sep 29 2014 12:42am)
But that is two statements o.o

1. gets_char method call
2. block statement functor


:D


Yes, but I said one logical line. Not one statement :3

Logical being you're not squishing 10 lines of C into a single line.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 28 2014 10:54pm
Quote (AbDuCt @ Sep 28 2014 11:44pm)
Yes, but I said one logical line. Not one statement :3

Logical being you're not squishing 10 lines of C into a single line.


10? 3. 1 more than ruby

Code
char c; while(scanf("%c",&c)) printf("%c\n",c);
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 28 2014 10:58pm
Quote (Minkomonster @ Sep 29 2014 12:54am)
10? 3. 1 more than ruby

Code
char c; while(scanf("%c",&c)) printf("%c\n",c);


Well I guess that is correct, but the assignment called for a user to enter a phrase and then parse it character by character.

I guess

Code
char c[1024]; fgets(c, 1023, STDIN); while(*c) { printf("%c\n", c); }


or the like might work.

This post was edited by AbDuCt on Sep 28 2014 10:59pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 28 2014 11:10pm
Quote (AbDuCt @ Sep 28 2014 11:58pm)
Well I guess that is correct, but the assignment called for a user to enter a phrase and then parse it character by character.

I guess

Code
char c[1024]; fgets(c, 1023, STDIN); while(*c) { printf("%c\n", c); }


or the like might work.


Well in all fairness, this exercise is pointless, as we could just as easily declare a function that wraps all that up. In which case, it would be reduced to a single "logical line" by invoking that function. That's what ruby is doing behind the scenes anyways. It isn't like ruby is magic. the implementation for each_char isn't a single line.


In fact, it is a hell of a lot more than a single line:

http://rxr.whitequark.org/mri/source/string.c#6474
Code

static VALUE
6474 rb_str_enumerate_chars(VALUE str, int wantarray)
6475 {
6476 VALUE orig = str;
6477 VALUE substr;
6478 long i, len, n;
6479 const char *ptr;
6480 rb_encoding *enc;
6481 VALUE UNINITIALIZED_VAR(ary);
6482
6483 if (rb_block_given_p()) {
6484 if (wantarray) {
6485 #if STRING_ENUMERATORS_WANTARRAY
6486 rb_warn("given block not used");
6487 ary = rb_ary_new();
6488 #else
6489 rb_warning("passing a block to String#chars is deprecated");
6490 wantarray = 0;
6491 #endif
6492 }
6493 }
6494 else {
6495 if (wantarray)
6496 ary = rb_ary_new();
6497 else
6498 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
6499 }
6500
6501 str = rb_str_new4(str);
6502 ptr = RSTRING_PTR(str);
6503 len = RSTRING_LEN(str);
6504 enc = rb_enc_get(str);
6505 switch (ENC_CODERANGE(str)) {
6506 case ENC_CODERANGE_VALID:
6507 case ENC_CODERANGE_7BIT:
6508 for (i = 0; i < len; i += n) {
6509 n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
6510 substr = rb_str_subseq(str, i, n);
6511 if (wantarray)
6512 rb_ary_push(ary, substr);
6513 else
6514 rb_yield(substr);
6515 }
6516 break;
6517 default:
6518 for (i = 0; i < len; i += n) {
6519 n = rb_enc_mbclen(ptr + i, ptr + len, enc);
6520 substr = rb_str_subseq(str, i, n);
6521 if (wantarray)
6522 rb_ary_push(ary, substr);
6523 else
6524 rb_yield(substr);
6525 }
6526 }
6527 RB_GC_GUARD(str);
6528 if (wantarray)
6529 return ary;
6530 else
6531 return orig;
6532 }


This post was edited by Minkomonster on Sep 28 2014 11:13pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 28 2014 11:13pm
Quote (Minkomonster @ Sep 29 2014 01:10am)
Well in all fairness, this exercise is pointless, as we could just as easily declare a function that wraps all that up. In which case, it would be reduced to a single "logical line" by invoking that function. That's what ruby is doing behind the scenes anyways. It isn't like ruby is magic. the implementation for each_char isn't a single line.


RUBY IS MAGIC RUBY IS LIFE

HOW DARE YOU

lol.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 28 2014 11:14pm
Quote (AbDuCt @ Sep 29 2014 12:13am)
RUBY IS MAGIC RUBY IS LIFE

HOW DARE YOU

lol.


See edit above.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 28 2014 11:16pm
Quote (Minkomonster @ Sep 29 2014 01:14am)
See edit above.


You just crushed my hopes and dreams. I'm bawling my eyes out like when my parents told me santa wasn't real.

But yea, you're correct lol.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 28 2014 11:23pm
Quote (AbDuCt @ Sep 29 2014 12:16am)
You just crushed my hopes and dreams. I'm bawling my eyes out like when my parents told me santa wasn't real.

But yea, you're correct lol.


You're parents aren't real either.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 28 2014 11:26pm
Quote (Minkomonster @ Sep 29 2014 01:23am)
You're parents aren't real either.


that escalated slowly
Go Back To Programming & Development Topic List
Prev1394041424356Next
Add Reply New Topic New Poll