├── LICENSE ├── README.md ├── algorithms ├── algorithm.wren └── test_algorithm.wren ├── queue ├── queue.wren └── test_queue.wren ├── set ├── set.wren └── test_set.wren └── stack ├── stack.wren └── test_stack.wren /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 minirop 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wren-helpers 2 | Classes available in other languages written in wren 3 | 4 | * Queue 5 | * Set 6 | * Stack 7 | 8 | Algorithms available in class *Algorithm* 9 | 10 | * none_of(list, predicate): return `true` if `predicate` returns `true` for no elements of `list`. 11 | * equal(list1, list2): return `true` if `list1` and `list2` have the same elements in the same order. 12 | * equal(list1, list2, predicate): return `true` if `predicate.call(list1[i], list2[i])` returns `true` for all `i`. 13 | * generate(value, size): return a list of `size` elements all being `value`. 14 | * remove(list, value): remove all occurences of `value` in `list`. 15 | * remove_if(list, predicate): remove all values of `list` where `predicate` returns `true`. 16 | * reverse(list): reverse the order of the elements of `list`. 17 | * unique(list): remove consecutive duplicates in `list`. 18 | * min(list): return the minimum element of `list`. 19 | * max(list): return the maximum element of `list`. 20 | -------------------------------------------------------------------------------- /algorithms/algorithm.wren: -------------------------------------------------------------------------------- 1 | class Algorithm { 2 | static none_of(list, predicate) { 3 | return !list.any(predicate) 4 | } 5 | 6 | static equal(list1, list2) { 7 | if (list1.count != list2.count) { 8 | return false 9 | } 10 | 11 | for (i in 0...list1.count) { 12 | if (list1[i] != list2[i]) { 13 | return false 14 | } 15 | } 16 | return true 17 | } 18 | 19 | static equal(list1, list2, predicate) { 20 | if (list1.count != list2.count) { 21 | return false 22 | } 23 | 24 | for (i in 0...list1.count) { 25 | if (predicate.call(list1[i], list2[i]) == false) { 26 | return false 27 | } 28 | } 29 | return true 30 | } 31 | 32 | static generate(value, size) { 33 | var list = [] 34 | for (i in 0...size) { 35 | list.add(value) 36 | } 37 | return list 38 | } 39 | 40 | static remove(list, value) { 41 | var newList = [] 42 | for (i in 0...list.count) { 43 | if (list[i] != value) { 44 | newList.add(list[i]) 45 | } 46 | } 47 | return newList 48 | } 49 | 50 | static remove_if(list, predicate) { 51 | var newList = [] 52 | for (i in 0...list.count) { 53 | if (predicate.call(list[i]) == false) { 54 | newList.add(list[i]) 55 | } 56 | } 57 | return newList 58 | } 59 | 60 | static reverse(list) { 61 | var newList = [] 62 | for (i in (list.count - 1)..0) { 63 | newList.add(list[i]) 64 | } 65 | return newList 66 | } 67 | 68 | static unique(list) { 69 | if (list.count == 0) return list 70 | 71 | var newList = [ list[0] ] 72 | var lastOne = list[0] 73 | for (i in 1...list.count) { 74 | if (list[i] != lastOne) { 75 | newList.add(list[i]) 76 | lastOne = list[i] 77 | } 78 | } 79 | return newList 80 | } 81 | 82 | static min(list) { 83 | if (list.count == 0) return null 84 | 85 | var minimum = list[0] 86 | for (i in 1...list.count) { 87 | if (list[i] < minimum) { 88 | minimum = list[i] 89 | } 90 | } 91 | return minimum 92 | } 93 | 94 | static max(list) { 95 | if (list.count == 0) return null 96 | 97 | var maximum = list[0] 98 | for (i in 1...list.count) { 99 | if (list[i] > maximum) { 100 | maximum = list[i] 101 | } 102 | } 103 | return maximum 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /algorithms/test_algorithm.wren: -------------------------------------------------------------------------------- 1 | import "algorithm" for Algorithm 2 | 3 | var list = [52, 11, 73, 12, 535, 23, 63, 88, 57, 12] 4 | 5 | IO.print("tested list: ", list) 6 | 7 | IO.print("none_of < 80: ", Algorithm.none_of(list) {|item| 8 | return item < 80 9 | }) 10 | IO.print("none_of < 10: ", Algorithm.none_of(list) {|item| 11 | return item < 10 12 | }) 13 | 14 | IO.print("equals to itself? ", Algorithm.equal(list, list)) 15 | IO.print("equals to an empty list? ", Algorithm.equal(list, [])) 16 | 17 | IO.print("min element: ", Algorithm.min(list)) 18 | IO.print("max element: ", Algorithm.max(list)) 19 | 20 | IO.print("a list of 3: ", Algorithm.generate(3, 5)) 21 | 22 | IO.print("after removing values '12': ", Algorithm.remove(list, 12)) 23 | IO.print("after removing values > 50: ", Algorithm.remove_if(list) {|item| 24 | return item > 50 25 | }) 26 | 27 | IO.print("reverse of [1, 2, 3]: ", Algorithm.reverse([1, 2, 3])) 28 | 29 | var uniqueness = [1,1,2,2,3,1,1,4,4,2,2,6] 30 | IO.print("(consecutive) uniqueness of ", uniqueness, ": ", Algorithm.unique(uniqueness)) 31 | -------------------------------------------------------------------------------- /queue/queue.wren: -------------------------------------------------------------------------------- 1 | class Queue { 2 | construct new() { 3 | _data = [] 4 | } 5 | 6 | clear() { 7 | _data = [] 8 | } 9 | 10 | front { 11 | if (count > 0) return _data[0] 12 | } 13 | count { _data.count } 14 | push(item) { _data.add(item) } 15 | pop() { _data.removeAt(0) } 16 | } 17 | -------------------------------------------------------------------------------- /queue/test_queue.wren: -------------------------------------------------------------------------------- 1 | import "queue" for Queue 2 | 3 | var q = Queue.new() 4 | q.push(42) 5 | q.push(33) 6 | q.push(12) 7 | IO.print(q.count, " elements: ", q.front) // prints "3 elements: 42" 8 | q.push(6) 9 | IO.print(q.count, " elements: ", q.front) // prints "4 elements: 42" 10 | var old_top = q.pop() 11 | IO.print(q.count, " elements: ", q.front, ", ", old_top) // prints "3 elements: 33, 42" 12 | -------------------------------------------------------------------------------- /set/set.wren: -------------------------------------------------------------------------------- 1 | class Set { 2 | construct new() { 3 | _data = [] 4 | } 5 | 6 | construct new(items) { 7 | _data = [] 8 | for (item in items) { 9 | insert(item) 10 | } 11 | } 12 | 13 | clear() { 14 | _data = [] 15 | } 16 | 17 | contains(item) { _data.contains(item) } 18 | count { _data.count } 19 | isEmpty { count == 0 } 20 | ! { isEmpty } 21 | toString { _data.toString } 22 | 23 | insert(item) { 24 | for (index in 0...count) { 25 | if (item < _data[index]) { 26 | _data.insert(index, item) 27 | return 28 | } else { 29 | if (item == _data[index]) { 30 | return 31 | } 32 | } 33 | } 34 | _data.add(item) 35 | } 36 | 37 | iterate(iterator) { _data.iterate(iterator) } 38 | iteratorValue(iterator) { _data.iteratorValue(iterator) } 39 | 40 | remove(item) { 41 | for (index in 0...count) { 42 | if (item == _data[index]) { 43 | _data.removeAt(index) 44 | return true 45 | } 46 | } 47 | return false 48 | } 49 | 50 | |(other) { 51 | var newSet = Set.new() 52 | for (item in _data) { 53 | newSet.insert(item) 54 | } 55 | for (item in other) { 56 | newSet.insert(item) 57 | } 58 | return newSet 59 | } 60 | 61 | &(other) { 62 | var newSet = Set.new() 63 | for (item in other) { 64 | if (_data.contains(item)) { 65 | newSet.insert(item) 66 | } 67 | } 68 | return newSet 69 | } 70 | 71 | +(other) { this | other } 72 | 73 | -(other) { 74 | var newSet = Set.new(_data) 75 | for (item in other) { 76 | if (_data.contains(item)) { 77 | newSet.remove(item) 78 | } 79 | } 80 | return newSet 81 | } 82 | 83 | ^(other) { 84 | return (this | other) - (this & other) 85 | } 86 | 87 | *(other) { 88 | var cartesianProduct = [] 89 | for (first in _data) { 90 | for (second in other) { 91 | cartesianProduct.add([first, second]) 92 | } 93 | } 94 | return cartesianProduct 95 | } 96 | 97 | ==(other) { 98 | if (count != other.count) return false 99 | 100 | for (i in 0...count) { 101 | if (!other.contains(_data[i])) return false 102 | } 103 | 104 | return true 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /set/test_set.wren: -------------------------------------------------------------------------------- 1 | import "set" for Set 2 | 3 | var s = Set.new() 4 | s.insert(1) 5 | s.insert(3) 6 | s.insert(6) 7 | IO.print(s.count, " elements") // prints "3 elements" 8 | s.insert(3) 9 | IO.print(s.count, " elements") // still prints "3 elements" 10 | s.remove(6) 11 | IO.print(s.count, " elements") // prints "2 elements" 12 | 13 | IO.print(s.contains(3), " ", s.contains(4)) // prints "true false" 14 | 15 | for (i in s) { 16 | IO.print(i) // prints 1 then 3 17 | } 18 | 19 | class User { 20 | construct new (id) { 21 | _id = id 22 | } 23 | 24 | <(other) { 25 | return other.id < _id 26 | } 27 | 28 | // reimplemented so User are equals if their id are equals (instead of being the same instance) 29 | ==(other) { 30 | return other.id == _id 31 | } 32 | 33 | id { 34 | return _id 35 | } 36 | } 37 | 38 | var x = Set.new() 39 | x.insert(User.new(1)) 40 | x.insert(User.new(2)) 41 | x.insert(User.new(1)) 42 | IO.print(x.count, " elements") // prints "2 elements" 43 | 44 | var set1 = Set.new([1, 2, 3]) 45 | var set2 = Set.new([3, 4, 5]) 46 | IO.print(set1, " | ", set2, " = ", (set1 | set2)) 47 | IO.print(set1, " & ", set2, " = ", (set1 & set2)) 48 | IO.print(set1, " + ", set2, " = ", (set1 + set2)) 49 | IO.print(set1, " - ", set2, " = ", (set1 - set2)) 50 | IO.print(set1, " ^ ", set2, " = ", (set1 ^ set2)) 51 | IO.print(set1, " * ", set2, " = ", (set1 * set2)) 52 | 53 | var set3 = Set.new([5, 4, 3]) 54 | IO.print(set2, " == ", set3, " = ", (set2 == set3)) 55 | IO.print(!set1) 56 | set1.clear() 57 | IO.print(!set1) 58 | -------------------------------------------------------------------------------- /stack/stack.wren: -------------------------------------------------------------------------------- 1 | class Stack { 2 | construct new() { 3 | _data = [] 4 | } 5 | 6 | clear() { 7 | _data = [] 8 | } 9 | 10 | top { 11 | if (count > 0) return _data[-1] 12 | } 13 | count { _data.count } 14 | push(item) { _data.add(item) } 15 | pop() { _data.removeAt(-1) } 16 | } 17 | -------------------------------------------------------------------------------- /stack/test_stack.wren: -------------------------------------------------------------------------------- 1 | import "stack" for Stack 2 | 3 | var s = Stack.new() 4 | s.push(42) 5 | s.push(33) 6 | s.push(12) 7 | IO.print(s.count, " elements: ", s.top) // prints "3 elements: 12" 8 | s.push(6) 9 | IO.print(s.count, " elements: ", s.top) // prints "4 elements : 6" 10 | var six = s.pop() 11 | IO.print(s.count, " elements: ", s.top, " -> ", six) // prints "3 elements: 12 -> 6" 12 | --------------------------------------------------------------------------------