Having CoffeeScript for Functional Programming is cool, and it will be much better with this coffee-mate, which provides a lot of util functions for js and some of them are especially for coffeescript, includes log macro, dict comprehension, function cart
for nested list comprehensions, pseudo-random, type transformation , string formating and a sort of high-order helper functions for functional programming.
log
: can be used as a convenient log macrodict
: can be used to construct an dict conveniently just like dict comprehensionsleep
: sleep syntactic sugar, second is used instead of millisecondcopy
: return a copy of an Array or Object, the second argument indicates the depth of copyingdeepcopy
: almost same as copy, but the default depth is Infinity
int
: transform string to int, base can be specified, returnnull
with illegal stringfloat
: transform string to float, returnnull
with illegal stringstr
: transform int to string, base can be specifiedhex
: transform int to hex string. (base is specified as 16)ord
: return the ascii code of a charchr
: convert an ascii code to a charjson
: return the json string of an objectobj
: convert the json string to an object
some_string.format
: given an dictionary, return a formated stringsome_string.repeat
: given a integer, return a repeated versionsome_string.uri_decode
: decode uri to get a dictionary of the params from url
some_array.first
: this is an attribute of an array, means the first element in itsome_array.second
: this is an attribute of an array, means the second element in itsome_array.third
: this is an attribute of an array, means the third element in itsome_array.last
: this is an attribute of an array, means the last element in it
some_object.size
: return the keys number of an dictionary(object), that's just a shotcut forObject.keys(obj).length
some_object.extend
: return a extended version of an dictionary(object), values from the param dictionaries will be treated as defaultssome_object.update
: return a extended version of an dictionary(object), values from the param dictionaries will be treated as updatessome_object.uri_encode
: encode a dictionary to get an uri which will be appended after a url as params
random_gen
: given a seed, return a random generator which generate an decimal in range[0, 1)
each timeranged_random_gen
: given an integern
and a seed, return a random generator which generate an integer in range[0, n-1]
iterator
: given an array, return it's iterator form, an iterator will pass through directly.iterator.end
: the symbol of iterating ending. ifiterating.end
is returned, the iterating is finished.list
: given an iterator, return an array, an array will pass through directly.foreach
: given an iterator, an callback function, an optional init value for result, executecallback(item, result)
for each item yeild by the iterator, finally return the result value(which should be modified by the callback directly).foreach.break
: the symbol of foreach breaking. ifforeach.break
is returned, the foreach loop is finished.enumerate
: given an iterator, array or object, returns an iterator which yields[key, value]
pairs.nature_number
: given an optional first element, returns an unlimitted increasing integer iterator.range
: 0~3 arguments is allowed, just like the python function range(), returns a limitted integer iterator.head
: given a nature numbern
, return a function which accepts an iteratoriter
and returns an iterator which yields the firstn
item fromiter
.best
: given a function describing which one is better, return a function describing which one is bestall
: given a judge function, return a function deals with an iterator, which returns true only if for every item in the iterator, the judge function returns trueany
: given a judge function, return a function deals with an iterator, which returns true if for any item in the iterator, the judge function returns truezip
: given a sort of iterators, return a zipped one which is also an iterator.cart
: given a sort of iterators, return their cartesian product which is also an iterator.church
: given a nature numbern
, return the church numbern
.Y
: the Y Combinator, given a high-order function, return it's fixed point which is also a function.memorize
: given a function, return it's memorized version.
square
: given x, return x * xcube
: given x, return x * x * xabs
: alias of Math.absfloor
: alias of Math.floorceil
: alias of Math.ceilsum
: given an array of numbers, return their summax
: given an array of numbers, return the greatest onemin
: given an array of numbers, return the smallest onemax_index
: given an array of numbers, return the index of the greatest onemin_index
: given an array of numbers, return the index of the smallest one
- install with npm:
npm install coffee-mate
- require separately:
{log, dict, sleep} = require 'coffee-mate'
- require globally:
require 'coffee-mate/global'
<script src="http://cdn.rawgit.com/luochen1990/coffee-mate/master/coffee_mate.js" type="text/javascript"></script>
<script src="http://cdn.rawgit.com/luochen1990/coffee-mate/master/global.js" type="text/javascript"></script>
run coffee ./test.coffee
under directory coffee-mate/
directly after you have coffee-script
installed.