> But really Common-Lispers would write the same functionality with this:
>
> (reduce #'+ '(1 2 3 4))
So cl with its vast standard library provides a function for that.
That's cool of course but nothing stops you from implementing such a
function in vimscript, perl or whatever.
function! Reduce(ffn, list) "{{{3
if empty(a:list)
return ''
else
let list = copy(a:list)
let s:acc = remove(list, 0)
let ffn = substitute(a:ffn, '\<v:acc\>', "s:acc", 'g')
for val in list
let s:acc = eval(substitute(ffn, '\<v:val\>', val, 'g'))
endfor
return s:acc
endif
endf
echom Reduce("v:val + v:acc", [1, 2, 3, 4])
echom Reduce("v:val > v:acc ? v:val : v:acc", [1, 2, 3, 4])
echom Reduce("'v:val' < v:acc ? 'v:val' : v:acc", split("characters",
'\zs'))
Now let's try to use dictionnaries for prototype-based programming and
I ask you to do something similar in say Scheme without any extra
libraries (using cl would be unfair):
let s:prototype = {'r': 0.0, 'i': 0.0}
function! C(r, i)
let c = copy(s:prototype)
let c.r = a:r
let c.i = a:i
return c
endf
function! s:prototype.Add(other) dict
let self.r += a:other.r
let self.i += a:other.i
return self
endf
function! s:prototype.ToString() dict
return printf("<%0.2f,%0.2f>", self.r, self.i)
endf
echom C(1.0, 2.0).Add(C(3.2, 4.3)).ToString()
--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php