<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: map code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 22:39:35 GMT</pubDate>
    <description>DZone Snippets: map code</description>
    <item>
      <title>mapi: similar to standard map in Scheme, but function has index of elements as second parameter.</title>
      <link>http://snippets.dzone.com/posts/show/5368</link>
      <description>&lt;br /&gt;&lt;code&gt;&lt;br /&gt;;;;  mapi: similar to standard map in Scheme, but function has index of elements as parameter.&lt;br /&gt;;;;&lt;br /&gt;(define (mapi p l)&lt;br /&gt;  (let loop ((l l) (i 0) (r '()))&lt;br /&gt;    (if (pair? l)&lt;br /&gt;	(loop (cdr l) (+ i 1) (cons (p (car l) i) r))&lt;br /&gt;	(reverse r))))&lt;br /&gt;&lt;br /&gt;;; Example: converting a binary number given as a list of binary digits. &lt;br /&gt;;; (apply + (mapi (lambda (x i) (* x (expt 2 i))) '(0 1 1)))&lt;br /&gt;;; =&gt; 6&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 Apr 2008 22:03:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5368</guid>
      <author>sumimus (Sumimus L)</author>
    </item>
    <item>
      <title>Printing out a Map in sorted order</title>
      <link>http://snippets.dzone.com/posts/show/5283</link>
      <description>// print out a Map in sorted order of its keys&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    public static String dumpToString(Map&lt;String, Object&gt; map) {&lt;br /&gt;        if (map == null) {&lt;br /&gt;            return Constants.EMPTY_STRING;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        StringBuffer strbuf = new StringBuffer();&lt;br /&gt;&lt;br /&gt;        SortedMap&lt;String, Object&gt; sMap = new TreeMap&lt;String, Object&gt;(map);&lt;br /&gt;        Set&lt;Map.Entry&lt;String, Object&gt;&gt; s = sMap.entrySet();&lt;br /&gt;&lt;br /&gt;        for (Map.Entry&lt;String, Object&gt; elem : s) {&lt;br /&gt;            String key = elem.getKey();&lt;br /&gt;            Object value = elem.getValue();&lt;br /&gt;            strbuf.append(key);&lt;br /&gt;            strbuf.append("=");&lt;br /&gt;            strbuf.append(value == null ? Constants.EMPTY_STRING : value);&lt;br /&gt;            strbuf.append("\r\n");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        return strbuf.toString();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 27 Mar 2008 09:09:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5283</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>R3 compatible MAP</title>
      <link>http://snippets.dzone.com/posts/show/5186</link>
      <description>; Not terrribly efficient, but it works.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    ; R3-compatible interface&lt;br /&gt;    map: func ['word data [block!] body [block!]] [&lt;br /&gt;        collect/only res compose/deep [&lt;br /&gt;            repeat (word) data [res: do bind/copy body (to lit-word! word)]&lt;br /&gt;        ]&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 29 Feb 2008 05:36:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5186</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>In-place map_with_index</title>
      <link>http://snippets.dzone.com/posts/show/5119</link>
      <description>// This code is similar to Enumerable#map, but like each_with_index, it also yields the index of the current element.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;    def map_with_index!&lt;br /&gt;       each_with_index do |e, idx| self[idx] = yield(e, idx); end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def map_with_index(&amp;block)&lt;br /&gt;        dup.map_with_index!(&amp;block)&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Feb 2008 10:29:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5119</guid>
      <author>vinterbleg (Simon Ask Ulsnes)</author>
    </item>
    <item>
      <title>Recursively dump imbricated Map of Maps</title>
      <link>http://snippets.dzone.com/posts/show/4531</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    public void dumpMapOfMap(Map map) {&lt;br /&gt;        Set s = map.entrySet();&lt;br /&gt;        Iterator sit = s.iterator();&lt;br /&gt;        boolean isFirst = true;&lt;br /&gt;&lt;br /&gt;        while (sit.hasNext()) {&lt;br /&gt;            Map.Entry elem = (Map.Entry)sit.next();&lt;br /&gt;            String key = (String)elem.getKey();&lt;br /&gt;            Object value = elem.getValue();&lt;br /&gt;&lt;br /&gt;            if (value instanceof String) {&lt;br /&gt;                // recursivity stop condition&lt;br /&gt;                System.out.print(key);&lt;br /&gt;                System.out.print(" : ");&lt;br /&gt;                System.out.println(value);&lt;br /&gt;            } else {&lt;br /&gt;                if (!isFirst) {&lt;br /&gt;                    System.out.println("");&lt;br /&gt;                } else {&lt;br /&gt;                    isFirst = false;&lt;br /&gt;                }&lt;br /&gt;                System.out.println(key);&lt;br /&gt;                Map valueMap = (Map)elem.getValue();&lt;br /&gt;                dumpMapOfMap(valueMap);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Sep 2007 12:58:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4531</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Download  coordinate from maproom</title>
      <link>http://snippets.dzone.com/posts/show/4080</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;IFS="&lt;br /&gt;"&lt;br /&gt;for C in Africa Antarctica Asia Australasia Europe North_America South_America&lt;br /&gt;do&lt;br /&gt;	mkdir -p GIS/$C&lt;br /&gt;	wget -O jeter.html -q "http://www.maproom.psu.edu/cgi-bin/dcw/dcwarea.cgi?$C"&lt;br /&gt;	for P in `egrep '^&lt;OPTION' jeter.html | cut -d '&gt;' -f2`&lt;br /&gt;	do&lt;br /&gt;		curl -d "area=$C" -d "country=$P" -o jeter2.html "http://www.maproom.psu.edu/cgi-bin/dcw/dcwcountry0.cgi"&lt;br /&gt;		P2=`grep FORM jeter2.html | grep point10 | tr "&lt;&gt;" "\n\n" | grep count| cut -d ' ' -f4 | cut -d '=' -f2`&lt;br /&gt;		#curl -d "country=$P2" -o jeter3.txt "http://www.maproom.psu.edu/cgi-bin/dcw/point10.cgi"&lt;br /&gt;		wget -O jeter2.html "http://www.maproom.psu.edu/cgi-bin/dcw/point10.cgi?country=$P2"&lt;br /&gt;		P3=`cat  jeter2.html | tr " &gt;" "\n\n" | grep ftp`&lt;br /&gt;		echo "##### $P2 $P3"	&lt;br /&gt;		wget -O jeter3.txt "$P3"&lt;br /&gt;		mv jeter3.txt &gt; GIS/$C/${P2}.js&lt;br /&gt;		sleep 3&lt;br /&gt;	done&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 31 May 2007 07:29:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4080</guid>
      <author>lindenb (Pierre)</author>
    </item>
    <item>
      <title>A sortable, ordered Map</title>
      <link>http://snippets.dzone.com/posts/show/2634</link>
      <description>This is a neat collection that allows its elements to be sorted arbitary by moving them up and down in the List or put them to a given index. Nice to display lists that can be edited (i.e. reordered) by the user.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.Collection;&lt;br /&gt;import java.util.Iterator;&lt;br /&gt;import java.util.List;&lt;br /&gt;&lt;br /&gt;import org.apache.commons.collections.map.ListOrderedMap;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @author janhoo&lt;br /&gt; */&lt;br /&gt;public class SortableOrderedMap extends ListOrderedMap {&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Inserts a given Key/Value-Pair on a certain Position&lt;br /&gt;   * alle following Pairs are moved up accordingly &lt;br /&gt;   * &lt;br /&gt;   * @param newKey&lt;br /&gt;   * @param newValue&lt;br /&gt;   * @param pos&lt;br /&gt;   * @return&lt;br /&gt;   */&lt;br /&gt;  public Object putOnPosition(Object newKey, Object newValue, int pos) {&lt;br /&gt;    Object res = put(newKey, newValue);&lt;br /&gt;    while (indexOf(newKey) &gt; pos) {&lt;br /&gt;      moveUp(newKey);&lt;br /&gt;    }&lt;br /&gt;    return res;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * Inserts a given Key/Value Pair before position of a given Key&lt;br /&gt;   * &lt;br /&gt;   * @param newKey&lt;br /&gt;   * @param newValue&lt;br /&gt;   * @param oldKey&lt;br /&gt;   * @return&lt;br /&gt;   */&lt;br /&gt;  public Object putBefore(Object newKey, Object newValue, Object oldKey) {&lt;br /&gt;    if (!containsKey(oldKey)) {&lt;br /&gt;      return put(newKey, newValue);&lt;br /&gt;    }&lt;br /&gt;    return putOnPosition(newKey, newValue, indexOf(oldKey));&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * &lt;br /&gt;   * inserts a given key/value pair after the position of a given key&lt;br /&gt;   * &lt;br /&gt;   * @param newKey&lt;br /&gt;   * @param newValue&lt;br /&gt;   * @param oldKey&lt;br /&gt;   * @return&lt;br /&gt;   */&lt;br /&gt;  public Object putAfter(Object newKey, Object newValue, Object oldKey) {&lt;br /&gt;    if (!containsKey(oldKey)) {&lt;br /&gt;      return put(newKey, newValue);&lt;br /&gt;    }&lt;br /&gt;    return putOnPosition(newKey, newValue, indexOf(oldKey)+1);&lt;br /&gt;  }&lt;br /&gt;	&lt;br /&gt;  /**&lt;br /&gt;   * moves item with given key up by one step. Does nothing&lt;br /&gt;   * if the item is already on top or not existend.&lt;br /&gt;   * &lt;br /&gt;   * @param the key of the Item to be moved&lt;br /&gt;   */&lt;br /&gt;  public synchronized void moveUp(Object key) {&lt;br /&gt;&lt;br /&gt;    // return if already on top or key not in map	&lt;br /&gt;    if (key.equals(firstKey()) || !containsKey(key)) {&lt;br /&gt;      return;&lt;br /&gt;    }&lt;br /&gt;    int pcur = indexOf(key);&lt;br /&gt;&lt;br /&gt;    insertOrder.set(pcur, previousKey(key));&lt;br /&gt;    insertOrder.set(pcur - 1, key);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * moves item with given key down by one step. Does nothing&lt;br /&gt;   * if the item is already on bottom or not existend.&lt;br /&gt;   * &lt;br /&gt;   * @param the key of the Item to be moved&lt;br /&gt;   */&lt;br /&gt;  public synchronized void moveDown(Object key) {&lt;br /&gt;    if (key.equals(lastKey()) || !containsKey(key)) {&lt;br /&gt;      return;&lt;br /&gt;    }&lt;br /&gt;    int pcur = indexOf(key);&lt;br /&gt;    insertOrder.set(pcur, nextKey(key));&lt;br /&gt;    insertOrder.set(pcur + 1, key);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  /* (non-Javadoc)&lt;br /&gt;   * @see org.apache.commons.collections.map.ListOrderedMap#asList()&lt;br /&gt;   */&lt;br /&gt;  public List asList() {&lt;br /&gt;    ArrayList v = new ArrayList();&lt;br /&gt;    for (Iterator it = values().iterator(); it.hasNext();) {&lt;br /&gt;      v.add(it.next());&lt;br /&gt;    }&lt;br /&gt;    return v;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;	&lt;br /&gt;  /* (non-Javadoc)&lt;br /&gt;   * @see java.lang.Object#toString()&lt;br /&gt;   */&lt;br /&gt;  public String toString() {&lt;br /&gt;    StringBuffer b = new StringBuffer();&lt;br /&gt;    for (Iterator it = values().iterator(); it.hasNext();) {&lt;br /&gt;      b.append(it.next().toString() + "\n");&lt;br /&gt;    }  &lt;br /&gt;    return b.toString();&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Attention: this requires the jakarta commons collections</description>
      <pubDate>Thu, 21 Sep 2006 20:01:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2634</guid>
      <author>janhoo (Jan Galinski)</author>
    </item>
    <item>
      <title>map function</title>
      <link>http://snippets.dzone.com/posts/show/1102</link>
      <description>&lt;code&gt;&lt;br /&gt;; Larry Palmiter&lt;br /&gt;map: func [fn blk args /local result][&lt;br /&gt;	result: copy []&lt;br /&gt;	repeat el blk [append/only result fn :el args]&lt;br /&gt;	return result&lt;br /&gt;]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 05:46:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1102</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>Lua: Lazy Map Implementation: Round One</title>
      <link>http://snippets.dzone.com/posts/show/942</link>
      <description>Concept code for a lazy map implementation in Lua&lt;br /&gt;&lt;br /&gt;&lt;code&gt;require "std.table" -- For memoize&lt;br /&gt;&lt;br /&gt;function map(t, f)&lt;br /&gt;    local nt = table.memoize(function (k) return f(t[k]) end)&lt;br /&gt;    local mt = getmetatable(nt)&lt;br /&gt;    function mt:__pairs()&lt;br /&gt;        local k&lt;br /&gt;        local function mynext()&lt;br /&gt;            k,v = next(t, k)&lt;br /&gt;            if k == nil then return nil end&lt;br /&gt;            return k,nt[k]&lt;br /&gt;        end&lt;br /&gt;        return mynext, self&lt;br /&gt;    end&lt;br /&gt;    return nt&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example Usage:&lt;br /&gt;&lt;code&gt;require "std.base" -- For better table tostring()&lt;br /&gt;&lt;br /&gt;local mylist = {1,3,5,7,9}&lt;br /&gt;local newlist = map(mylist, function(v) return 3 * v - 2 end)&lt;br /&gt;assert(mylist[3] == 5)&lt;br /&gt;assert(newlist[3] == 13)&lt;br /&gt;&lt;br /&gt;require "std.base" -- For the pairs() that obeys the __pairs metamethod&lt;br /&gt;print("old:", mylist)&lt;br /&gt;print("new(3*v-2):", newlist)&lt;/code&gt;</description>
      <pubDate>Mon, 05 Dec 2005 16:13:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/942</guid>
      <author>kergoth (Chris Larson)</author>
    </item>
    <item>
      <title>Google map pin</title>
      <link>http://snippets.dzone.com/posts/show/778</link>
      <description>&lt;code&gt;&lt;br /&gt;def pin(x,y,char=None):&lt;br /&gt;    # top-left is x-5, y-17&lt;br /&gt;    points = [ (0,0), (-2,-6), (-5,-10), (-5,-14), (-2,-17), \&lt;br /&gt;                      (2,-17), (5,-14), (5,-10), (2,-6)]&lt;br /&gt;    c.polygon([(x+dx,y+dy) for dx,dy in points], 0, (255,119,107), 1)&lt;br /&gt;    if char:&lt;br /&gt;        c.text((x-2, y-7), unicode(char))&lt;br /&gt;    else:&lt;br /&gt;        c.point((x,y-12), 0, width=5)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Instead of adding a pin image to the program, this function&lt;br /&gt;will draw a google map pin at point (x,y) instead.&lt;br /&gt;If you give it a char, it will put that char in the middle of &lt;br /&gt;the pin as well&lt;br /&gt;&lt;code&gt;&lt;br /&gt;pin(20,20,'A') # A in the pin&lt;br /&gt;pin(40,40)  # just bull-eye&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 01 Oct 2005 19:11:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/778</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
