Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* fun.coffee: initial commit

  • Loading branch information...
commit 780a8287aaf4a2ba50c5df46cf8c7630f431021c 0 parents
David Nolen authored
Showing with 175 additions and 0 deletions.
  1. +165 −0 fun.coffee
  2. +7 −0 license.txt
  3. +3 −0  readme.md
165 fun.coffee
@@ -0,0 +1,165 @@
+inc = (x) -> x + 1
+
+dec = (x) -> x - 1
+
+sum = (a, b) -> a + b
+
+even = (x) -> x % 2 == 0
+
+odd = (x) -> x % 2 == 1
+
+identity = (x) -> x
+
+toFn = (obj) -> (p) -> obj[p]
+
+get = (coll, x) -> if coll then coll[x] else null
+
+getIn = (x, keys) -> reduce get, x, keys
+
+acc = (x, coll) -> coll[x]
+
+flip = (f) -> (a, b) -> f b, a
+
+apply = (f, args) -> f.apply(null, args)
+
+call = (f, args...) -> f.apply(null, args)
+
+partial = (f, rest1...) -> (rest2...) -> f.apply(null, rest1.concat(rest2))
+
+arity = (arities) ->
+ (args...) ->
+ arities[args.length].apply(null, args)
+
+# ==============================================================================
+# Strict Sequences
+
+map = (f, colls...) ->
+ if colls.length == 1
+ f(x) for x, i in colls[0]
+ else
+ first = colls[0]
+ f.apply(null, (map partial(flip(get), i), colls)) for x, i in first
+
+reduce = arity
+ 2: (f, coll) -> reduce(f, coll[0], coll[1..])
+ 3: (f, acc, coll) ->
+ for x, i in coll
+ acc = f(acc, x)
+ acc
+
+filter = (pred, coll) ->
+ x for x in coll when pred(x)
+
+# ==============================================================================
+# Lazy Sequences
+
+class LazySeq
+ constructor: (@head, @tail) ->
+ first: -> @head
+ rest: -> if @tail then @tail() else null
+
+lazy = (coll) ->
+ if coll.length == 0
+ return null
+ h = coll[0]
+ new LazySeq(h, -> lazy(coll[1..]))
+
+toArray = (s) ->
+ acc = []
+ while s
+ acc.push s.first()
+ s = s.rest()
+ acc
+
+integers = arity
+ 0: -> integers 0
+ 1: (x) ->
+ new LazySeq(x, -> integers (x+1))
+
+fib = ->
+ fibSeq = (a, b) -> new LazySeq(a, -> fibSeq b, a+b)
+ fibSeq 0, 1
+
+range = arity
+ 1: (end) -> range 0, end
+ 2: (start, end) ->
+ if start == end
+ null
+ else
+ new LazySeq(start, -> range inc(start), end)
+
+take = (n, s) ->
+ if n == 0 or s == null
+ null
+ else
+ new LazySeq(s.first(), -> take dec(n), s.rest())
+
+last = (s) ->
+ c = null
+ while s
+ c = s.first()
+ s = s.rest()
+ c
+
+lazyMap = (f, s) ->
+ if s
+ new LazySeq(f(s.first()), -> lazyMap f, s.rest())
+ else
+ null
+
+lazyReduce = arity
+ 2: (f, s) -> lazyReduce(f, s.first(), s.rest())
+ 3: (f, acc, s) ->
+ while s
+ acc = f(acc, s.first())
+ s = s.rest()
+ acc
+
+lazyFilter = (pred, s) ->
+ if s
+ h = s.first()
+ if pred h
+ new LazySeq(h, -> lazyFilter pred, s.rest())
+ else
+ lazyFilter pred, s.rest()
+ else
+ null
+
+# ==============================================================================
+# Exports
+
+toExport =
+ inc: inc
+ dec: dec
+ sum: sum
+ even: even
+ odd: odd
+ identity: identity
+ toFn: toFn
+ get: get
+ getIn: getIn
+ flip: flip
+ apply: apply
+ call: call
+ partial: partial
+ map: map
+ reduce: reduce
+ filter: filter
+ LazySeq: LazySeq
+ lazy: lazy
+ toArray: toArray
+ take: take
+ lazyMap: lazyMap
+ lazyReduce: lazyReduce
+ lazyFilter: lazyFilter
+ integers: integers
+ range: range
+ fib: fib
+ last: last
+
+if exports?
+ for n, f of toExport
+ exports[n] = f
+else
+ for n, f of toExport
+ this[n] = f
7 license.txt
@@ -0,0 +1,7 @@
+Copyright (c) 2011 David Nolen
+
+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.
3  readme.md
@@ -0,0 +1,3 @@
+h1. fun.coffeee
+
+Some functional fun for CoffeeScript. One day there'll be documentation. Until that time you'll have to make it on your own ;)
Please sign in to comment.
Something went wrong with that request. Please try again.