Module: Colby::Core

Defined in:
lib/colby/core.rb

Instance Method Summary collapse

Instance Method Details

#assoc(coll, *pairs) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/colby/core.rb', line 32

def assoc(coll, *pairs)
  @retval = coll
  pairs.each_slice(2) do |k,v|
    @retval = assoc_2(@retval, k , v)
  end
  @retval
end

#assoc_2(coll, key, val) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/colby/core.rb', line 40

def assoc_2(coll, key, val)
  if coll.is_a? Hamster::Hash
    return coll.send(:put, key) {val}
  end
  if coll.is_a? Hamster::Vector
    if has_key(coll, key)
      return coll.send(:set, key, val)
    else
      raise IndexOutOfBounds
    end
  end
end

#conj(coll, item) ⇒ Object



25
26
27
28
29
30
# File 'lib/colby/core.rb', line 25

def conj(coll, item)
  return coll.cons(item) if coll.is_a? Hamster::List
  return coll.add(item) if coll.is_a? Hamster::Vector
  return coll.merge(item) if coll.is_a? Hamster::Hash
  return Hamster.set(*coll, item) if coll.is_a? Hamster::Set
end

#count(coll) ⇒ Object



73
74
75
# File 'lib/colby/core.rb', line 73

def count(coll)
  return coll.length
end

#find(coll, key) ⇒ Object



62
63
64
65
# File 'lib/colby/core.rb', line 62

def find(coll, key)
  return Core.vec(key, Core.get(coll, key)) if coll.is_a? Hamster::Vector
  return Core.vec(key, Core.get(coll,key)) if coll.is_a? Hamster::Hash
end

#get(coll, key) ⇒ Object



53
54
55
# File 'lib/colby/core.rb', line 53

def get(coll, key)
  coll.get(key) unless coll.is_a? Hamster::List
end

#has_key(coll, key) ⇒ Object



67
68
69
70
71
# File 'lib/colby/core.rb', line 67

def has_key(coll, key)
  return coll.has_key?(key) if coll.is_a? Hamster::Hash
  return coll[key] != nil if (coll.is_a? Hamster::Vector) || (coll.is_a? Hamster::Set)
  nil
end

#hash_map(*pairs) ⇒ Object



8
9
10
11
# File 'lib/colby/core.rb', line 8

def hash_map(*pairs)
  return Hamster.hash(Hash[*pairs]) if pairs.is_a? Array
  return Hamster.hash(*pairs) if pairs.is_a? Hash
end

#list(*data) ⇒ Object



17
18
19
# File 'lib/colby/core.rb', line 17

def list(*data)
  Hamster.list(*data)
end

#nth(coll, key) ⇒ Object



57
58
59
60
# File 'lib/colby/core.rb', line 57

def nth(coll, key)
  return get(coll, key) if coll.is_a? Hamster::Vector
  return coll.to_a[key] if coll.is_a? Hamster::List
end

#peek(coll) ⇒ Object



77
78
79
80
# File 'lib/colby/core.rb', line 77

def peek(coll)
  return coll.head if coll.is_a? Hamster::List
  return coll.last if coll.is_a? Hamster::Vector
end

#pop(coll) ⇒ Object



82
83
84
85
86
87
# File 'lib/colby/core.rb', line 82

def pop(coll)
  return coll.tail if coll.is_a? Hamster::List
  if coll.is_a? Hamster::Vector
    return vec(*coll.to_a[0..-2])
  end
end

#seq(data) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/colby/core.rb', line 93

def seq(data)
  if data.is_a? Hamster::List
    return data
  elsif data.is_a? Array or data.is_a? Hamster::Vector
    return list(*data)
  elsif data.is_a? Hash
    return list(*data.keys.to_a.zip(data.values.to_a))
  elsif data.is_a? Hamster::Hash
    l = list
    data.foreach {|k,v| l = conj(l,(list k, v))}
    return l
  elsif data.is_a? Hamster::Set
    return (list(*data)).sort
  elsif data.is_a? String
    return list(*data.split(""))
  else
    raise Exception
  end
end

#set(*data) ⇒ Object



21
22
23
# File 'lib/colby/core.rb', line 21

def set(*data)
  Hamster.set(*data)
end

#vec(*data) ⇒ Object



13
14
15
# File 'lib/colby/core.rb', line 13

def vec(*data)
  Hamster.vector(*data)
end

#zip_map(coll1, coll2) ⇒ Object



89
90
91
# File 'lib/colby/core.rb', line 89

def zip_map(coll1, coll2)
  return hash_map(Hash[coll1.to_a.zip(coll2.to_a)])
end