Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'dev'

  • Loading branch information...
commit cf0b8c10fc5e71ddb1340a11193379cb32288821 2 parents e708dee + 19f2223
@nikita-volkov authored
View
69 Cakefile
@@ -1,12 +1,27 @@
ChildProcess = require 'child_process'
+FS = require "fs"
+
task "build", "compile to javascript and perform npm-install", ->
- compile -> npmInstall ->
+ clean -> compile -> npmInstall ->
task "run", "build and execute", ->
- compile -> npmInstall -> run "lib/Main.js"
+ clean -> compile -> npmInstall -> run ->
+
+task "publish", "build and publish to npm registry", ->
+ clean -> compile -> npmInstall -> npmPublish ->
+
+task "clean", "clean the lib folder", ->
+ clean ->
+
+
+### Actions ###
+clean = (cb) ->
+ removeDirContents "lib", cb
+npmPublish = (cb) ->
+ executeAndOutput "npm publish", cb
npmInstall = (cb) ->
executeAndOutput "npm install", cb
@@ -14,10 +29,12 @@ npmInstall = (cb) ->
compile = (cb) ->
executeAndOutput "coffee -c -o lib src", cb
-run = (file, cb) ->
- executeAndOutput "node #{file}", cb
+run = (cb) ->
+ executeAndOutput "node lib/Main.js", cb
+### Child process helpers ###
+
executeAndOutput = (cmd, cb) ->
ChildProcess.exec cmd, (error, stdout, stderr) ->
if error
@@ -27,4 +44,46 @@ executeAndOutput = (cmd, cb) ->
console.log stdout
if stderr
console.log stderr
- cb?()
+ cb?()
+
+
+### File system helpers ###
+
+fileExists = (p) -> (try FS.statSync p)?.isFile() ? false
+
+dirExists = (p) -> (try FS.statSync p)?.isDirectory() ? false
+
+paths = (p) -> try p + "/" + sp for sp in FS.readdirSync p
+
+removePath = (path, cb) ->
+ if dirExists path then removeDir path, cb
+ else if fileExists path then removeFile path, cb
+ else cb?()
+
+removeFile = (path, cb) ->
+ try FS.unlink path, cb
+ catch e then cb?()
+
+removeDir = (dir, cb) ->
+ removeDirContents dir, ->
+ try FS.rmdir dir, cb
+ catch e then cb?()
+
+removeDirContents = (dir, cb) ->
+ each removePath, ((paths dir) ? []), cb
+
+
+### Array helpers ###
+
+each = (action, xs, cb) ->
+ if xs.length > 0
+ finished = 0
+ for x in xs
+ action x, ->
+ finished++
+ cb?() if finished == xs.length
+ return
+ else cb?()
+ return
+
+
View
2  package.json
@@ -3,7 +3,7 @@
"name": "Fury",
"description": "A library of higher order functions for function composing, overloading, asynchronicity and other",
"keywords": ["functional", "higher order", "library", "composing", "overloading", "partial application", "async", "coffeescript", "haskell"],
- "version": "0.2.0",
+ "version": "0.3.0",
"homepage": null,
"repository": {
"type": "git",
View
13 src/Fury.coffee
@@ -1,11 +1,14 @@
Function = require "./Fury/Function"
HigherOrderFunction = require "./Fury/HigherOrderFunction"
-ComposedFunctionTemplate = require "./Fury/ComposedFunctionTemplate"
-OverloadedFunctionTemplate = require "./Fury/OverloadedFunctionTemplate"
+Template = require "./Fury/Template"
exports[k] = v for k, v of {
Function
HigherOrderFunction
- ComposedFunctionTemplate
- OverloadedFunctionTemplate
-}
+ Template
+}
+
+exports[k] = v for k, v of Function
+exports[k] = v for k, v of HigherOrderFunction
+exports[k] = v for k, v of Template
+
View
2  src/Fury/ComposedFunctionTemplate.coffee
@@ -56,7 +56,7 @@ func = (template) ->
Function.partiallyApplied (composed tail), head
else if isCorrect head
Array.reduced Function.nested, (composed template)
- else throw "Incorrect template"
+ else throw "Incorrect composed function template"
composed = (xs) ->
for x in xs
View
70 src/Fury/OverloadedFunctionTemplate.coffee
@@ -1,10 +1,6 @@
-###
-OverloadedFunctionTemplate is an alias for PartialFunctionTemplates. Since PartialFunctionTemplate is not implemented being considered useless, this title seems less confusing.
-###
{isInstanceOf} = require "./Object"
PartialFunctionTemplateTerm = require "./PartialFunctionTemplateTerm"
-
exports.partialFunction =
partialFunction = (templates) ->
###
@@ -73,10 +69,74 @@ partialFunction = (templates) ->
exports.func =
func = (templates) ->
+ ###
+ TESTS:
+ - an overloaded by length number writer
+ f = func [
+ [2, (a, b) -> a + b]
+ [4, (a, b, c, d)-> a + b + c + d]
+ ]
+ @equals 3, f 1, 2
+ @equals 10, f 1, 2, 3, 4
+ @fails -> f()
+ @fails -> f 1
+ @fails -> f 1, 2, 3
+ @fails -> f 1, 2, 3, 4, 5
+ @fails -> f 1, 2, 3, 4, 5, 6
+
+ - an overloaded by types array or string joiner
+ f = func [
+ [[String, String], (ys, xs) -> xs + ys]
+ [[Array, Array], (ys, xs) -> xs.concat ys]
+ ]
+ @equals "abcd", f "cd", "ab"
+ @equals [1,2,3,4,5], f [3, 4, 5], [1, 2]
+ @fails -> f 2, 4
+ @fails -> f "ds"
+ @fails -> f "1", "a", "c"
+
+ - an overloaded by types of different length function
+ f = func [
+ [[String, String], -> "2 strings"],
+ [[Array], -> "1 array"]
+ ]
+ @equals "2 strings", f "", ""
+ @equals "1 array", f [2]
+
+ - ignores parameter if type for it is null
+ f = func [
+ [[null, String], -> "ok"]
+ ]
+ @equals "ok", f 2, ""
+ @equals "ok", f "2", ""
+
+ - does not ignore specified parameters
+ f = func [
+ [[null, String], -> "ok"]
+ ]
+ @fails -> f 2, 2
+
+ - treats empty parameters list correctly
+ f = func [
+ [[], -> 1]
+ ]
+ @equals 1, f()
+ @fails -> f 1
+
+ - counts trailing null parameters
+ f = func [
+ [[null], -> 1]
+ [[String, null], -> 2]
+ ]
+ @equals 1, f 0
+ @equals 2, f "0", ""
+ ###
->
try results arguments, templates
catch e
- if e != "Partial function condition not met"
+ if e == "Partial function condition not met"
+ throw "Arguments aren't applicable to this overloaded function"
+ else
throw e
results = (args, templates) ->
View
28 src/Fury/Template.coffee
@@ -0,0 +1,28 @@
+###
+Template is some abstract data used to costruct the output object. Basically what the template represents is implied by the function being called on it, so there may be any kinds of templates implied in this module.
+###
+ComposedFunctionTemplate = require "./ComposedFunctionTemplate"
+OverloadedFunctionTemplate = require "./OverloadedFunctionTemplate"
+
+exports.overloadedFunction =
+overloadedFunction =
+ OverloadedFunctionTemplate.func
+
+exports.composedFunction =
+composedFunction =
+ ComposedFunctionTemplate.func
+
+
+exports.func =
+func = (template) ->
+ ###
+ Automatically detects the type of template. Less performant than direct alternatives.
+ ###
+ if ComposedFunctionTemplate.isCorrect template
+ ComposedFunctionTemplate.func template
+ else
+ OverloadedFunctionTemplate.func template
+ # todo: to add isCorrect check to the OverloadedFunctionTemplate
+ # todo: to add a partial function from PartialFunctionTemplate
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.