Permalink
Browse files

Add elem and notElem functions.

  • Loading branch information...
1 parent 1bac526 commit b706c1861885df77d922769514c704dcf8056cca @beastaugh committed Jun 18, 2011
Showing with 60 additions and 2 deletions.
  1. +2 −2 CHANGELOG
  2. +22 −0 site/index.md
  3. +12 −0 src/udon.js
  4. +24 −0 test/specs/udon_spec.js
View
@@ -4,8 +4,8 @@ Udon changelog
Edge
----
-* Added `id`, `concat`, `sum`, `product`, `reverse`, `intersperse` and
- `intercalate` functions.
+* Added `id`, `concat`, `sum`, `product`, `elem`, `notElem`, `reverse`,
+ `intersperse` and `intercalate` functions.
Version 1.1.0
-------------
View
@@ -53,6 +53,8 @@ API summary
- [`none`](#api-none)
- [`sum`](#api-sum)
- [`product`](#api-product)
+ - [`elem`](#api-elem)
+ - [`notElem`](#api-notElem)
- [`partition`](#api-partition)
- [`unfoldr`](#api-unfoldr)
- [`zip`](#api-zip)
@@ -374,6 +376,26 @@ Udon.sum([1, 2, 3, 4]);
// -> 24
~~~
+<h3 id="api-elem"><code>elem</code></h3>
+
+Returns `true` if the given element is in the array, `false` otherwise.
+
+~~~{.JavaScript}
+Udon.elem(2, [1, 2, 3]) === true;
+
+Udon.elem(4, [1, 2, 3]) === false;
+~~~
+
+<h3 id="api-notElem"><code>notElem</code></h3>
+
+Returns `false` if the given element is in the array, and `true` if it is not.
+
+~~~{.JavaScript}
+Udon.notElem('Z', ['a', 'b', 'c']) === true;
+
+Udon.notElem('Z', ['X', 'Y', 'Z']) === false;
+~~~
+
<h3 id="api-partition"><code>partition</code></h3>
Separates a list into lists of those elements which do and do not satisfy some
View
@@ -159,6 +159,18 @@ Udon.product = function(ns) {
return t;
};
+Udon.elem = function(e, xs) {
+ var i = xs.length;
+ while (i--) if (e === xs[i]) return true;
+ return false;
+};
+
+Udon.notElem = function(e, xs) {
+ var i = xs.length, notInList = true;
+ while (i--) if (e === xs[i]) notInList = false;
+ return notInList;
+};
+
Udon.partition = function(f, xs) {
var ts = [], fs = [], len = xs.length, i, e;
for (i = 0; i < len; i++) {
View
@@ -391,6 +391,30 @@ JS.ENV.UdonSpec = JS.Test.describe('Udon', function() { with (this) {
}});
});
+ describe('elem', function() {
+ it('`elem` should return false when an element is not present in an array', function() { with(this) {
+ assertEqual(false, Udon.elem(5, []));
+ assertEqual(false, Udon.elem(5, [1, 2, 3, 4]));
+ }});
+
+ it('`elem` should return true when an element is present in an array', function() { with(this) {
+ assert(Udon.elem(5, [1, 2, 3, 4, 5]));
+ assert(Udon.elem(5, [5, 4, 3, 2, 1]));
+ }});
+ });
+
+ describe('notElem', function() {
+ it('`notElem` should return true when an element is not present in an array', function() { with(this) {
+ assert(Udon.notElem(5, []));
+ assert(Udon.notElem(5, [1, 2, 3, 4]));
+ }});
+
+ it('`notElem` should return false when an element is present in an array', function() { with(this) {
+ assertEqual(false, Udon.notElem(5, [1, 2, 3, 4, 5]));
+ assertEqual(false, Udon.notElem(5, [5, 4, 3, 2, 1]));
+ }});
+ });
+
describe('partition', function() {
it('`partition` should partition the elements of an array into arrays of those satisfying and failing to satisfy some predicate', function() { with(this) {
var isNegative = function(n) { return n < 0; };

0 comments on commit b706c18

Please sign in to comment.