Home Explore Blog CI



neovim

18th chunk of `runtime/doc/usr_41.txt`
6f706c476d2b0cc21d07217d5fda420c92f1c4dcf5c0ae3e0000000100000fa2
 ordered.  You can sort the list to get a
specific order: >

	:for key in sort(keys(uk2nl))
	:  echo key
	:endfor
<	one ~
	three ~
	two ~

But you can never get back the order in which items are defined.  For that you
need to use a List, it stores items in an ordered sequence.


DICTIONARY FUNCTIONS

The items in a Dictionary can normally be obtained with an index in square
brackets: >

	:echo uk2nl['one']
<	een ~

A method that does the same, but without so many punctuation characters: >

	:echo uk2nl.one
<	een ~

This only works for a key that is made of ASCII letters, digits and the
underscore.  You can also assign a new value this way: >

	:let uk2nl.four = 'vier'
	:echo uk2nl
<	{'three': 'drie', 'four': 'vier', 'one': 'een', 'two': 'twee'} ~

And now for something special: you can directly define a function and store a
reference to it in the dictionary: >

	:function uk2nl.translate(line) dict
	:  return join(map(split(a:line), 'get(self, v:val, "???")'))
	:endfunction

Let's first try it out: >

	:echo uk2nl.translate('three two five one')
<	drie twee ??? een ~

The first special thing you notice is the "dict" at the end of the ":function"
line.  This marks the function as being used from a Dictionary.  The "self"
local variable will then refer to that Dictionary.
   Now let's break up the complicated return command: >

	split(a:line)

The split() function takes a string, chops it into whitespace separated words
and returns a list with these words.  Thus in the example it returns: >

	:echo split('three two five one')
<	['three', 'two', 'five', 'one'] ~

This list is the first argument to the map() function.  This will go through
the list, evaluating its second argument with "v:val" set to the value of each
item.  This is a shortcut to using a for loop.  This command: >

	:let alist = map(split(a:line), 'get(self, v:val, "???")')

Is equivalent to: >

	:let alist = split(a:line)
	:for idx in range(len(alist))
	:  let alist[idx] = get(self, alist[idx], "???")
	:endfor

The get() function checks if a key is present in a Dictionary.  If it is, then
the value is retrieved.  If it isn't, then the default value is returned, in
the example it's '???'.  This is a convenient way to handle situations where a
key may not be present and you don't want an error message.

The join() function does the opposite of split(): it joins together a list of
words, putting a space in between.
  This combination of split(), map() and join() is a nice way to filter a line
of words in a very compact way.


OBJECT ORIENTED PROGRAMMING

Now that you can put both values and functions in a Dictionary, you can
actually use a Dictionary like an object.
   Above we used a Dictionary for translating Dutch to English.  We might want
to do the same for other languages.  Let's first make an object (aka
Dictionary) that has the translate function, but no words to translate: >

	:let transdict = {}
	:function transdict.translate(line) dict
	:  return join(map(split(a:line), 'get(self.words, v:val, "???")'))
	:endfunction

It's slightly different from the function above, using 'self.words' to lookup
word translations.  But we don't have a self.words.  Thus you could call this
an abstract class.

Now we can instantiate a Dutch translation object: >

	:let uk2nl = copy(transdict)
	:let uk2nl.words = {'one': 'een', 'two': 'twee', 'three': 'drie'}
	:echo uk2nl.translate('three one')
<	drie een ~

And a German translator: >

	:let uk2de = copy(transdict)
	:let uk2de.words = {'one': 'eins', 'two': 'zwei', 'three': 'drei'}
	:echo uk2de.translate('three one')
<	drei eins ~

You see that the copy() function is used to make a copy of the "transdict"
Dictionary and then the copy is changed to add the words.  The original
remains the same, of course.

Now you can go one step further, and use your preferred translator: >

	:if $LANG =~ "de"
	:  let trans = uk2de
	:else
	:  let trans = uk2nl
	:endif
	:echo trans.translate('one two three')
<	een twee drie ~

Here "trans" refers

Title: Dictionary Functions, Object-Oriented Programming in Vim
Summary
This section delves into advanced uses of Dictionaries in Vim, detailing how to access and modify items, as well as how to define and store functions within them. It explains the use of the 'dict' keyword for Dictionary-based functions and the 'self' variable. It also covers the split(), map(), get(), and join() functions for efficient text manipulation. Furthermore, it introduces object-oriented programming concepts in Vim using Dictionaries as objects, demonstrating how to create 'abstract classes' with shared functions and instantiate language-specific translators using the copy() function.