Skip to content

Instantly share code, notes, and snippets.

@tonyjunkes
Last active October 25, 2015 08:08
Show Gist options
  • Save tonyjunkes/166a5fdc2fc84da0542d to your computer and use it in GitHub Desktop.
Save tonyjunkes/166a5fdc2fc84da0542d to your computer and use it in GitHub Desktop.
<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>
(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))
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()
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
@tonyjunkes
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment