prelude-ls-1.1.2/ 000775 000000 000000 00000000000 12525256367 013564 5 ustar 00root root 000000 000000 prelude-ls-1.1.2/.gitignore 000664 000000 000000 00000000034 12525256367 015551 0 ustar 00root root 000000 000000 *.swp
node_modules
coverage
prelude-ls-1.1.2/.travis.yml 000664 000000 000000 00000000044 12525256367 015673 0 ustar 00root root 000000 000000 language: node_js
node_js:
- 0.10
prelude-ls-1.1.2/CHANGELOG.md 000664 000000 000000 00000007625 12525256367 015407 0 ustar 00root root 000000 000000 # 1.1.2
- add `Func.memoize`
- fix `zip-all` and `zip-with-all` corner case (no input)
- build with LiveScript 1.4.0
# 1.1.1
- curry `unique-by`, `minimum-by`
# 1.1.0
- added `List` functions: `maximum-by`, `minimum-by`, `unique-by`
- added `List` functions: `at`, `elem-index`, `elem-indices`, `find-index`, `find-indices`
- added `Str` functions: `capitalize`, `camelize`, `dasherize`
- added `Func` function: `over` - eg. ``same-length = (==) `over` (.length)``
- exported `Str.repeat` through main `prelude` object
- fixed definition of `foldr` and `foldr1`, the new correct definition is backwards incompatible with the old, incorrect one
- fixed issue with `fix`
- improved code coverage
# 1.0.3
- build browser versions
# 1.0.2
- bug fix for `flatten` - slight change with bug fix, flattens arrays only, not array-like objects
# 1.0.1
- bug fixes for `drop-while` and `take-while`
# 1.0.0
* massive update - separated functions into separate modules
* functions do not accept multiple types anymore - use different versions in their respective modules in some cases (eg. `Obj.map`), or use `chars` or `values` in other cases to transform into a list
* objects are no longer transformed into functions, simply use `(obj.)` in LiveScript to do that
* browser version now using browserify - use `prelude = require('prelude-ls')`
* added `compact`, `split`, `flatten`, `difference`, `intersection`, `union`, `count-by`, `group-by`, `chars`, `unchars`, `apply`
* added `lists-to-obj` which takes a list of keys and list of values and zips them up into an object, and the converse `obj-to-lists`
* added `pairs-to-obj` which takes a list of pairs (2 element lists) and creates an object, and the converse `obj-to-pairs`
* removed `cons`, `append` - use the concat operator
* removed `compose` - use the compose operator
* removed `obj-to-func` - use partially applied access (eg. `(obj.)`)
* removed `length` - use `(.length)`
* `sort-by` renamed to `sort-with`
* added new `sort-by`
* removed `compare` - just use the new `sort-by`
* `break-it` renamed `break-list`, (`Str.break-str` for the string version)
* added `Str.repeat` which creates a new string by repeating the input n times
* `unfold` as alias to `unfoldr` is no longer used
* fixed up style and compiled with LiveScript 1.1.1
* use Make instead of Slake
* greatly improved tests
# 0.6.0
* fixed various bugs
* added `fix`, a fixpoint (Y combinator) for anonymous recursive functions
* added `unfoldr` (alias `unfold`)
* calling `replicate` with a string now returns a list of strings
* removed `partial`, just use native partial application in LiveScript using the `_` placeholder, or currying
* added `sort`, `sortBy`, and `compare`
# 0.5.0
* removed `lookup` - use (.prop)
* removed `call` - use (.func arg1, arg2)
* removed `pluck` - use map (.prop), xs
* fixed buys wtih `head` and `last`
* added non-minifed browser version, as `prelude-browser.js`
* renamed `prelude-min.js` to `prelude-browser-min.js`
* renamed `zip` to `zipAll`
* renamed `zipWith` to `zipAllWith`
* added `zip`, a curried zip that takes only two arguments
* added `zipWith`, a curried zipWith that takes only two arguments
# 0.4.0
* added `parition` function
* added `curry` function
* removed `elem` function (use `in`)
* removed `notElem` function (use `not in`)
# 0.3.0
* added `listToObject`
* added `unique`
* added `objToFunc`
* added support for using strings in map and the like
* added support for using objects in map and the like
* added ability to use objects instead of functions in certain cases
* removed `error` (just use throw)
* added `tau` constant
* added `join`
* added `values`
* added `keys`
* added `partial`
* renamed `log` to `ln`
* added alias to `head`: `first`
* added `installPrelude` helper
# 0.2.0
* removed functions that simply warp operators as you can now use operators as functions in LiveScript
* `min/max` are now curried and take only 2 arguments
* added `call`
# 0.1.0
* initial public release
prelude-ls-1.1.2/LICENSE 000664 000000 000000 00000002036 12525256367 014572 0 ustar 00root root 000000 000000 Copyright (c) George Zahariev
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
prelude-ls-1.1.2/Makefile 000664 000000 000000 00000002416 12525256367 015227 0 ustar 00root root 000000 000000 default: all
SRC = $(shell find src -name "*.ls" -type f | sort)
LIB = $(SRC:src/%.ls=lib/%.js)
LS = node_modules/livescript
LSC = node_modules/.bin/lsc
BROWSERIFY = node_modules/.bin/browserify
UGLIFYJS = node_modules/.bin/uglifyjs
MOCHA = node_modules/.bin/mocha
MOCHA2 = node_modules/.bin/_mocha
ISTANBUL = node_modules/.bin/istanbul
lib:
mkdir -p lib/
lib/%.js: src/%.ls lib
$(LSC) --output lib --bare --compile "$<"
browser:
mkdir browser/
prelude-browser.js: $(LIB) browser preroll.ls
{ $(LSC) ./preroll.ls ; $(BROWSERIFY) -r ./lib/index.js:prelude-ls ; } > browser/prelude-browser.js
prelude-browser-min.js: browser/prelude-browser.js
$(UGLIFYJS) browser/prelude-browser.js --mangle --comments "all" > browser/prelude-browser-min.js
package.json: package.json.ls
$(LSC) --compile package.json.ls
.PHONY: build build-browser test coverage dev-install loc clean
all: build
build: $(LIB) package.json
build-browser: prelude-browser.js prelude-browser-min.js
test: build
$(MOCHA) --reporter dot --ui tdd --compilers ls:$(LS)
coverage: build
$(ISTANBUL) cover $(MOCHA2) -- --reporter dot --ui tdd --compilers ls:$(LS)
dev-install: package.json
npm install .
loc:
wc --lines src/*
clean:
rm -f ./*.js
rm -rf lib
rm -rf browser
rm -rf coverage
rm -f package.json
prelude-ls-1.1.2/README.md 000664 000000 000000 00000001145 12525256367 015044 0 ustar 00root root 000000 000000 # prelude.ls [](https://travis-ci.org/gkz/prelude-ls)
is a functionally oriented utility library. It is powerful and flexible. Almost all of its functions are curried. It is written in, and is the recommended base library for, LiveScript.
See **[the prelude.ls site](http://preludels.com)** for examples, a reference, and more.
You can install via npm `npm install prelude-ls`
### Development
`make test` to test
`make build` to build `lib` from `src`
`make build-browser` to build browser versions
prelude-ls-1.1.2/package.json.ls 000664 000000 000000 00000001710 12525256367 016466 0 ustar 00root root 000000 000000 name: 'prelude-ls'
version: '1.1.2'
author: 'George Zahariev '
description: "prelude.ls is a functionally oriented utility library. It is powerful and flexible. Almost all of its functions are curried. It is written in, and is the recommended base library for, LiveScript."
keywords:
'prelude'
'livescript'
'utility'
'ls'
'coffeescript'
'javascript'
'library'
'functional'
'array'
'list'
'object'
'string'
main: 'lib/'
files:
'lib/'
'README.md'
'LICENSE'
homepage: 'http://preludels.com'
bugs: 'https://github.com/gkz/prelude-ls/issues'
licenses:
* type: 'MIT'
url: 'https://raw.github.com/gkz/prelude-ls/master/LICENSE'
...
engines:
node: '>= 0.8.0'
repository:
type: 'git'
url: 'git://github.com/gkz/prelude-ls.git'
scripts:
test: "make test"
dev-dependencies:
livescript: '~1.4.0'
'uglify-js': '~2.4.12'
mocha: '~2.2.4'
istanbul: '~0.2.4'
browserify: '~3.24.13'
sinon: '~1.10.2'
prelude-ls-1.1.2/preroll.ls 000664 000000 000000 00000000366 12525256367 015610 0 ustar 00root root 000000 000000 version = require './lib' .VERSION
console.log """
// Generated by LiveScript 1.2.0
// prelude.ls #version
// Copyright (c) George Zahariev
// Released under the MIT License
// https://raw.githubusercontent.com/gkz/prelude-ls/master/LICENSE
"""
prelude-ls-1.1.2/src/ 000775 000000 000000 00000000000 12525256367 014353 5 ustar 00root root 000000 000000 prelude-ls-1.1.2/src/Func.ls 000664 000000 000000 00000000765 12525256367 015616 0 ustar 00root root 000000 000000 apply = (f, list) -->
f.apply null, list
curry = (f) ->
curry$ f # using util method curry$ from livescript
flip = (f, x, y) --> f y, x
fix = (f) ->
( (g) -> -> f(g g) ...arguments ) do
(g) -> -> f(g g) ...arguments
over = (f, g, x, y) --> f (g x), (g y)
memoize = (f) ->
memo = {}
(...args) ->
key = [arg + typeof! arg for arg in args].join ''
memo[key] = if key of memo then memo[key] else f ...args
#? wrap
module.exports = {
curry, flip, fix, apply, over, memoize
}
prelude-ls-1.1.2/src/List.ls 000664 000000 000000 00000013342 12525256367 015631 0 ustar 00root root 000000 000000 each = (f, xs) -->
for x in xs
f x
xs
map = (f, xs) -->
[f x for x in xs]
compact = (xs) -->
[x for x in xs when x]
filter = (f, xs) -->
[x for x in xs when f x]
reject = (f, xs) -->
[x for x in xs when not f x]
partition = (f, xs) -->
passed = []
failed = []
for x in xs
(if f x then passed else failed).push x
[passed, failed]
find = (f, xs) -->
for x in xs when f x
return x
void
head = first = (xs) ->
xs.0
tail = (xs) ->
return void unless xs.length
xs.slice 1
last = (xs) ->
xs[*-1]
initial = (xs) ->
return void unless xs.length
xs.slice 0, -1
empty = (xs) ->
not xs.length
reverse = (xs) ->
xs.concat!.reverse!
unique = (xs) ->
result = []
for x in xs when x not in result
result.push x
result
unique-by = (f, xs) -->
seen = []
for x in xs
val = f x
continue if val in seen
seen.push val
x
fold = foldl = (f, memo, xs) -->
for x in xs
memo = f memo, x
memo
fold1 = foldl1 = (f, xs) -->
fold f, xs.0, xs.slice 1
foldr = (f, memo, xs) -->
for x in xs by -1
memo = f x, memo
memo
foldr1 = (f, xs) -->
foldr f, xs[*-1], xs.slice 0, -1
unfoldr = (f, b) -->
result = []
x = b
while (f x)?
result.push that.0
x = that.1
result
concat = (xss) ->
[].concat.apply [], xss
concat-map = (f, xs) -->
[].concat.apply [], [f x for x in xs]
flatten = (xs) -->
[].concat.apply [], [(if typeof! x is 'Array' then flatten x else x) for x in xs]
difference = (xs, ...yss) ->
results = []
:outer for x in xs
for ys in yss
continue outer if x in ys
results.push x
results
intersection = (xs, ...yss) ->
results = []
:outer for x in xs
for ys in yss
continue outer unless x in ys
results.push x
results
union = (...xss) ->
results = []
for xs in xss
for x in xs
results.push x unless x in results
results
count-by = (f, xs) -->
results = {}
for x in xs
key = f x
if key of results
results[key] += 1
else
results[key] = 1
results
group-by = (f, xs) -->
results = {}
for x in xs
key = f x
if key of results
results[key].push x
else
results[key] = [x]
results
and-list = (xs) ->
for x in xs when not x
return false
true
or-list = (xs) ->
for x in xs when x
return true
false
any = (f, xs) -->
for x in xs when f x
return true
false
all = (f, xs) -->
for x in xs when not f x
return false
true
sort = (xs) ->
xs.concat!.sort (x, y) ->
if x > y
1
else if x < y
-1
else
0
sort-with = (f, xs) -->
xs.concat!.sort f
sort-by = (f, xs) -->
xs.concat!.sort (x, y) ->
if (f x) > (f y)
1
else if (f x) < (f y)
-1
else
0
sum = (xs) ->
result = 0
for x in xs
result += x
result
product = (xs) ->
result = 1
for x in xs
result *= x
result
mean = average = (xs) ->
sum = 0
for x in xs
sum += x
sum / xs.length
maximum = (xs) ->
max = xs.0
for x in xs.slice 1 when x > max
max = x
max
minimum = (xs) ->
min = xs.0
for x in xs.slice 1 when x < min
min = x
min
maximum-by = (f, xs) -->
max = xs.0
for x in xs.slice 1 when (f x) > (f max)
max = x
max
minimum-by = (f, xs) -->
min = xs.0
for x in xs.slice 1 when (f x) < (f min)
min = x
min
scan = scanl = (f, memo, xs) -->
last = memo
[memo] ++ [last = f last, x for x in xs]
scan1 = scanl1 = (f, xs) -->
return void unless xs.length
scan f, xs.0, xs.slice 1
scanr = (f, memo, xs) -->
xs = xs.concat!.reverse!
(scan f, memo, xs).reverse!
scanr1 = (f, xs) -->
return void unless xs.length
xs = xs.concat!.reverse!
(scan f, xs.0, xs.slice 1).reverse!
slice = (x, y, xs) -->
xs.slice x, y
take = (n, xs) -->
if n <= 0
xs.slice 0, 0
else
xs.slice 0, n
drop = (n, xs) -->
if n <= 0
xs
else
xs.slice n
split-at = (n, xs) --> [(take n, xs), (drop n, xs)]
take-while = (p, xs) -->
len = xs.length
return xs unless len
i = 0
while i < len and p xs[i]
i += 1
xs.slice 0 i
drop-while = (p, xs) -->
len = xs.length
return xs unless len
i = 0
while i < len and p xs[i]
i += 1
xs.slice i
span = (p, xs) --> [(take-while p, xs), (drop-while p, xs)]
break-list = (p, xs) --> span (not) << p, xs
zip = (xs, ys) -->
result = []
len = ys.length
for x, i in xs
break if i is len
result.push [x, ys[i]]
result
zip-with = (f, xs, ys) -->
result = []
len = ys.length
for x, i in xs
break if i is len
result.push f x, ys[i]
result
zip-all = (...xss) ->
min-length = undefined
for xs in xss
min-length = xs.length
[[xs[i] for xs in xss] for i til min-length]
zip-all-with = (f, ...xss) ->
min-length = undefined
for xs in xss
min-length = xs.length
[f.apply(null, [xs[i] for xs in xss]) for i til min-length]
at = (n, xs) -->
if n < 0 then xs[xs.length + n] else xs[n]
elem-index = (el, xs) -->
for x, i in xs when x is el
return i
void
elem-indices = (el, xs) -->
[i for x, i in xs when x is el]
find-index = (f, xs) -->
for x, i in xs when f x
return i
void
find-indices = (f, xs) -->
[i for x, i in xs when f x]
module.exports = {
each, map, filter, compact, reject, partition, find,
head, first, tail, last, initial, empty,
reverse, difference, intersection, union, count-by, group-by,
fold, fold1, foldl, foldl1, foldr, foldr1, unfoldr, and-list, or-list,
any, all, unique, unique-by, sort, sort-with, sort-by, sum, product, mean, average,
concat, concat-map, flatten,
maximum, minimum, maximum-by, minimum-by,
scan, scan1, scanl, scanl1, scanr, scanr1,
slice, take, drop, split-at, take-while, drop-while, span, break-list,
zip, zip-with, zip-all, zip-all-with,
at, elem-index, elem-indices, find-index, find-indices,
}
prelude-ls-1.1.2/src/Num.ls 000664 000000 000000 00000001760 12525256367 015456 0 ustar 00root root 000000 000000 max = (>?)
min = ()
negate = (x) -> -x
abs = Math.abs
signum = (x) ->
if x < 0
-1
else if x > 0
1
else
0
quot = (x, y) --> ~~(x / y)
rem = (%)
div = (x, y) --> Math.floor x / y
mod = (%%)
recip = (1 /)
pi = Math.PI
tau = pi * 2
exp = Math.exp
sqrt = Math.sqrt
ln = Math.log
pow = (^)
sin = Math.sin
tan = Math.tan
cos = Math.cos
asin = Math.asin
acos = Math.acos
atan = Math.atan
atan2 = (x, y) --> Math.atan2 x, y
truncate = (x) -> ~~x
round = Math.round
ceiling = Math.ceil
floor = Math.floor
is-it-NaN = (x) -> x isnt x
even = (x) -> x % 2 == 0
odd = (x) -> x % 2 != 0
gcd = (x, y) -->
x = Math.abs x
y = Math.abs y
until y is 0
z = x % y
x = y
y = z
x
lcm = (x, y) -->
Math.abs Math.floor (x / (gcd x, y) * y)
module.exports = {
max, min, negate, abs, signum, quot, rem, div, mod, recip,
pi, tau, exp, sqrt, ln, pow, sin, tan, cos, acos, asin, atan, atan2,
truncate, round, ceiling, floor, is-it-NaN, even, odd, gcd, lcm,
}
prelude-ls-1.1.2/src/Obj.ls 000664 000000 000000 00000002231 12525256367 015423 0 ustar 00root root 000000 000000 values = (object) ->
[x for , x of object]
keys = (object) ->
[x for x of object]
pairs-to-obj= (object) ->
{[x.0, x.1] for x in object}
obj-to-pairs = (object) ->
[[key, value] for key, value of object]
lists-to-obj= (keys, values) -->
{[key, values[i]] for key, i in keys}
obj-to-lists = (object) ->
keys = []
values = []
for key, value of object
keys.push key
values.push value
[keys, values]
empty = (object) ->
for x of object then return false
true
each = (f, object) -->
for , x of object then f x
object
map = (f, object) -->
{[k, f x] for k, x of object}
compact = (object) -->
{[k, x] for k, x of object when x}
filter = (f, object) -->
{[k, x] for k, x of object when f x}
reject = (f, object) -->
{[k, x] for k, x of object when not f x}
partition = (f, object) -->
passed = {}
failed = {}
for k, x of object
(if f x then passed else failed)[k] = x
[passed, failed]
find = (f, object) -->
for , x of object when f x then return x
void
module.exports = {
values, keys,
pairs-to-obj, obj-to-pairs, lists-to-obj, obj-to-lists,
empty, each, map, filter, compact, reject, partition, find,
}
prelude-ls-1.1.2/src/Str.ls 000664 000000 000000 00000002020 12525256367 015455 0 ustar 00root root 000000 000000 split = (sep, str) -->
str.split sep
join = (sep, xs) -->
xs.join sep
lines = (str) ->
return [] unless str.length
str.split '\n'
unlines = (.join '\n')
words = (str) ->
return [] unless str.length
str.split /[ ]+/
unwords = (.join ' ')
chars = (.split '')
unchars = (.join '')
reverse = (str) ->
str.split '' .reverse!.join ''
repeat = (n, str) -->
result = ''
for til n
result += str
result
capitalize = (str) ->
(str.char-at 0).to-upper-case! + str.slice 1
camelize = (.replace /[-_]+(.)?/g, (, c) -> (c ? '').to-upper-case!)
# convert camelCase to camel-case, and setJSON to set-JSON
dasherize = (str) ->
str
.replace /([^-A-Z])([A-Z]+)/g, (, lower, upper) ->
"#{lower}-#{if upper.length > 1 then upper else upper.to-lower-case!}"
.replace /^([A-Z]+)/, (, upper) ->
if upper.length > 1 then "#upper-" else upper.to-lower-case!
module.exports = {
split, join, lines, unlines, words, unwords, chars, unchars, reverse,
repeat, capitalize, camelize, dasherize,
}
prelude-ls-1.1.2/src/index.ls 000664 000000 000000 00000003042 12525256367 016021 0 ustar 00root root 000000 000000 require! [
'./Func.js'
'./List.js'
'./Obj.js'
'./Str.js'
'./Num.js'
]
id = (x) -> x
is-type = (type, x) --> typeof! x is type
replicate = (n, x) -->
[x for til n]
Str <<< List{
empty, slice, take, drop, split-at, take-while, drop-while, span, break-str: break-list
}
prelude = {
Func, List, Obj, Str, Num,
id, is-type
replicate,
}
prelude <<< List{
each, map, filter, compact, reject, partition, find,
head, first, tail, last, initial, empty,
reverse, difference, intersection, union, count-by, group-by,
fold, foldl, fold1, foldl1, foldr, foldr1, unfoldr, and-list, or-list,
any, all, unique, unique-by, sort, sort-with, sort-by, sum, product, mean, average,
concat, concat-map, flatten,
maximum, minimum, maximum-by, minimum-by,
scan, scanl, scan1, scanl1, scanr, scanr1,
slice, take, drop, split-at, take-while, drop-while, span, break-list,
zip, zip-with, zip-all, zip-all-with,
at, elem-index, elem-indices, find-index, find-indices,
}
prelude <<< Func{
apply, curry, flip, fix, over,
}
prelude <<< Str{
split, join, lines, unlines, words, unwords, chars, unchars,
repeat, capitalize, camelize, dasherize,
}
# not importing all of Obj's functions
prelude <<< Obj{
values, keys,
pairs-to-obj, obj-to-pairs, lists-to-obj, obj-to-lists,
}
prelude <<< Num{
max, min, negate, abs, signum, quot, rem, div, mod, recip,
pi, tau, exp, sqrt, ln, pow, sin, tan, cos, acos, asin, atan, atan2,
truncate, round, ceiling, floor, is-it-NaN, even, odd, gcd, lcm,
}
prelude.VERSION = '1.1.2'
module.exports = prelude
prelude-ls-1.1.2/test/ 000775 000000 000000 00000000000 12525256367 014543 5 ustar 00root root 000000 000000 prelude-ls-1.1.2/test/Func.ls 000664 000000 000000 00000003110 12525256367 015771 0 ustar 00root root 000000 000000 require! \sinon
{apply, curry, flip, fix, over, memoize} = require '..' .Func
{strict-equal: eq, not-strict-equal: not-eq, deep-equal: deep-eq, ok} = require 'assert'
suite 'apply' ->
test 'empty list' ->
f = -> 1
eq 1, apply f, []
test 'a couple of args' ->
eq 5, apply (+), [2 3]
test 'curried' ->
f = apply (+)
eq 5, f [2 3]
suite 'curry' ->
test 'simple function' ->
add = (x, y) -> x + y
add-curried = curry add
add-four = add-curried 4
eq 6 add-four 2
suite 'flip' ->
test 'minus op' ->
eq 10, (flip (-)) 5 15
suite 'fix' ->
test 'single arg' ->
eq 89, (fix (fib) -> (n) ->
| n <= 1 => 1
| otherwise => fib(n-1) + fib(n-2))(10)
test 'multi-arg variation' ->
eq 89, (fix (fib) -> (n, minus=0) ->
| (n - minus) <= 1 => 1
| otherwise => fib(n, minus+1) + fib(n, minus+2))(10)
suite 'over' ->
test 'basic' ->
f = (==) `over` (-> it)
ok f 2 2
ok not f 2 3
test 'with accessor function' ->
same-length = (==) `over` (.length)
ok same-length [1 2 3] [4 5 6]
ok not same-length [1 2] [4 5 6]
suite 'memoize' ->
spy = f = null
setup ->
spy := sinon.spy -> &
f := memoize spy
test 'only 1 call when using the same arguments' ->
[0 to 10].for-each -> f!
ok spy.called-once
test 'call again when using different arguments' ->
f \mung
f \mung
f '1,2'
f [1 2]
ok spy.called-thrice
test 'that the correct values are returned' ->
eq f(\mung), f(\mung)
eq f(\mung \face), f(\mung \face)
not-eq f(\mung), f(\mung \face)
prelude-ls-1.1.2/test/List.ls 000664 000000 000000 00000047402 12525256367 016025 0 ustar 00root root 000000 000000 {
id
Num: {even, odd, floor, is-it-NaN}
List: {
each, map, filter, compact, reject, partition, find,
head, first, tail, last, initial, empty,
reverse, difference, intersection, union, count-by, group-by,
fold, fold1, foldl, foldl1, foldr, foldr1, unfoldr, and-list, or-list,
any, all, unique, unique-by, sort, sort-with, sort-by, sum, product, mean, average,
concat, concat-map, flatten,
maximum, minimum, maximum-by, minimum-by,
scan, scan1, scanl, scanl1, scanr, scanr1,
slice, take, drop, split-at, take-while, drop-while, span, break-list,
zip, zip-with, zip-all, zip-all-with,
at, elem-index, elem-indices, find-index, find-indices,
}
} = require '..'
{strict-equal: eq, deep-equal: deep-eq, ok} = require 'assert'
suite 'each' ->
test 'empty list as input' ->
deep-eq [], each id, []
test 'side effects affect input (and thus result)' ->
deep-eq [[1],[2]] each (.pop!), [[1 5] [2 6]]
test 'curried' ->
f = each (.pop!)
deep-eq [[1],[2]], f [[1 5] [2 6]]
suite 'map' ->
test 'empty list as input' ->
deep-eq [], map id, []
test 'mapping over array' ->
deep-eq [2 3 4], map (+ 1), [1 2 3]
test 'curried' ->
f = map (+ 1)
deep-eq [2 3 4], f [1 2 3]
suite 'compact' ->
test 'empty list as input' ->
deep-eq [], compact []
test 'compacting array' ->
deep-eq [1 true 'ha'], compact [0 1 false true '' 'ha']
suite 'filter' ->
test 'empty list as input' ->
deep-eq [], filter id, []
test 'filtering array' ->
deep-eq [2, 4], filter even, [1 to 5]
test 'filter on true returns original list' ->
deep-eq [1 2 3], filter (-> true), [1 2 3]
test 'filter on false returns empty list' ->
deep-eq [], filter (-> false), [1 2 3]
test 'curried' ->
f = filter even
deep-eq [2, 4], f [1 to 5]
suite 'reject' ->
test 'empty list as input' ->
deep-eq [], reject id, []
test 'reject list' ->
deep-eq [1 3 5], reject even, [1 to 5]
test 'reject on true returns empty list' ->
deep-eq [], reject (-> true), [1 2 3]
test 'reject on true returns original list' ->
deep-eq [1 2 3], reject (-> false), [1 2 3]
test 'curried' ->
f = reject even
deep-eq [1 3 5], f [1 to 5]
suite 'partition' ->
test 'empty list as input' ->
deep-eq [[],[]], partition id, []
test 'partition list' ->
deep-eq [[76 88 77 90],[49 58 43]], partition (>60), [49 58 76 43 88 77 90]
test 'partition on true returns original list as passing, empty list as failing' ->
deep-eq [[1 2 3],[]], partition (-> true), [1 2 3]
test 'partition on false returns empty list as failing, empty list as passing' ->
deep-eq [[], [1 2 3]], partition (-> false), [1 2 3]
test 'curried' ->
f = partition (>60)
deep-eq [[76 88 77 90],[49 58 43]], f [49 58 76 43 88 77 90]
suite 'find' ->
test 'empty list as input' ->
eq void, find id, []
test 'find from list' ->
eq 4, find even, [3 1 4 8 6]
test 'finding nothing when function always false' ->
eq void, find (-> false), [1 2 3]
test 'find first item when function always true' ->
eq 1, find (-> true), [1 2 3]
test 'curried' ->
f = find even
eq 4, f [3 1 4 8 6]
suite 'list portions' ->
list = [1 2 3 4 5]
suite 'head' ->
test 'empty list as input' ->
eq void, head []
test 'list' ->
eq 1, head list
test 'first as alias' ->
eq first, head
suite 'tail' ->
test 'empty list as input' ->
eq void, tail []
test 'list' ->
deep-eq [2 3 4 5], tail list
test 'one element list' ->
deep-eq [], tail [1]
suite 'last' ->
test 'empty list as input' ->
eq void, last []
test 'list' ->
eq 5, last list
test 'one element list' ->
eq 1, last [1]
suite 'initial' ->
test 'empty list as input' ->
eq void, initial []
test 'list' ->
deep-eq [1 2 3 4], initial list
test 'one element list' ->
deep-eq [], initial [1]
suite 'empty' ->
test 'empty list as input' ->
ok empty []
test 'non-empty list as input' ->
ok not empty [1]
suite 'reverse' ->
test 'empty list as input' ->
deep-eq [], reverse []
test 'reverse list, it is unmodified' ->
list = [1 2 3 4 5]
deep-eq [5 4 3 2 1], reverse list
deep-eq [1 2 3 4 5], list
suite 'unique' ->
test 'empty list as input' ->
deep-eq [], unique []
test 'unique list' ->
deep-eq [1,2,3,4,5,6], unique [1 1 2 3 3 4 5 5 5 5 5 6 6 6 6]
test 'mixed string/num' ->
deep-eq ['1' 1 2 4 5], unique ['1' 1 2 4 5 5]
suite 'unique-by' ->
test 'empty list as input' ->
deep-eq [], unique-by id, []
test 'basic' ->
deep-eq [1,2,3,4,5,6], unique-by id, [1 1 2 3 3 4 5 5 5 5 5 6 6 6 6]
test 'accessor' ->
deep-eq [[] [1 2 3] [4] [5 6]], unique-by (.length), [[], [1 2 3], [4], [5 6], [7], [8 9 10]]
test 'curried' ->
deep-eq [1,2,3,4,5,6], (unique-by id) [1 1 2 3 3 4 5 5 5 5 5 6 6 6 6]
suite 'fold' ->
test 'empty list as input' ->
eq 0, fold (+), 0, []
test 'list as input' ->
eq 12, fold (+), 0, [1 2 3 6]
test 'foldl is alias' ->
eq fold, foldl
test 'curried' ->
f = fold (+)
eq 12, f 0, [1 2 3 6]
g = fold (+), 0
eq 12 g [1 2 3 6]
suite 'fold1' ->
test 'empty list as input' ->
eq void, fold1 (+), []
test 'list as input' ->
eq 12, fold1 (+), [1 2 3 6]
test 'foldl1 as alais' ->
eq fold1, foldl1
test 'curried' ->
f = fold1 (+)
eq 12, f [1 2 3 6]
suite 'foldr' ->
test 'empty list as input' ->
eq 0, foldr (+), 0, []
test 'list as input' ->
eq 7, foldr (-), 9, [1 2 3 4]
eq 'abcde', foldr (+), 'e', <[ a b c d ]>
test 'curried' ->
f = foldr (-)
eq 7, f 9, [1 2 3 4]
g = foldr (-), 9
eq 7, g [1 2 3 4]
suite 'foldr1' ->
test 'empty list as input' ->
eq void, foldr1 (+), []
test 'list as input' ->
eq 7, foldr1 (-), [1 2 3 4 9]
eq 'abcde', foldr1 (+), <[ a b c d e ]>
test 'curried' ->
f = foldr1 (-)
eq 7, f [1 2 3 4 9]
suite 'unfoldr' ->
test 'complex case' ->
deep-eq [10,9,8,7,6,5,4,3,2,1], unfoldr (-> if it == 0 then null else [it, it - 1]), 10
test 'returning null right away results in a one item list' ->
deep-eq [], unfoldr (-> null), 'a'
test 'curried' ->
f = unfoldr (-> if it == 0 then null else [it, it - 1])
deep-eq [10,9,8,7,6,5,4,3,2,1], f 10
suite 'concat' ->
test 'empty list as input' ->
deep-eq [], concat []
test 'multiple lists' ->
deep-eq [1,2,3,4,5,6], concat [[1 2] [3 4] [5 6]]
suite 'concat-map' ->
test 'empty list as input' ->
deep-eq [], concat-map id, []
test 'using mapping and concatinating' ->
deep-eq [1,1,2,1,2,3], concat-map (-> [1 to it]), [1 2 3]
test 'curried' ->
f = concat-map (-> [1 to it])
deep-eq [1,1,2,1,2,3], f [1 2 3]
suite 'flatten' ->
test 'empty list as input' ->
deep-eq [], flatten []
test 'nested lists as input' ->
deep-eq [1,2,3,4,5], flatten [1, [[2], 3], [4, [[5]]]]
test 'lists with strings' ->
deep-eq ['a','b','c','d','e'], flatten ['a', [['b'], 'c'], ['d', [['e']]]]
suite 'difference' ->
test 'empty list(s) as input' ->
deep-eq [], difference []
deep-eq [], difference [] []
test 'subtract nothing' ->
deep-eq [1 2 3], difference [1 2 3]
deep-eq [1 2 3], difference [1 2 3] []
test 'subtract single element' ->
deep-eq [2 3], difference [1 2 3] [1]
test 'subtract multiple elements' ->
deep-eq [1 3 4], difference [1 2 3 4 5] [5 2 10] [9]
suite 'intersection' ->
test 'empty list(s) as input' ->
deep-eq [], intersection []
deep-eq [], intersection [] []
test 'no common elements' ->
deep-eq [],intersection [2 3] [9 8] [12 1] [99]
test 'some common elements' ->
deep-eq [1 2], intersection [1 2 3] [101 2 1 10] [2 1] [-1 0 1 2]
test 'all common elements' ->
deep-eq [1 2 3], intersection [1 2 3] [2 1 3] [3 1 2]
suite 'union' ->
test 'empty list(s) as input' ->
deep-eq [], union []
deep-eq [], union [] []
test 'list and empty list' ->
deep-eq [1 2 3], union [1 2 3] []
test 'with various' ->
deep-eq [1 5 7 3], union [1 5 7] [3 5] []
suite 'count-by' ->
test 'empty list as input' ->
deep-eq [], count-by id, []
test 'list of numbers' ->
deep-eq {4: 1, 6: 2}, count-by floor, [4.2, 6.1, 6.4]
test 'list of strings' ->
deep-eq {3: 2, 5: 1}, count-by (.length), <[ one two three ]>
test 'curried' ->
f = count-by floor
deep-eq {4: 1, 6: 2}, f [4.2, 6.1, 6.4]
suite 'group-by' ->
test 'empty list as input' ->
deep-eq [], group-by id, []
test 'list of numbers' ->
deep-eq {4: [4.2], 6: [6.1 6.4]}, group-by floor, [4.2, 6.1, 6.4]
test 'list of strings' ->
deep-eq {3: <[ one two ]>, 5: <[ three ]>}, group-by (.length), <[ one two three ]>
test 'curried' ->
f = group-by floor
deep-eq {4: [4.2], 6: [6.1 6.4]}, f [4.2, 6.1, 6.4]
suite 'and-list' ->
test 'empty list as input' ->
ok and-list []
test 'true' ->
ok and-list [true, 2 + 2 == 4]
test 'false' ->
ok not and-list [true true false true]
suite 'or-list' ->
test 'empty list as input' ->
ok not or-list []
test 'true' ->
ok or-list [false false false true false]
test 'false' ->
ok not or-list [false, 2 + 2 == 3]
suite 'any' ->
test 'empty list as input' ->
ok not any id, []
test 'true' ->
ok any even, [1 4 3]
test 'false' ->
ok not any even, [1 3 7 5]
test 'curried' ->
f = any even
ok f [1 4 3]
suite 'all' ->
test 'empty list as input' ->
ok all even, []
test 'true' ->
ok all even, [2 4 6]
test 'false' ->
ok not all even, [2 5 6]
test 'curried' ->
f = all even
ok f [2 4 6]
suite 'sort' ->
test 'empty list as input' ->
deep-eq [], sort []
test 'single digit numbers' ->
deep-eq [1,2,3,4,5,6], sort [3 1 5 2 4 6]
test 'multi digit numbers' ->
deep-eq [2,5,6,12,334,4999], sort [334 12 5 2 4999 6]
test 'same digits' ->
deep-eq [1 2 2 2 3], sort [2 3 2 1 2]
suite 'sort-with' ->
f = (x, y) ->
| x.length > y.length => 1
| x.length < y.length => -1
| otherwise => 0
test 'empty list as input' ->
deep-eq [], sort-with id, []
test 'complex case' ->
deep-eq ['one','two','three'], sort-with f, <[ three one two ]>
test 'curried' ->
g = sort-with f
deep-eq ['one','two','three'], g <[ three one two ]>
suite 'sort-by' ->
arr =
'hey'
'a'
'there'
'hey'
'ha'
test 'empty list as input' ->
deep-eq [], sort-by id, []
test 'complex case' ->
deep-eq ['a', 'ha', 'hey', 'hey', 'there'], sort-by (.length), arr
test 'curried' ->
f = sort-by (.length)
deep-eq ['a', 'ha', 'hey', 'hey', 'there'], f arr
suite 'sum' ->
test 'empty list as input' ->
eq 0, sum []
test 'list as input' ->
eq 10, sum [1 2 3 4]
test 'negative numbers' ->
eq -2, sum [1 -2 3 -4]
suite 'product' ->
test 'empty list as input' ->
eq 1, product []
test 'list as input' ->
eq 24, product [1 2 3 4]
test 'negative numbers' ->
eq -6, product [1 -2 3]
suite 'mean' ->
test 'empty list as input' ->
ok is-it-NaN mean []
test 'list as input' ->
eq 4, mean [2 3 4 5 6]
test 'average as alias' ->
eq mean, average
suite 'maximum' ->
test 'empty list as input' ->
eq void, maximum []
test 'single element list' ->
eq 1, maximum [1]
test 'multi element list' ->
eq 6, maximum [1 2 6 4 5]
suite 'minimum' ->
test 'empty list as input' ->
eq void, minimum []
test 'single element list' ->
eq 1, minimum [1]
test 'multi element list' ->
eq 2, minimum [4 3 2 6 9]
test 'list of strings' ->
eq 'a', minimum ['c', 'e', 'a', 'd', 'b']
suite 'maximum-by' ->
test 'empty list as input' ->
eq void, maximum-by id, []
test 'single element list' ->
eq 1, maximum-by id, [1]
test 'multi element list' ->
eq 'long-string', maximum-by (.length), <[ hi there I am a really long-string ]>
test 'curried' ->
eq 2, (maximum-by id) [1 2 0]
suite 'minimum-by' ->
test 'empty list as input' ->
eq void, minimum-by id, []
test 'single element list' ->
eq 1, minimum-by id, [1]
test 'multi element list' ->
eq 'I', minimum-by (.length), <[ hi there I am a really long-string ]>
test 'curried' ->
eq 0, (minimum-by id) [1 2 0]
suite 'scan' ->
test 'empty list as input' ->
deep-eq [null], scan id, null, []
test 'complex case' ->
deep-eq [4,9,20,43], scan ((x, y) -> 2 * x + y), 4, [1 2 3]
test 'scanl as alias' ->
eq scan, scanl
test 'curried' ->
f = scan ((x, y) -> 2 * x + y)
deep-eq [4,9,20,43], f 4, [1 2 3]
g = scan ((x, y) -> 2 * x + y), 4
deep-eq [4,9,20,43], g [1 2 3]
suite 'scan1' ->
test 'empty list as input' ->
deep-eq void, scan1 id, []
test 'complex case' ->
deep-eq [1,3,6,10], scan1 (+), [1 2 3 4]
test 'scanl1 as alias' ->
eq scan1, scanl1
test 'curried' ->
f = scan1 (+)
deep-eq [1,3,6,10], f [1 2 3 4]
suite 'scanr' ->
test 'empty list as input' ->
deep-eq [null], scanr id, null, []
test 'complex case' ->
deep-eq [15,14,12,9,5], scanr (+), 5, [1 2 3 4]
test 'curried' ->
f = scanr (+)
deep-eq [15,14,12,9,5], f 5, [1 2 3 4]
g = scanr (+), 5
deep-eq [15,14,12,9,5], g [1 2 3 4]
suite 'scanr1' ->
test 'empty list as input' ->
deep-eq void, scanr1 id, []
test 'complex case' ->
deep-eq [10,9,7,4], scanr1 (+), [1 2 3 4]
test 'curried' ->
f = scanr1 (+)
deep-eq [10,9,7,4], f [1 2 3 4]
suite 'slice' ->
test 'zero to zero' ->
deep-eq [], slice 0 0 [1 2 3 4 5]
test 'empty lsit as input' ->
deep-eq [], slice 2 3 []
test 'parts' ->
deep-eq [3 4], slice 2 4 [1 2 3 4 5]
test 'curried' ->
f = slice 2
deep-eq [3 4], f 4 [1 2 3 4 5]
g = slice 2 4
deep-eq [3 4], g [1 2 3 4 5]
suite 'take' ->
test 'empty list as input' ->
deep-eq [], take 3 []
test 'zero on list' ->
deep-eq [], take 0 [1 2 3 4 5]
test 'negative number' ->
deep-eq [], take -1 [1 2 3 4 5]
test 'too big number' ->
deep-eq [1 2 3 4 5], take 9 [1 2 3 4 5]
test 'list' ->
deep-eq [1 2 3], take 3 [1 2 3 4 5]
test 'curried' ->
f = take 3
deep-eq [1 2 3], f [1 2 3 4 5]
suite 'drop' ->
test 'empty list as input' ->
deep-eq [], drop 3 []
test 'zero on list' ->
deep-eq [1 2 3 4 5], drop 0 [1 2 3 4 5]
test 'negative number' ->
deep-eq [1 2 3 4 5], drop -1 [1 2 3 4 5]
test 'too big number' ->
deep-eq [], drop 9 [1 2 3 4 5]
test 'list' ->
deep-eq [4 5], drop 3 [1 2 3 4 5]
test 'curried' ->
f = drop 3
deep-eq [4 5], f [1 2 3 4 5]
suite 'split-at' ->
test 'empty list as input' ->
deep-eq [[], []], split-at 3 []
test 'zero on list' ->
deep-eq [[], [1 2 3 4 5]], split-at 0 [1 2 3 4 5]
test 'negative number' ->
deep-eq [[], [1 2 3 4 5]], split-at -1 [1 2 3 4 5]
test 'too big number' ->
deep-eq [[1 2 3 4 5], []], split-at 9 [1 2 3 4 5]
test 'list' ->
deep-eq [[1 2 3], [4 5]], split-at 3 [1 2 3 4 5]
test 'curried' ->
f = split-at 3
deep-eq [[1 2 3], [4 5]], f [1 2 3 4 5]
suite 'take-while' ->
test 'empty list as input' ->
deep-eq [], take-while id, []
test 'list' ->
deep-eq [1 3 5], take-while odd, [1 3 5 4 8 7 9]
test 'all pass' ->
deep-eq [42], take-while (== 42), [42]
deep-eq [2 4 8], take-while even, [2 4 8]
test 'all fail' ->
deep-eq [], take-while (== 7), [42]
deep-eq [], take-while odd, [2 4 8]
test 'curried' ->
f = take-while odd
deep-eq [1 3 5], f [1 3 5 4 8 7 9]
suite 'drop-while' ->
test 'empty list as input' ->
deep-eq [], drop-while id, []
test 'list' ->
deep-eq [7 9 10], drop-while even, [2 4 6 7 9 10]
test 'all pass' ->
deep-eq [], drop-while (== 42), [42]
deep-eq [], drop-while even, [2 4 8]
test 'all-fail' ->
deep-eq [42], drop-while (== 7), [42]
deep-eq [2 4 8], drop-while odd, [2 4 8]
test 'curried' ->
f = drop-while even
deep-eq [7 9 10], f [2 4 6 7 9 10]
suite 'span' ->
test 'empty list as input' ->
deep-eq [[], []], span id, []
test 'list' ->
deep-eq [[2 4 6], [7 9 10]], span even, [2 4 6 7 9 10]
test 'curried' ->
f = span even
deep-eq [[2 4 6], [7 9 10]], f [2 4 6 7 9 10]
suite 'break-list' ->
test 'empty list as input' ->
deep-eq [[], []], break-list id, []
test 'list' ->
deep-eq [[1 2], [3 4 5]], break-list (== 3), [1 2 3 4 5]
test 'curried' ->
f = break-list (== 3)
deep-eq [[1 2], [3 4 5]], f [1 2 3 4 5]
suite 'zip' ->
test 'empty lists as input' ->
deep-eq [], zip [] []
test 'equal length lists' ->
deep-eq [[1 4], [2 5]], zip [1 2] [4 5]
test 'first list shorter' ->
deep-eq [[1 4], [2 5]], zip [1 2] [4 5 6]
test 'second list shorter' ->
deep-eq [[1 4], [2 5]], zip [1 2 3] [4 5]
test 'curried' ->
f = zip [1 2]
deep-eq [[1 4], [2 5]], f [4 5]
suite 'zip-with' ->
test 'empty lists as input' ->
deep-eq [], zip-with id, [], []
test 'equal length lists' ->
deep-eq [4 4 4], zip-with (+), [1 2 3], [3 2 1]
test 'first list shorter' ->
deep-eq [5 7], zip-with (+), [1 2], [4 5 6]
test 'second list shorter' ->
deep-eq [5 7], zip-with (+), [1 2 3] [4 5]
test 'curried' ->
f = zip-with (+)
deep-eq [4 4 4], f [1 2 3], [3 2 1]
g = zip-with (+), [1 2 3]
deep-eq [4 4 4], g [3 2 1]
suite 'zip-all' ->
test 'no lists as input' ->
deep-eq [], zip-all!
test 'empty lists as input' ->
deep-eq [], zip-all [] [] []
test 'equal length lists' ->
deep-eq [[1 4 7], [2 5 8], [3 6 9]], zip-all [1 2 3] [4 5 6] [7 8 9]
test 'first list shorter' ->
deep-eq [[1 4 7], [2 5 8]], zip-all [1 2] [4 5 6] [7 8 9]
test 'second list shorter' ->
deep-eq [[1 4 7], [2 5 8]], zip-all [1 2 3] [4 5] [7 8 9]
test 'third list shorter' ->
deep-eq [[1 4 7], [2 5 8]], zip-all [1 2 3] [4 5 6] [7 8]
suite 'zip-all-with' ->
test 'nothing as input' ->
deep-eq [], zip-all-with!
test 'no lists as input' ->
deep-eq [], zip-all-with id
test 'empty lists as input' ->
deep-eq [], zip-all-with id, [], []
test 'equal length lists' ->
deep-eq [5 5 5], zip-all-with (-> &0 + &1 + &2), [1 2 3], [3 2 1], [1 1 1]
test 'first list shorter' ->
deep-eq [5 7], zip-all-with (+), [1 2], [4 5 6]
test 'second list shorter' ->
deep-eq [5 7], zip-all-with (+), [1 2 3] [4 5]
suite 'at' ->
test 'empty list as input' ->
eq void, at 0, []
test 'positive n' ->
eq 2, at 1, [1 2 3]
test 'negative n' ->
eq 3, at -1, [1 2 3]
test 'not defined at index' ->
eq void, at 10, [1 2 3]
test 'curried' ->
eq 2, (at 1) [1 2 3]
suite 'elem-index' ->
test 'empty list as input' ->
eq void, elem-index 2, []
test 'basic' ->
eq 1, elem-index 2, [1 2 3]
test 'multiple' ->
eq 1, elem-index 2, [1 2 3 2 1]
test 'not there' ->
eq void, elem-index 5, [1 2 3]
test 'curried' ->
eq 1, (elem-index 2) [1 2 3]
suite 'elem-indices' ->
test 'empty list as input' ->
deep-eq [], elem-indices 2, []
test 'single' ->
deep-eq [1], elem-indices 2, [1 2 3]
test 'multiple' ->
deep-eq [1, 3], elem-indices 2, [1 2 3 2 1]
test 'not there' ->
deep-eq [], elem-indices 5, [1 2 3]
test 'curried' ->
deep-eq [1], (elem-indices 2) [1 2 3]
suite 'find-index' ->
test 'empty list as input' ->
eq void, find-index id, []
test 'basic' ->
eq 1, find-index (== 2), [1 2 3]
test 'multiple' ->
eq 1, find-index even, [1 2 3 4]
test 'not there' ->
eq void, find-index odd, [2 4 6]
test 'curried' ->
eq 1, (find-index (== 2)) [1 2 3]
suite 'find-indices' ->
test 'empty list as input' ->
deep-eq [], find-indices id, []
test 'basic' ->
deep-eq [1], find-indices (== 2), [1 2 3]
test 'multiple' ->
deep-eq [1, 3], find-indices even, [1 2 3 4]
test 'not there' ->
deep-eq [], find-indices odd, [2 4 6]
test 'curried' ->
deep-eq [1], (find-indices (== 2)) [1 2 3]
prelude-ls-1.1.2/test/Num.ls 000664 000000 000000 00000007556 12525256367 015657 0 ustar 00root root 000000 000000 {
max, min, negate, abs, signum, quot, rem, div, mod, recip,
pi, tau, exp, sqrt, ln, pow, sin, tan, cos, acos, asin, atan, atan2,
truncate, round, ceiling, floor, is-it-NaN, even, odd, gcd, lcm,
} = require '..'
{strict-equal: eq, deep-equal: deep-eq, ok} = require 'assert'
suite 'max' ->
test 'numbers' ->
eq 3, max 3 3
eq 3, max 2 3
eq 3, max 3 2
test 'characters' ->
eq \b, max \a \b
test 'curried' ->
f = max 2
eq 3, f 3
suite 'min' ->
test 'numbers' ->
eq 0, min 9 0
test 'characters' ->
eq \a, min \a \b
test 'curried' ->
f = min 9
eq 0, f 0
suite 'negate' ->
test 'zero' ->
eq 0, negate 0
test 'negative number' ->
eq -2, negate 2
test 'positive number' ->
eq 3, negate -3
suite 'abs' ->
test 'zero' ->
eq 0, abs 0
test 'negative number' ->
eq 4 abs -4
test 'positive number' ->
eq 4 abs 4
suite 'signum' ->
test 'zero' ->
eq 0 signum 0
test 'negative number' ->
eq -1 signum -5.3
test 'positive number' ->
eq 1 signum 8
suite 'quot' ->
test 'simple' ->
eq -6, quot -20 3
test 'curried' ->
f = quot -20
eq -6, f 3
suite 'rem' ->
test 'simple' ->
eq -2, rem -20 3
test 'curried' ->
f = rem -20
eq -2, f 3
suite 'div' ->
test 'simple' ->
eq -7, div -20 3
test 'curried' ->
f = div -20
eq -7, f 3
suite 'mod' ->
test 'simple' ->
eq 1, mod -20 3
test 'curried' ->
f = mod -20
eq 1, f 3
suite 'recip' ->
test 'zero' ->
eq Infinity, recip 0
test 'larger than 1' ->
eq 0.5, recip 2
test 'between 0 and 1' ->
eq 2, recip 0.5
suite 'pi' ->
test 'constant' ->
eq 3.141592653589793, pi
suite 'tau' ->
test 'constant' ->
eq 6.283185307179586, tau
suite 'exp' ->
test 'simple' ->
eq 2.718281828459045, exp 1
suite 'sqrt' ->
test 'negative numbers' ->
ok is-it-NaN sqrt -1
test 'simple' ->
eq 2 sqrt 4
suite 'ln' ->
test 'simple' ->
eq 0.6931471805599453, ln 2
suite 'pow' ->
test 'simple' ->
eq 4, pow 2 2
eq 4, pow -2 2
test 'with negative numbers' ->
eq 0.25, pow 2 -2
test 'between one and zero' ->
eq 4, pow 16 0.5
test 'curried' ->
f = pow 2
eq 4, f 2
suite 'sin' ->
test 'zero' ->
eq 0, sin 0
test 'pi/2' ->
eq 1, sin pi/2
suite 'tan' ->
test 'zero' ->
eq 0, tan 0
suite 'cos' ->
test 'zero' ->
eq 1, cos 0
test 'pi' ->
eq -1, cos pi
suite 'acos' ->
test 'number' ->
eq 1.4706289056333368, acos 0.1
suite 'asin' ->
test 'number' ->
eq 1.5707963267948966, asin 1
suite 'atan' ->
test 'number' ->
eq 0.7853981633974483, atan 1
suite 'atan2' ->
test 'number' ->
eq 0.4636476090008061, atan2 1 2
test 'curried' ->
f = atan2 1
eq 0.4636476090008061, f 2
suite 'truncate' ->
test 'zero' ->
eq 0, truncate 0
test 'positive number' ->
eq 1 truncate 1.5
test 'negative number' ->
eq -1 truncate -1.5
suite 'round' ->
test 'up' ->
eq 1 round 0.6
eq 1 round 0.5
test 'down' ->
eq 0 round 0.4
suite 'ceiling' ->
test 'zero' ->
eq 0, ceiling 0
test 'positive number' ->
eq 1, ceiling 0.1
test 'negative number' ->
eq 0, ceiling -0.9
suite 'floor' ->
test 'zero' ->
eq 0, floor 0
test 'positive number' ->
eq 0, floor 0.9
test 'negative number' ->
eq -1, floor -0.1
suite 'is-it-NaN' ->
test 'true' ->
ok is-it-NaN Math.sqrt -1
test 'false' ->
ok not is-it-NaN '0'
suite 'even' ->
test 'true' ->
ok even 0
ok even -2
test 'false' ->
ok not even 7
suite 'odd' ->
test 'true' ->
ok odd 3
test 'false' ->
ok not odd -4
ok not odd 0
suite 'gcd' ->
test 'some numbers' ->
eq 6, gcd 12 18
test 'curried' ->
f = gcd 12
eq 6, f 18
suite 'lcm' ->
test 'some numbers' ->
eq 36, lcm 12 18
test 'curried' ->
f = lcm 12
eq 36, f 18
prelude-ls-1.1.2/test/Obj.ls 000664 000000 000000 00000006632 12525256367 015624 0 ustar 00root root 000000 000000 {
id
Obj: {
values, keys,
pairs-to-obj, obj-to-pairs, lists-to-obj, obj-to-lists,
empty, each, map, filter, compact, reject, partition, find,
}
} = require '..'
{strict-equal: eq, deep-equal: deep-eq, ok} = require 'assert'
suite 'values' ->
test 'empty object as input' ->
deep-eq [], values {}
test 'object as input' ->
deep-eq [1 2 3], values sadf: 1, asdf: 2, fdas: 3
suite 'keys' ->
test 'empty object as input' ->
deep-eq [], keys {}
test 'object as input' ->
deep-eq <[ sadf asdf fdas ]>, keys sadf: 1, asdf: 2, fdas: 3
suite 'pairs-to-obj' ->
test 'empty list as input' ->
deep-eq {}, pairs-to-obj []
test 'pairs as input' ->
deep-eq {a: 'b', c: 'd', e: 1}, pairs-to-obj [['a' 'b'] ['c' 'd'] ['e' 1]]
suite 'obj-to-pairs' ->
test 'empty object as input' ->
deep-eq [], obj-to-pairs {}
test 'object as input' ->
deep-eq [['a' 'b'] ['c' 'd'] ['e' 1]], obj-to-pairs {a: 'b', c: 'd', e: 1}
suite 'lists-to-obj' ->
test 'empty lists as input' ->
deep-eq {}, lists-to-obj [] []
test 'two lists of the same length' ->
deep-eq {a: 1, b: 2, c: 3}, lists-to-obj <[ a b c ]> [1 2 3]
test 'first list is shorter' ->
deep-eq {a: 1, b: 2}, lists-to-obj <[ a b ]> [1 2 3]
test 'first list is longer' ->
deep-eq {a: 1, b: 2, c: void}, lists-to-obj <[ a b c ]> [1 2]
test 'curried' ->
f = lists-to-obj <[ a b c ]>
deep-eq {a: 1, b: 2, c: 3}, f [1 2 3]
suite 'obj-to-lists' ->
test 'empty object as input' ->
deep-eq [[], []], obj-to-lists {}
test 'two lists of the same length' ->
deep-eq [<[ a b c ]>, [1 2 3]], obj-to-lists {a: 1, b: 2, c: 3}
suite 'empty' ->
test 'empty object as input' ->
ok empty {}
test 'non-empty object as input' ->
ok not empty {x: 1}
suite 'each' ->
test 'empty object as input' ->
deep-eq {}, each id, {}
test 'iterate over object values' ->
count = 4
each (-> count += it), {a: 1, b: 2, c: 3}
eq 10 count
test 'curried' ->
count = 4
f = each (-> count += it)
f {a: 1, b: 2, c: 3}
eq 10 count
suite 'map' ->
test 'empty object as input' ->
deep-eq {}, map id, {}
test 'mapping over object' ->
deep-eq {a:2, b:4}, map (* 2), {a:1, b:2}
test 'curried' ->
f = map (* 2)
deep-eq {a:2, b:4}, f {a:1, b:2}
suite 'compact' ->
test 'empty object as input' ->
deep-eq {}, compact {}
test 'compacting object' ->
deep-eq {b: 1, e: 'ha'}, compact {a: 0, b: 1, c: false, d: '', e: 'ha'}
suite 'filter' ->
test 'empty object as input' ->
deep-eq {}, filter id, {}
test 'filtering object' ->
deep-eq {b: 2}, filter (== 2), {a:1, b:2}
test 'curried' ->
f = filter (== 2)
deep-eq {b: 2}, f {a:1, b:2}
suite 'reject' ->
test 'empty object as input' ->
deep-eq {}, reject id, {}
test 'reject object' ->
deep-eq {a: 1}, reject (==2), {a:1, b:2}
test 'curried' ->
f = reject (== 2)
deep-eq {a: 1}, f {a:1, b:2}
suite 'partition' ->
test 'empty object as input' ->
deep-eq [{}, {}], partition id, {}
test 'partition object' ->
deep-eq [{b: 2}, {a: 1, c: 3}], partition (==2), {a:1, b:2, c:3}
test 'curried' ->
f = partition (== 2)
deep-eq [{b: 2}, {a: 1, c: 3}], f {a:1, b:2, c:3}
suite 'find' ->
test 'empty object as input' ->
eq void, find id, {}
test 'find from object' ->
eq 2, find (==2), {a:1, b:2}
test 'curried' ->
f = find (== 2)
eq 2, f {a:1, b:2}
prelude-ls-1.1.2/test/Str.ls 000664 000000 000000 00000012352 12525256367 015656 0 ustar 00root root 000000 000000 {
id
Str: {
split, join, lines, unlines, words, unwords, chars, unchars, empty, reverse
repeat, capitalize, camelize, dasherize
slice, take, drop, split-at, take-while, drop-while, span, break-str
}
} = require '..'
{strict-equal: eq, deep-equal: deep-eq, ok} = require 'assert'
suite 'split' ->
test 'empty string as input' ->
deep-eq [], split '' ''
test 'string of some length' ->
deep-eq <[ 1 2 3 ]>, split '|' '1|2|3'
test 'curried' ->
f = split '|'
deep-eq <[ 1 2 3 ]>, f '1|2|3'
suite 'join' ->
test 'empty list as input' ->
eq '' join '', []
test 'list as input' ->
eq '1,2,3', join ',' [1 2 3]
test 'empty string as seperator' ->
eq '123', join '' [1 2 3]
test 'curried' ->
f = join ','
eq '1,2,3', f [1 2 3]
suite 'lines' ->
test 'empty string as input' ->
deep-eq [], lines ''
test 'string as input' ->
deep-eq <[ one two three ]>, lines 'one\ntwo\nthree'
suite 'unlines' ->
test 'empty array as input' ->
eq '', unlines []
test 'array as input' ->
eq 'one\ntwo\nthree', unlines [\one \two \three]
suite 'words' ->
test 'empty string as input' ->
deep-eq [], words ''
test 'string as input' ->
deep-eq <[ what is this ]>, words 'what is this'
suite 'unwords' ->
test 'empty array as input' ->
eq '', unwords []
test 'array as input' ->
eq 'what is this', unwords [\what \is \this]
suite 'chars' ->
test 'empty string as input' ->
deep-eq [], chars ''
test 'string as input' ->
deep-eq <[ h e l l o ]>, chars 'hello'
suite 'unchars' ->
test 'empty array as input' ->
eq '', unchars []
test 'array as input' ->
eq 'there', unchars ['t', 'h', 'e', 'r', 'e']
suite 'empty' ->
test 'empty string as input' ->
ok empty ''
test 'string as input' ->
ok not empty 'a'
suite 'reverse' ->
test 'empty string as input' ->
eq '', reverse ''
test 'a string' ->
eq 'cba', reverse 'abc'
eq 'olleh', reverse 'hello'
suite 'repeat' ->
test 'zero times' ->
eq '' repeat 0 'hi'
test 'empty string as input' ->
eq '', repeat 2 ''
test 'a string several times' ->
eq 'aa', repeat 2 'a'
eq 'hihihi', repeat 3 'hi'
eq 'hihihihihihihihihihi', repeat 10 'hi'
test 'curried' ->
f = repeat 2
eq 'aa', f 'a'
suite 'capitalize' ->
test 'empty string as input' ->
eq '', capitalize ''
test 'basic' ->
eq 'Foo', capitalize 'foo'
suite 'camelize' ->
test 'empty string as input' ->
eq '', camelize ''
test 'no change' ->
eq 'fooBar', camelize 'fooBar'
test 'dashes' ->
eq 'fooBar', camelize 'foo-bar'
test 'underscore' ->
eq 'fooBar', camelize 'foo_bar'
test 'ending dash' ->
eq 'fooBar', camelize 'foo-bar-'
test 'more than one' ->
eq 'fooBar', camelize 'foo--bar'
suite 'dasherize' ->
test 'empty string as input' ->
eq '', dasherize ''
test 'no change' ->
eq 'foo-bar', dasherize 'foo-bar'
test 'basic' ->
eq 'foo-bar', dasherize 'fooBar'
test 'with numbers' ->
eq 'f1-bar', dasherize 'f1Bar'
test 'repeated capitals' ->
eq 'set-JSON', dasherize 'setJSON'
test 'starting with capital' ->
eq 'foo-bar', dasherize 'FooBar'
test 'starting with repeated capitals' ->
eq 'JSON-get', dasherize 'JSONget'
suite 'slice' ->
test 'zero to zero' ->
eq '', slice 0 0 'hello'
test 'empty string as input' ->
eq '', slice 2 3 ''
test 'parts' ->
eq 'll', slice 2 4 'hello'
test 'curried' ->
f = slice 2
eq 'll', f 4 'hello'
g = slice 2 4
eq 'll', g 'hello'
suite 'take' ->
test 'empty string as input' ->
eq '', take 3 ''
test 'zero on string' ->
eq '', take 0 'abcde'
test 'string' ->
eq 'ab', take 2 'abcde'
test 'curried' ->
f = take 2
eq 'ab', f 'abcde'
suite 'drop' ->
test 'empty string as input' ->
eq '', drop 3 ''
test 'zero on string' ->
eq 'abcde', drop 0 'abcde'
test 'string' ->
eq 'cde', drop 2 'abcde'
test 'curried' ->
f = drop 2
eq 'cde', f 'abcde'
suite 'split-at' ->
test 'empty string as input' ->
deep-eq ['', ''], split-at 3 ''
test 'zero on string' ->
deep-eq ['', 'abcde'], split-at 0 'abcde'
test 'string' ->
deep-eq ['ab', 'cde'], split-at 2 'abcde'
test 'curried' ->
f = split-at 2
deep-eq ['ab', 'cde'], f 'abcde'
suite 'take-while' ->
test 'empty string as input' ->
eq '', take-while id, ''
test 'string' ->
eq 'mmmmm', take-while (is 'm'), 'mmmmmhmm'
test 'curried' ->
f = take-while (is 'm')
eq 'mmmmm', f 'mmmmmhmm'
suite 'drop-while' ->
test 'empty string as input' ->
eq '', drop-while id, ''
test 'string' ->
eq 'hmm', drop-while (is \m), 'mmmmmhmm'
test 'curried' ->
f = drop-while (is \m)
eq 'hmm', f 'mmmmmhmm'
suite 'span' ->
test 'empty string as input' ->
deep-eq ['', ''], span id, ''
test 'string' ->
deep-eq ['mmmmm', 'hmm'], span (is \m), 'mmmmmhmm'
test 'curried' ->
f = span (is \m)
deep-eq ['mmmmm', 'hmm'], f 'mmmmmhmm'
suite 'break-str' ->
test 'empty string as input' ->
deep-eq ['', ''], break-str id, ''
test 'string' ->
deep-eq ['mmmmm', 'hmm'], break-str (is \h), 'mmmmmhmm'
test 'curried' ->
f = break-str (is \h)
deep-eq ['mmmmm', 'hmm'], f 'mmmmmhmm'
prelude-ls-1.1.2/test/browser.html 000664 000000 000000 00000000203 12525256367 017107 0 ustar 00root root 000000 000000
prelude-ls-1.1.2/test/index.ls 000664 000000 000000 00000002101 12525256367 016204 0 ustar 00root root 000000 000000 {id, is-type, replicate, VERSION} = require '..'
{strict-equal: eq, deep-equal: deep-eq, ok} = require 'assert'
suite 'library' ->
test 'version' ->
eq VERSION, (require '../package.json').version
suite 'id' ->
test 'number' ->
eq 5, id 5
test 'object is the same' ->
obj = {}
eq obj, id obj
suite 'is-type' ->
test 'literals' ->
ok is-type 'Undefined' void
ok is-type 'Boolean' true
ok is-type 'Number' 1
ok is-type 'Number' 1.2
ok is-type 'String' 'asdfa'
ok is-type 'Object' {}
ok is-type 'Array' []
ok not is-type 'Boolean' 1
test 'constructors' ->
ok is-type 'Date' new Date
test 'classes' ->
class A
ok is-type 'Object' new A
test 'curried' ->
f = is-type 'Boolean'
ok f true
suite 'replicate' ->
test 'zero as input' ->
deep-eq [], replicate 0 0
deep-eq [], replicate 0 'a'
test 'number as input' ->
deep-eq [3,3,3,3], replicate 4 3
test 'string as input' ->
deep-eq <[ a a a a ]>, replicate 4 'a'
test 'curried' ->
f = replicate 4
deep-eq [3,3,3,3], f 3