A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label map. Show all posts
Showing posts with label map. Show all posts

Vim mappings for commands like JSON pretty-print and perltidy

imap :!python -m json.tool
map :%!perltidy

?
nmap :%!python -m json.tool

# AB
nmap :%!python -m json.tool
map :!perl -cW %
map :%!perltidy
map :resize +10

Ruby iterator cheat sheet

A list of many useful iterators in Ruby for hashes and arrays:
  • each - Do block for each item. Works on hash or array. Can use 'break'. Return nothing.
    • each_index - Same as each but pass in the index to the block
  • collect (aka map) - Do block for each item. Return array. Same as Perl map.
    • collect! / map! - Same as map, but modify array in place
  • select (aka find_all) - Do block for each item. Return array if block is true. Same as Perl grep.
    • select! (aka keep_if) - Same as select but modify array in place
    • reject - Opposite of select. Return if block is false.
    • reject! (aka delete_if) - Same as reject but modify array in place
  • inject(xyzzy) (aka reduce) - Do block for each pair of items, starting with xyzzy & first item.
  • compact - Return array with nil items removed
    • compact! - Same as collect but modify array in place
  • delete(foo) - Remove all items that are equal to foo
  • clear - Remove all items
  • empty? - Return true if array has no items
  • count - Return number of items
  • length (aka size) - Return number of items
  • flatten! - Modify array to be 1-dimensional
  • shuffle! - Shuffle in place
  • sort! - Sort in place
  • bsearch - Find item using binary search
  • combination(3) - Return all combainations of length 3
  • permutation(4) - Return all permutations of length 4
  • sort_by! - Sort in place using keys in block
  • transpose - swap rows and columns
  • zip - step through two arrays at the same time


(source, source, source)

Automatically generated diagrams of Perl code


  • Use UML::Sequence to diagram your method calls - Looks cool but couldn't get the module's tests to pass.
  • Write your own code to produce a Graphviz spec file - Good choice if you want a data structure diagrammed, e.g. State transitions.
  • SchemaSpy works well for database tables.
Untried:
  • UML::Class::Simple - class diagrams, not methods?
  • Devel::Diagram - class diagrams, not methods?
  • Devel::DProfPP

Perl map command

%hash = map { get_a_key_for($_) => $_ } @array;

http://perldoc.perl.org/functions/map.html