Last active
October 25, 2015 08:08
-
-
Save tonyjunkes/166a5fdc2fc84da0542d to your computer and use it in GitHub Desktop.
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
<cfscript> | |
array function nextIds(required array ids, required numeric id, numeric count = 0) { | |
var idPos = arguments.ids.find(arguments.id); | |
return arguments.ids.filter(function(item, index) { | |
return idPos && index != idPos && index <= idPos + count; | |
}); | |
} | |
writeDump( nextIds([7141, 6881, 5821, 8001, 7904, 6601, 7961, 6021, 4721], 7141, 3) ); | |
writeDump( nextIds([7141,6881,5821], 7141, 3) ); | |
writeDump( nextIds([7141], 7141, 3) ); | |
writeDump( nextIds([1111], 9999, 3) ); | |
</cfscript> |
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
(defn next-ids | |
[ids id count] | |
(let [end-index (+ (.indexOf ids id) count)] | |
(if (.contains ids id) | |
(filterv (apply every-pred | |
[#(not= % id) #(<= (.indexOf ids %) end-index)]) ids) []))) | |
(pprint (next-ids [7141 6881 5821 8001 7904 6601 7961 6021 4721] 7141 3)) | |
(pprint (next-ids [7141 6881 5821] 7141 3)) | |
(pprint (next-ids [7141] 7141 3)) | |
(pprint (next-ids [1111] 9999 3)) |
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
List nextIds(List ids, int id, int count) { | |
int endIndex = ids.indexOf(id) + count | |
ids.findAll { id in ids && it != id && ids.indexOf(it) <= endIndex } | |
} | |
println nextIds([7141, 6881, 5821, 8001, 7904, 6601, 7961, 6021, 4721], 7141, 3).toString() | |
println nextIds([7141, 6881, 5821], 7141, 3).toString() | |
println nextIds([7141], 7141, 3).toString() | |
println nextIds([1111], 9999, 3).toString() |
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
def next_ids(ids, id, count) | |
end_index = ids.index(id).nil? || ids.index(id) + count | |
return ids.select { |item| (ids.include? id) && item != id && ids.index(item) <= end_index } | |
end | |
puts next_ids([7141, 6881, 5821, 8001, 7904, 6601, 7961, 6021, 4721], 7141, 3).inspect | |
puts next_ids([7141, 6881, 5821], 7141, 3).inspect | |
puts next_ids([7141], 7141, 3).inspect | |
puts next_ids([1111], 9999, 3).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the CFML to use index in filter() and tweaked the Groovy to be a bit simpler.
Tweaked the Clojure a bit. I have this feeling that the conditional is not necessary but I cannot get accurate results with what I've tried so far ;)
Added a Ruby example.