Skip to content

Instantly share code, notes, and snippets.

@funatsufumiya
Created July 15, 2014 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funatsufumiya/7fe0d71a6e5664a988f7 to your computer and use it in GitHub Desktop.
Save funatsufumiya/7fe0d71a6e5664a988f7 to your computer and use it in GitHub Desktop.
Rubyで配列言語(Array Language)を実現する ref: http://qiita.com/atmarksharp/items/bd881b91d1074cc36f0c
-->sqrt(2)
ans = 1.4142136
-->sqrt([1,2,3,4,5])
ans = 1. 1.4142136 1.7320508 2. 2.236068
class Shape
def initialize(x,y)
@x = x
@y = y
end
def to_s
"Shape(#{@x},#{@y})"
end
end
puts "\n=== [1] Single Shape ==="
shape = Shape.new(7,7)
p shape
puts "\n=== [2] Multiple Shapes ==="
points = [[1,1],[1,2],[3,4]]
shapes = points.map do |p|
Shape.new(p[0],p[1])
end
p points
p shapes
puts "\n=== [3] Calculate Single Value ==="
num = 3
p num
p Math.sqrt(num)
puts "\n=== [4] Calculate Multiple Values ==="
nums = [1,2,3,4,5]
p nums
result = nums.map do |i|
Math.sqrt(i)
end
p result
=== [1] Single Shape ===
#<Shape:0x007fd38901de98 @x=7, @y=7>
=== [2] Multiple Shapes ===
[[1, 1], [1, 2], [3, 4]]
[#<Shape:0x007fd38901dc18 @x=1, @y=1>, #<Shape:0x007fd38901dbf0 @x=1, @y=2>, #<Shape:0x007fd38901dbc8 @x=3, @y=4>]
=== [3] Calculate Single Value ===
3
1.7320508075688772
=== [4] Calculate Multiple Values ===
[1, 2, 3, 4, 5]
[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979]
class Arg
def initialize(args)
if args.is_a? Array
@args = args
else
@args = [args]
end
end
def get()
@args
end
end
class Iterator
def self.instance(clazz,args)
if args.is_a? Arg
result = args.get.map do |arg|
clazz.new(arg)
end
Arg.new(result)
else
raise "Args should be instance of Arg"
end
end
end
class Shape
def self.instance(args)
Iterator::instance(Shape,args)
end
def initialize(args)
@x = args[0]
@y = args[1]
end
def to_s
"Shape(#{@x},#{@y})"
end
end
def sqrt(arg)
result = arg.get.map do |i|
Math.sqrt(i)
end
Arg.new(result)
end
puts "\n=== [1] Single Shape ==="
shape = Shape::instance(Arg.new([7,7]))
p shape
puts "\n=== [2] Multiple Shapes ==="
args = Arg.new([[1,1],[1,2],[3,4]])
shapes = Shape::instance(args)
p args
p shapes
puts "\n=== [3] Calculate Single Value ==="
num = Arg.new(3)
p num
p sqrt(num)
puts "\n=== [4] Calculate Multiple Values ==="
nums = Arg.new([1,2,3,4,5])
p nums
p sqrt(nums)
sqrt(Arg.new(3))
sqrt(Arg.new(1,2,3,4,5))
=== [1] Single Shape ===
#<Arg:0x007f8131202c90 @args=[#<Shape:0x007f8131202d58 @x=1, @y=1>, #<Shape:0x007f8131202cb8 @x=1, @y=1>]>
=== [2] Multiple Shapes ===
#<Arg:0x007f8131202858 @args=[[1, 1], [1, 2], [3, 4]]>
#<Arg:0x007f8131202768 @args=[#<Shape:0x007f8131202808 @x=1, @y=1>, #<Shape:0x007f81312027b8 @x=1, @y=2>, #<Shape:0x007f8131202790 @x=3, @y=4>]>
=== [3] Calculate Single Value ===
#<Arg:0x007f8131201f98 @args=[3]>
#<Arg:0x007f8131201e30 @args=[1.7320508075688772]>
=== [4] Calculate Multiple Values ===
#<Arg:0x007f8131201c78 @args=[1, 2, 3, 4, 5]>
#<Arg:0x007f8131201a70 @args=[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment