Reply to message «Re: for statement with character classes», 
sent 16:51:40 14 June 2011, Tuesday
by David Fishburn:

> I never remember the map() function, but I still wouldn't have come up
> with the char2nr stuff.
I widely use it instead of `for' construct: it is faster (both perfomance and 
writing speed). I usually write
    call map(dict, 's:F.do.something.with.item(v:val)')
instead of `for' if I can (that means, if s:F.do.something.with.item is called 
not only there so there is a reason to make a function for it). (Use copy(), 
keys() or values() if you don't want to alter dict).

Some other tricks:
    let dict={'a': 'b', 'c': 'd'}
    let revdict={}
    call map(copy(dict), 'extend(revdict, {v:val : v:key})')
: vimscript implemenation of perl `reverse %hash'.

    for value in list
        if has_key(dict, value)
            " Do something
        endif
    endfor
can be replaced with
    for value in filter(copy(list), 'has_key(dict, v:val)')
        " Do something
    endfor
. It is not map, but also came from functional programming (though you have to 
remember that both map() and filter() modify their first argument).

I use both these functions very widely: map is 6th most used function (285) in 
my scripts and filter is 16th (112). Counted with

    for fun in $(< if.dat) ; do
        echo $fun $(grep -r -o -P '\b'$fun'\(' ~/.vim/dev/*/(plugin|autoload) \
                    | wc -l)
    done | sort -r -k 2 -n > fusage.dat
(where if.dat contains a list of functions separated by newlines. Requires zsh 
(probably with `setopt extendedglob'), grep with pcre support).

Five leaders: type (1071), has_key (594), add (538), len (380), empty (288).

Strange, but for /usr/share/vim73 map() function is only 26th and leaders are 
very different: exists (3219), getline (792), substitute (661), has (607), 
append (584). Looks like the cause is absence of lists and dictinaries in 
earlier vim's: next (after getline) function that works (accepts or returns) 
with list or dictionaries is on the 15th place.

Original message:
> On 6/13/2011 11:43 PM, ZyX wrote:
> > Reply to message «for statement with character classes»,
> > sent 05:52:36 14 June 2011, Tuesday
> > by David Fishburn:
> > 
> > No, there are no such shortcuts. Nearly equivalent:
> >      for letter in map(range(char2nr('0'), char2nr('9'))+
> >      
> >                       \range(char2nr('a'), char2nr('z'))+
> >                       \range(char2nr('A'), char2nr('Z')),
> >                       'nr2char(v:val)')
> >          
> >          ...
> >      
> >      endfor
> 
> I would say that is a pretty good short cut and exactly what I needed.
> 
> > Why do you need this?
> 
> For the YankRing plugin
> (http://www.vim.org/scripts/script.php?script_id=1234) I want to
> optionally display all the numbered and lettered registers in the window
> and allow the user to choose from those as well.  The FOR statement
> allows me to easily iterate through the registers and display them.
> 
> Something like this:
> 
>      for letter in map(range(char2nr('0'), char2nr('9'))+
>                       \range(char2nr('A'), char2nr('Z')), 'nr2char(v:val)')
>          YRDisplayElem(letter, getreg(letter) )
>      endfor
> 
> I never remember the map() function, but I still wouldn't have come up
> with the char2nr stuff.
> 
> Much appreciated.
> 
> Dave

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to