Last active
December 14, 2015 08:09
-
-
Save obenjiro/5055551 to your computer and use it in GitHub Desktop.
Simple version of .Net Linq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = [ 1, 2, 3, 2 ]; | |
console.log(PseudoLinq(a).select(function(i){ | |
return i + 1 | |
}).toArray()); | |
console.log(PseudoLinq(a).where(function(i){ | |
return i > 1 | |
}).select(function(i){ | |
return i - 1 | |
}).toArray()); | |
function PseudoLinq(data){ | |
if (Array.isArray(data)) { | |
data = data.slice(0); | |
} else { | |
throw new Error("not implemented yet"); | |
} | |
//private | |
var _convertHashToArray = function(hash) { | |
throw new Error("not implemented yet"); | |
} | |
return { | |
select: function(lambda){ | |
if (Array.isArray(data)) { | |
for (var i = 0; i < data.length; i++) { | |
data[i] = lambda(data[i]); | |
} | |
} else { | |
temp_data = []; | |
for (var i in data) { | |
temp_data[i] = lambda({ | |
key: i, | |
value: data[i] | |
}) | |
} | |
data = temp_data; | |
} | |
return this; | |
}, | |
where: function(lambda){ | |
if (Array.isArray(data)) { | |
var result_data = []; | |
for (var i = 0; i < data.length; i++) { | |
if (lambda(data[i]) === true) { | |
result_data.push(data[i]); | |
} | |
} | |
data = result_data; | |
} else { | |
throw new Error("not implemented yet"); | |
} | |
return this; | |
}, | |
toArray: function(){ | |
if (Array.isArray(data)) { | |
return data; | |
} else { | |
return _convertHashToArray(data); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment