Skip to content

Instantly share code, notes, and snippets.

@haiiro-shimeji
Last active December 16, 2015 11:59
Show Gist options
  • Select an option

  • Save haiiro-shimeji/5431711 to your computer and use it in GitHub Desktop.

Select an option

Save haiiro-shimeji/5431711 to your computer and use it in GitHub Desktop.
$.extend({
foldl: function(array, acc, callback) {
$.each(array, function(_, value) {
acc = callback(value, acc)
})
return acc
},
foldr: function(array, acc, callback) {
for (var i = array.length-1; 0 <= i; --i) {
acc = callback(array[i], acc)
}
return acc
},
foldl1: function(array, callback) {
return $.foldl(array.slice(1), array[0], callback)
},
foldr1: function(array, callback) {
return $.foldr(array.slice(0, array.length-1), array[array.length-1], callback)
},
and: function(array) {
var res = true
$.each(array, function(_, v) {
return res = (v ? true : false)
})
return res
},
or: function(array) {
var res = false
$.each(array, function(_, v) {
return !(res = (v ? true : false))
})
return res
},
curry: function(f, len) {
var c = function(l, args) {
return function(a) {
switch(l) {
case 1:
args.push(a)
return f.apply(this, args)
case 0:
return f.apply(this, args)
default:
args.push(a)
return c(l-1, args)
}
}
}
return c(len || f.length, [])
},
flip: function(curried) {
return function(a) {
return function(b) {
return curried(b)(a)
}
}
},
curryM: function(f) {
var df = function() {
return $.when.apply(this, arguments)
.pipe(function() {
return f.apply(this, arguments)
})
}
df.length = f.length
return $.curry(df, f.length)
}
})
test("$.foldl", function() {
equal(
$.foldl([1, 2, 3], 5, function(v, acc) {
return acc + v
}),
11
)
equal(
$.foldl([], 5, function(v, acc) {
return acc + v
}),
5
)
})
test("$.foldr", function() {
deepEqual(
$.foldr([1, 2, 3], [], function(v, acc) {
acc.push(v)
return acc
}),
[3, 2, 1]
)
deepEqual(
$.foldr([], [], function(v, acc) {
acc.push(v)
return acc
}),
[]
)
var ARRAY = [1, 2, 3]
deepEqual(
$.foldr(ARRAY, [], function(v, acc) {
acc.push(v)
return acc
}),
[3, 2, 1]
)
deepEqual(ARRAY, [1, 2, 3])
})
test("$.foldl1", function() {
equal(
$.foldl1([1, 2, 3], function(v, acc) {
return acc + v
}),
6
)
})
test("$.foldr1", function() {
deepEqual(
$.foldr1([2, 4, 8], function(v, acc) {
return acc / v
}),
1
)
var ARRAY = [2, 4, 8]
$.foldr1(ARRAY, function(v, acc) {
return acc / v
})
deepEqual(ARRAY, [2, 4, 8])
})
test("$.and", function() {
ok($.and([true, true, true]))
ok(!$.and([true, false, true]))
ok($.and([true, "hoge", true]))
ok(!$.and([true, undefined, true]))
ok(!$.and([true, "", true]))
ok(!$.and([true, null, true]))
ok(!$.and([true, 0, true]))
ok($.and([]))
})
test("$.or", function() {
ok($.or([true, true, true]))
ok($.or([true, false, true]))
ok($.or([true, "hoge", true]))
ok($.or([true, undefined, true]))
ok($.or([true, "", true]))
ok($.or([true, null, true]))
ok($.or([true, 0, true]))
ok(!$.or([null, 0, undefined, ""]))
ok(!$.or([]))
})
test("$.curry 0 argument", function() {
var curried = $.curry(function() {
return 10
})
equal(curried(), 10)
})
test("$.curry 1 argument", function() {
var curried = $.curry(function(a) {
return a
})
equal(curried(1), 1)
})
test("$.curry 2 arguments", function() {
var curried = $.curry(function(a, b) {
return a + b
})
//equal(curried(1), function(b) {})
equal(curried(1)(2), 3)
equal(curried(1)(2), 3) //ensure that curried has no state.
})
test("$.curry 3 arguments", function() {
var curried = $.curry(function(a, b, c) {
return a * b * c
})
equal(curried(1)(2)(3), 6)
})
/* this will causes error.
test("$.flip 0 arguments", function() {
equal(
$.flip($.curry(function() {
return 10
}))(),
10
)
})
*/
/* this will causes error.
test("$.flip 1 arguments", function() {
equal(
$.flip($.curry(function(a) {
return a
}))(3),
3
)
})
*/
test("$.flip 2 arguments", function() {
equal(
$.flip($.curry(function(a, b) {
return a - b
}))(3)(5),
5 - 3
)
})
test("$.flip 3 arguments", function() {
equal(
$.flip($.curry(function(a, b, c) {
return a - b * c
}))(3)(5)(10),
5 - 3 * 10
)
})
test("$.curryM", function() {
$.curryM(Math.max)
($.Deferred().resolve(3))
($.Deferred().resolve(5))
.done(function(r) {
equal(r, 5)
})
$.curryM(function(a, b) {
return a + b
})
($.Deferred().resolve(3))
($.Deferred().resolve(5))
.done(function(r) {
equal(r, 8)
})
})
test("$.curryM fail", function() {
$.curryM(function(a, b) {
return a + b
})
($.Deferred().resolve(3))
($.Deferred().reject(5))
.done(function(_) {
ok(false)
})
.fail(function() {
ok(true)
})
})
test("$.curryM flip", function() {
$.flip($.curryM(function(a, b, c) {
return a - b * c
}))
($.Deferred().resolve(3))
($.Deferred().resolve(5))
($.Deferred().resolve(10))
.done(function(r) {
equal(r, 5 - 3 * 10)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment