On 18 February 2010 12:53, Tom Link <[email protected]> wrote:
> > But really Common-Lispers would write the same functionality with this:
> >
> > (reduce #'+ '(1 2 3 4))
Well that's just (+ 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'))
Don't do this with string processing! Much better with funcrefs.
fun Reduce(funcname, list)
let F = function(a:funcname)
let acc = a:list[0]
for value in a:list[1:]
let acc = F(acc, value)
endfor
return acc
endfun
fun Add(a,b)
return a:a + a:b
endfun
fun Max(a,b)
return a:a > a:b ? a:a : a:b
endfun
fun Min(a,b)
return a:a < a:b ? a:a : a:b
endfun
let list = [1,2,3,4,5]
echo Reduce('Add', list)
echo Reduce('Max', list)
echo Reduce('Min', list)
--Antony
--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php