Browse Source

Implement cons

master
Dylan Baker 5 years ago
parent
commit
c5189a5c87
2 changed files with 16 additions and 0 deletions
  1. 10
    0
      lib/chervil/core.rb
  2. 6
    0
      spec/core_spec.rb

+ 10
- 0
lib/chervil/core.rb View File

@@ -92,6 +92,16 @@ module Chervil
92 92
         else
93 93
           error
94 94
         end
95
+      end,
96
+      "cons" => Proc.new do |args|
97
+        error = arity_check(args, 2)
98
+        if error.nil?
99
+          el, els = args
100
+          error = type_check([els], Array)
101
+          if error.nil?
102
+            els.prepend(el)
103
+          end
104
+        end
95 105
       end
96 106
     }
97 107
   end

+ 6
- 0
spec/core_spec.rb View File

@@ -57,5 +57,11 @@ module Chervil
57 57
         Error.new("`car` expects a non-empty list")
58 58
       )
59 59
     end
60
+
61
+    it 'implements cons' do
62
+      expect(Core::CORE['cons'].call([1.0, [2.0, 3.0]])).to eq(
63
+        [1.0, 2.0, 3.0]
64
+      )
65
+    end
60 66
   end
61 67
 end

Loading…
Cancel
Save