AnnouncementsMatrixEventsFunnyVideosMusicAncapsTechnologyEconomicsPrivacyGIFSCringeAnarchyFilmPicsThemesIdeas4MatrixAskMatrixHelpTop Subs
1

This makes writing variations of text very easy. This is particularly useful in Vim but could be used on the command line as well.

I'll show you kind of a fake example. If we tried to type out each variation manually this would be hard.

Hike
Jog
Bike

Great Falls
Riverbend
Difficult Run
Scott's Run
Rock Creek
Claude Moore
Hunter Valley
Lake Fairfax
Seneca
Bles Park
Ball's Bluff
Bear's Den
Yellow Stone
the Tidal Basin
Mananas

next

morning
evening

for

20m
1h

This would be hell to write out every variation. In Vim we have at least some out of the box help because it's pretty easy to copy and paste many of the same line and quick to edit a single word. But it still feels clunky and it gets much worse once you have two variable sections.

But what if we could just highlight it all with shift-V, then press :!mult and be done? You can. Because of some auto-fill in the command will end up looking like :'<,'>!mult but that ugly bit Vim types for you after highlighting text.

So here is the code, in Nim

import strutils

proc readBlocks(): seq[seq[string]] =
 var groups: seq[seq[string]] = @[]
 var current: seq[string] = @[]
 for line in stdin.lines:
  let s = line.strip
  if s.len == 0:
   if current.len > 0:
    groups.add(current)
    current = @[]
  else:
   current.add(s)
 if current.len > 0:
  groups.add(current)
 groups

proc combineTwoGroups(a, b: seq[string]): seq[string] =
 var results: seq[string] = @[]
 for x in a:
  for y in b:
   results.add(x & " " & y)
 results

proc combineAllGroups(groups: seq[seq[string]]): seq[string] =
 if groups.len == 0:
  return @[]
 var acc = groups[0]
 for i in 1 ..< groups.len:
  acc = combineTwoGroups(acc, groups[i])
 acc

when isMainModule:
 for s in combineAllGroups(readBlocks()):
  echo s

Compile that with:

nim c -d:release -o:mult mult.nim

I just keep that in my ~/bin but you can leave it anywhere you like. If you don't have nim it has a one line install listed here: https://nim-lang.org/install_unix.html

I had additional things I would have gladly covered but I want to keep this post down to only one code segment. For a more extended and ridiculous version of this continue here: https://goatmatrix.net/c/Programming/3ZKfQRJAhS

Comment preview