Skip to content

Commit 8682fc8

Browse files
add features for marine's program
1 parent 5226e89 commit 8682fc8

4 files changed

Lines changed: 65 additions & 4 deletions

File tree

lib/code/object/list.rb

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ def call(**args)
227227
sig(args)
228228
code_sum
229229
when "uniq"
230-
sig(args)
231-
code_uniq
230+
sig(args) { (Function | Class).maybe }
231+
code_uniq(code_value, **globals)
232232
when "many?"
233233
sig(args)
234234
code_many?
@@ -1001,8 +1001,39 @@ def code_size
10011001
Integer.new(raw.size)
10021002
end
10031003

1004-
def code_uniq
1005-
List.new(raw.uniq)
1004+
def code_uniq(argument = nil, **globals)
1005+
code_argument = argument.to_code
1006+
1007+
unless code_argument.is_a?(Function) || code_argument.is_a?(Class)
1008+
return List.new(raw.uniq)
1009+
end
1010+
1011+
if code_argument.is_a?(Class)
1012+
return List.new(
1013+
raw
1014+
.select { |code_element| code_element.is_a?(code_argument.raw) }
1015+
.uniq
1016+
)
1017+
end
1018+
1019+
index = 0
1020+
1021+
List.new(
1022+
raw.uniq do |code_element|
1023+
if code_argument.is_a?(Function)
1024+
code_argument
1025+
.call(
1026+
arguments: List.new([code_element, Integer.new(index), self]),
1027+
**globals
1028+
)
1029+
.tap { index += 1 }
1030+
else
1031+
code_element.tap { index += 1 }
1032+
end
1033+
rescue Error::Next => e
1034+
e.code_value.tap { index += 1 }
1035+
end
1036+
)
10061037
end
10071038

10081039
def code_sum

lib/code/object/time.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ def initialize(*args, **_kargs, &_block)
7474
self.raw = args.first.to_time.in_time_zone(::Time.zone)
7575
elsif args.first.is_a?(::ActiveSupport::TimeWithZone)
7676
self.raw = args.first.dup
77+
elsif args.first.is_a?(Integer) || args.first.is_a?(Decimal) ||
78+
args.first.is_a?(::Integer) || args.first.is_a?(::Float) ||
79+
args.first.is_a?(::BigDecimal)
80+
code_value = args.first.to_code
81+
timestamp =
82+
if code_value.is_a?(Decimal)
83+
code_value.raw.to_r
84+
else
85+
code_value.raw
86+
end
87+
self.raw = ::Time.zone.at(timestamp)
7788
else
7889
self.raw = ::Time.zone.now
7990
end

spec/code/object/time_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe Code::Object::Time do
6+
[
7+
['Time.new(1).format("%s")', ":1"],
8+
['Time.new(1100042342).format("%s")', ":1100042342"],
9+
['Time.new(1.2).format("%s.%L")', '"1.200"'],
10+
['Time.new(11212.1212).format("%s.%L")', '"11212.121"']
11+
].each do |input, expected|
12+
it "#{input} == #{expected}" do
13+
expect(Code.evaluate(input)).to eq(Code.evaluate(expected))
14+
end
15+
end
16+
end

spec/code_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@
284284
["[1, 2, 3].map { |i| next(0) if i.even? i ** 2}", "[1, 0, 9]"],
285285
["[1, 2, 3].select { |n| n.even? }", "[2]"],
286286
["[1, 2, 3].select { |n| n.even? }.select { |n| n.odd? }", "[]"],
287+
["[1, 2, 3, 4].uniq { |n| n % 2 }", "[1, 2]"],
288+
['[1, "1", 2, "2"].uniq(String)', '["1", "2"]'],
289+
["[1, 2, 3, 4].uniq(String)", "[]"],
287290
["[1, 2].map(&:to_string)", "[:1, :2]"],
288291
["[1, 2].map(&:to_string)", '["1", "2"]'],
289292
["[1, 2].map(&:to_string)", '["1", "2"]'],

0 commit comments

Comments
 (0)