Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lets enjoy code reading

makicamel
January 09, 2020

Lets enjoy code reading

実例で見るコードリーディングのススメ
2020.01.09. Omotesando.rb #54 〜 コードの読み方 〜

makicamel

January 09, 2020
Tweet

More Decks by makicamel

Other Decks in Programming

Transcript

  1. ͔֬ΊͯΈ͍ͨ ɹaⁿ ͬͯ͜Μͳײ͡ʁ
 ɹ(a΋n΋0Ҏ্ͷ੔਺ͱ͢Δ) def power(a, n) result = 1

    n.times { |i| result *= a } result end power(2, 33) # => 8589934592
  2. ͲΜͳ࣮૷ʁ static VALUE int_pow(long x, unsigned long y) { //

    ... y &= ~1; do { while (y % 2 == 0) { if (!FIT_SQRT_LONG(x)) { VALUE v; bignum: v = rb_big_pow(rb_int2big(x), LONG2NUM(y)); if (RB_FLOAT_TYPE_P(v)) /* infinity due to overflow */ return v; if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v); return v; } x = x * x; y >>= 1; } { if (MUL_OVERFLOW_FIXNUM_P(x, z)) { goto bignum; } z = x * z; } } while (--y); if (neg) z = -z; return LONG2NUM(z); } Կ΋Θ͔Βͳ͍ OVNFSJDDSVCZSVCZc(JU)VC
 IUUQTHJUIVCDPNSVCZSVCZCMPCEBCDFFGCBBGDOVNFSJDD
  3. ͲΜͳ࣮૷ʁ DPNQMFYSCSVCJOJVTSVCJOJVTc(JU)VC
 IUUQTHJUIVCDPNSVCJOJVTSVCJOJVTCMPCFCEDBCCGEBBCBGFE DPSFDPNQMFYSC def ** (other) # ... if

    other.kind_of?(Complex) # ... elsif other.kind_of?(Integer) if other > 0 x = self z = x n = other - 1 while n != 0 while (div, mod = n.divmod(2) mod == 0) x = Complex(x.real*x.real - x.imag*x.imag, 2*x.real*x.imag) n = div end z *= x n -= 1 end z else # ... end elsif Complex.generic?(other) # ... end end ͳΜͱ͔ಡΊͦ͏
  4. def ** (other) if !other.kind_of?(Float) && other == 0 return

    Complex(1) end if other.kind_of?(Complex) # ... elsif other.kind_of?(Integer) if other > 0 x = self z = x n = other - 1 while n != 0 while (div, mod = n.divmod(2) mod == 0) x = Complex(x.real*x.real - x.imag*x.imag, 2*x.real*x.imag) n = div end z *= x n -= 1 end z else # ... end elsif Complex.generic?(other) # ... end end other͕0ͷ࣌1Λฦ͢ ࠓճ͸ॲཧͷྲྀΕΛ
 ஌Γ͍͚ͨͩͳͷͰলུ (self΋other΋ਖ਼ͷ੔਺ͷ૝ఆ)
  5. def ** (other) if !other.kind_of?(Float) && other == 0 return

    Complex(1) end if other.kind_of?(Complex) # ... elsif other.kind_of?(Integer) if other > 0 x = self z = x n = other - 1 while n != 0 while (div, mod = n.divmod(2) mod == 0) x = Complex(x.real*x.real - x.imag*x.imag, 2*x.real*x.imag) n = div end z *= x n -= 1 end z else # ... end elsif Complex.generic?(other) # ... end end ࠓճ͸ॲཧͷྲྀΕΛ
 ஌Γ͍͚ͨͩͳͷͰলུ (਺ֶͰ͖ͳ͍ਓ)
  6. def power(a, other) x = a z = x n

    = other - 1 while n != 0 while (div, mod = n.divmod(2); mod == 0) x = x * x n = div end z *= x n -= 1 end z end ͘͢͝ಡΊͦ͏ʂ ɹɹɹ Կ΋Θ͔Βͳ͍ ؆ུԽ͢Δͱ ࣮ߦ͠΍͍͢Α͏ʹselfΛaʹ͢Δ
  7. def power(a, other) x = a z = x n

    = other - 1 while n != 0 p " n: #{n}" while (div, mod = n.divmod(2); mod == 0) p x = x * x p n = div end z *= x p " z: #{z}" n -= 1 end z end ࢦ਺͕2ͷഒ਺ͷؒ ৽͍͠ࢦ਺͸n / 2 ؆ུԽ͢Δͱ x = x2Λ͚ͭͮ͠Δ
  8. def power(a, other) x = a z = x n

    = other - 1 while n != 0 p " n: #{n}" while (div, mod = n.divmod(2); mod == 0) p x = x * x p n = div end z *= x p " z: #{z}" n -= 1 end z end ྦྷ৐ ࢦ਺͕ۮ਺ͷؒ ࢦ਺͕ح਺ʹͳͬͨΒ
 zʹྦྷ৐ͨ͠xΛֻ͚Δ (((x2)2)2…͠ଓ͚Δ
  9. def power(a, other) x = a z = x n

    = other - 1 while n != 0 p " n: #{n}" while (div, mod = n.divmod(2); mod == 0) p x = x * x p n = div end z *= x p " z: #{z}" n -= 1 end z end ྦྷ৐ a = 3, other = 3ͷ৔߹( 33 ) n(ࢦ਺) = 2 div = 1, mod = 0 n = 1 z = 3 * 9 = 27
 n = 0 x = 3 * 3 = 9
  10. ͭ·Γ •33 = 3 * 32 •34 = 3 *

    33
 = 3 * 3 * 32 35 = 3 * 34
 = 3 * (32)2
 ఈΛऔΓग़͢ ࢦ਺Λ1 + nʹ෼͚Δ
  11. ͭ·Γ •33 = 3 * 32 •35 = 3 *

    34
 = 3 * (32)2 35 = 3 * 34
 = 3 * (32)2
 ෼͚ΒΕͨࢦ਺͕2nͰׂΓ͖ΕΔؒ
 ࢦ਺Λ2ͰׂΓଓ͚͍ͯ͘
  12. ͭ·Γ •33 = 3 * 32 •35 = 3 *

    34
 = 3 * (32)2 35 = 3 * 34
 = 3 * (32)2
 ෼͚ΒΕͨࢦ਺͕2nͰׂΓ͖ΕΔؒ
 ࢦ਺Λ2ͰׂΓଓ͚͍ͯ͘
  13. ͭ·Γ •33 = 3 * 32 •35 = 3 *

    34
 = 3 * (32)2 •37 = 3 * 36
 = 3 * (32)2 * 3
 ෼͚ΒΕͨࢦ਺͕2nͰׂΓ੾Εͳ͘ͳΔͱ
 ఈΛ෼͚Δ
  14. Θͨ͠ͷ࣮૷ • def power(a, n) result = 1 n.times {

    |i| result *= a } result end power(2, 33) # => 8589934592 •n.times {} ͩͱ୯७ʹnճܭࢉ͢Δ
 233 = 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 *
 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 *
 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 *
 2 * 2 * 2
  15. rubyͷ࣮૷ • def power(a, other) x = a z =

    x n = other - 1 while n != 0 while (div, mod = n.divmod(2); mod == 0) x *= x n = div end z *= x n -= 1 end z end •ࢦ਺Λ2Ͱׂ͍ͬͯ͘ͱܭࢉճ਺͕૿͑ͳ͍
  16. •ࢦ਺Λ2Ͱׂ͍ͬͯ͘ͱܭࢉճ਺͕૿͑ͳ͍
 233 = 2 * 232
 = 2 * ((((22)2)2)2)2

    rubyͷ࣮૷ Θͨ͠ͷ࣮૷ •233 = 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 *
 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 *
 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 *
 2 * 2 * 2 ͍͢͝ݮͬͨʂ
  17. •bundle install —path vendor/bundle && rm .bundle/config •gem-src •source_location(rubyͷϝιουఆٛΛݺ΂Δ) •pry

    + pry-doc(`$`Ͱcͷϝιουఆٛ΋ݺ΂Δ) •RubyMine •OctTree ͍ͭͰ΋ಡΊΔ؀ڥΛ࡞Δ