Class: Crisp::Functions::Array

Inherits:
Object
  • Object
show all
Defined in:
lib/crisp/functions/array.rb

Overview

Defining array crisp functions

Class Method Summary collapse

Class Method Details

.load(current_env) ⇒ Object

load the functions and bind them into the given environment



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/crisp/functions/array.rb', line 6

def self.load(current_env)

  # head
  # return head of array
  #
  #  (head [1 2 3])
  #  => 1
  Function.new do
    validate_args_count(1, args.size)

    if args[0].class.name != "Crisp::Nodes::ArrayLiteral"
      raise ArgumentError, "argument is not an array"
    end

    if raw_head = args[0].raw_elements[0]
      raw_head.resolve(env)
    else
      nil
    end
  end.bind('head', current_env)

  # tail
  # return tail of array
  #
  #  (tail [1 2 3])
  #  => [2 3]
  Function.new do
    validate_args_count(1, args.size)

    if args[0].class.name != "Crisp::Nodes::ArrayLiteral"
      raise ArgumentError, "argument is not an array"
    end

    if raw_tail = args[0].raw_elements[1..-1]
      raw_tail.map { |arg| arg.resolve(env) }
    else
      []
    end
  end.bind('tail', current_env)

end