On Sun, Jun 28, 2015 at 01:22:18PM -0700, adam willis wrote:
it has everything to do with the language being imperative, he is asking
for declarative functionality from an imperative language. He has to build
up the functionality himself. this has nothing to do with generics.
I don't think 'declarative' means what you think it means. First of all, it
has everything to do with generics, since this is trivial to do in
a language with generics. E.g. C++ (didn't compile, could contain errors):
---
template <typename T>
std::vector<T> removeIndices(std::set<int> const &indices,
std::vector<T> v)
{
for (auto iter = indices.rbegin(); iter != indices.rend(); ++iter)
vec.erase(vec.begin() + *iter);
return v;
}
---
Generics make this simple, because:
- You can easily define generic data types that have the necessary
constraints (e.g. ordered sets).
- You define the function once, and it works for vectors of every type.
As for declarative programming - declarative programming simply means that
decribes that logic, and not the control flow. E.g. I just wrote up the
Prolog (the defacto declarative language) solution (which is actually much
more work):
---
remove(Indices,List,RList) :-
remove(Indices,List,RList,0).
remove([IndexHead|IndexTail],[_|Tail],RTail,IndexHead) :-
Counter is IndexHead + 1,
remove(IndexTail,Tail,RTail,Counter).
remove(Indices,[Head|Tail],[Head|RTail],Counter) :-
Counter1 is Counter + 1,
remove(Indices,Tail,RTail,Counter1).
remove([],[],[],_).
---
Rather than having an explicit control structure, it defines the cases where
the list head is retained/removed. As a result, you can use the same
predicate in a couple of ways:
---
?- remove([1,3],[a,b,c,d,e,f],R).
R = [a, c, e, f] .
?- remove(Idx,[a,b,c,d,e,f],[a,c,e,f]).
Idx = [1, 3] .
?- remove([1,3],L,[a,c,e,f]).
L = [a, _G1177, c, _G1183, e, f] .
---
tl;dr: the imperative type-generic solution is actually much shorter than
the declarative approach (though, you can probably come up with something
shorter using nth0).
Take care,
Daniël de Kok
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.