Skip to content

Commit 6ac8893

Browse files
committed
More stdlib
1 parent e2a8ce3 commit 6ac8893

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/lit/stdlib/lit_array.cr

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ module Lit
9696
end
9797
)
9898
})
99+
when "any?"
100+
::Lit::Native::Fn.new(name.lexeme, 1, ->(interpreter : Interpreter, arguments : ::Array(Value), token : Token) : Value {
101+
fn = arguments[0]
102+
if !fn.is_a?(Function)
103+
raise RuntimeError.new(token, "Expected function as the first argument, got #{interpreter.type_of(fn)}.")
104+
end
105+
@elements.any? do |element|
106+
fn.call(interpreter, [element], token).as(Bool)
107+
end
108+
})
109+
when "all?"
110+
::Lit::Native::Fn.new(name.lexeme, 1, ->(interpreter : Interpreter, arguments : ::Array(Value), token : Token) : Value {
111+
fn = arguments[0]
112+
if !fn.is_a?(Function)
113+
raise RuntimeError.new(token, "Expected function as the first argument, got #{interpreter.type_of(fn)}.")
114+
end
115+
@elements.all? do |element|
116+
fn.call(interpreter, [element], token).as(Bool)
117+
end
118+
})
99119
when "sample"
100120
::Lit::Native::Fn.new(name.lexeme, 0, ->(_interpreter : Interpreter, _arguments : ::Array(Value), _token : Token) : Value {
101121
@elements.sample

src/lit/stdlib/native.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ module Lit
136136
::Lit::Native::Fn.new("argv", 0, ->(interpreter : Interpreter, _arguments : ::Array(Value), _token : Token) : Value {
137137
interpreter.argv
138138
}),
139+
::Lit::Native::Fn.new("env", 0, ->(interpreter : Interpreter, _arguments : ::Array(Value), _token : Token) : Value {
140+
LitMap.new(
141+
ENV.to_h { |key, value| {key.as(Value), value.as(Value)} }
142+
)
143+
}),
139144
]
140145
end
141146
end

0 commit comments

Comments
 (0)