From 09fb509eb44a93a0e18f41a0c676950f35230774 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 29 May 2015 00:32:16 +0900 Subject: [PATCH] crystal: refactor by adding Array to Mal type conversion --- crystal/core.cr | 16 +++++++--------- crystal/types.cr | 7 +++++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/crystal/core.cr b/crystal/core.cr index 67ef9f71..15ec78d6 100644 --- a/crystal/core.cr +++ b/crystal/core.cr @@ -14,7 +14,7 @@ macro calc_op(op) end def self.list(args) - args.each_with_object(Mal::List.new){|a,l| l << a} + args.to_mal end def self.list?(args) @@ -73,11 +73,9 @@ def self.slurp(args) end def self.cons(args) - arg1 = args[1].unwrap - eval_error "2nd arg of cons must be list" unless arg1.is_a? Array - arg1.each_with_object(Mal::List.new << args[0]) do |elem, list| - list << elem - end + head, tail = args[0] as Mal::Type, args[1].unwrap + eval_error "2nd arg of cons must be list" unless tail.is_a? Array + ([head] + tail).to_mal end def self.concat(args) @@ -109,7 +107,7 @@ def self.rest(args) return Mal::List.new if a0.nil? eval_error "1st argument of first must be list or vector or nil" unless a0.is_a? Array return Mal::List.new if a0.empty? - a0[1..-1].each_with_object(Mal::List.new){|e,l| l << e} + a0[1..-1].to_mal end def self.apply(args) @@ -183,7 +181,7 @@ def self.keyword?(args) end def self.vector(args) - args.each_with_object(Mal::Vector.new){|e,v| v << e} + args.to_mal(Mal::Vector) end def self.vector?(args) @@ -263,7 +261,7 @@ end def self.vals(args) head = args.first.unwrap eval_error "1st argument of assoc must be hashmap" unless head.is_a? Mal::HashMap - head.values.each_with_object(Mal::List.new){|e,l| l << e} + head.values.to_mal end def self.sequential?(args) diff --git a/crystal/types.cr b/crystal/types.cr index 6f68ed0e..56203307 100644 --- a/crystal/types.cr +++ b/crystal/types.cr @@ -83,3 +83,10 @@ end macro gen_type(t, *args) Mal::Type.new {{t.id}}.new({{*args}}) end + +class Array + def to_mal(t = Mal::List) + each_with_object(t.new){|e, l| l << e} + end +end +