Skip to content

Commit

Permalink
learn
Browse files Browse the repository at this point in the history
  • Loading branch information
abstain23 committed Jun 15, 2020
1 parent 474e096 commit d1188f1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
5 changes: 5 additions & 0 deletions canvas/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Regcode {
constructor() {

}
}
23 changes: 23 additions & 0 deletions curry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function curry(fn, length) {
length = length || fn.length
return function(...args) {
if (args.length < length) {
return curry(sub_curry(fn, ...args),length - args.length)
} else {
return fn.call(this, ...args)
}
}
}

function sub_curry(fn, ...args) {
return function (...newArgs) {
return fn.apply(this, [...args, ...newArgs])
}
}

var fn = curry(function (a, b, c) {
return [a,b,c]
})
console.log(fn(1,2, 44))
console.log(fn(1, 2)(44))
console.log(fn(1)(2)(44))
48 changes: 47 additions & 1 deletion 专题系列/数组去重.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,50 @@ function unique(array) {

//es6最简单

let unique = (a) => [...new Set(a)]
// let unique = (a) => [...new Set(a)]
var arr = [1, 1, '1', '1']

function unique(arr) {
var res = []
for (var i = 0; i < arr.length; i++) {
// if (!(arr[i] in temp)) {
// temp[arr[i]] = arr[i]
// res[j++] = arr[i]
// }
// for (var j = 0; j < res.length; j++) {
// if (arr[i] === res[j]) {
// break
// }
// }
// if (j === res.length) {
// res.push(arr[i])
// }
if (res.indexOf(arr[i]) === -1) {
res.push(arr[i])
}
}
return res
}

function unique2(arr) {
var res = [], len = arr.length
var sortedArr = arr.concat().sort()
var seen
for (var i = 0; i < len; i++) {
if (!i || seen !== sortedArr[i]) {
res.push(sortedArr[i])
}
seen = sortedArr[i]
}
return res
}

function unique3(arr) {
var _set = {}, res = []
for (let i = 0; i < arr.length; i++) {
let key = typeof arr[i] + arr[i]
_set[key] || (_set[key] = true, res.push(arr[i]))
}
return res
}
console.log(unique3(arr))
4 changes: 2 additions & 2 deletions 专题系列/数组扁平化.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

var arr = [1, [2, [3, 4]]];

function xx(arr) {
function flat(arr) {
return arr.reduce((pre,next) => {
return pre.concat(Array.isArray(next)? xx(next) : next)
},[])
}

console.log(xx(arr))
console.log(falt(arr))
</script>
</body>
</html>
3 changes: 2 additions & 1 deletion 数组一些常用的方法实现.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ Array.prototype.find = function (cb, context) {
let res = cb.call(context, arr[i], i, arr)
if(res) return arr[i]
}
}
}

0 comments on commit d1188f1

Please sign in to comment.