<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: functional code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 21:38:08 GMT</pubDate>
    <description>DZone Snippets: functional code</description>
    <item>
      <title>Functional JarClassLoader - warning, really awful</title>
      <link>http://snippets.dzone.com/posts/show/3527</link>
      <description>Some really awful functional style Java...&lt;br /&gt;&lt;br /&gt;Note that this depends on a bunch of other classes in the playground namespace. If you actually want to use it ask me and I'll send you the source - I didn't really feel like snippetting all the needed code. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package playground.library.classloader;&lt;br /&gt;&lt;br /&gt;import java.io.ByteArrayOutputStream;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.InputStream;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.HashSet;&lt;br /&gt;import java.util.Iterator;&lt;br /&gt;import java.util.List;&lt;br /&gt;import java.util.Set;&lt;br /&gt;import java.util.jar.JarEntry;&lt;br /&gt;import java.util.jar.JarFile;&lt;br /&gt;import playground.library.functional.FunctionalUtils;&lt;br /&gt;import playground.library.functional.Pair;&lt;br /&gt;import playground.library.functional.iterator.FilterIterator;&lt;br /&gt;import playground.library.functional.iterator.IteratorTransformer;&lt;br /&gt;import playground.library.functional.Predicate;&lt;br /&gt;import playground.library.functional.Transformer;&lt;br /&gt;import playground.library.functional.iterator.DelegatingIterator;&lt;br /&gt;import playground.library.functional.iterator.LinkingIterator;&lt;br /&gt;import playground.library.jar.JarEntryIterator;&lt;br /&gt;import playground.library.utils.IteratorUtils;&lt;br /&gt;import playground.library.utils.IOUtils;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * ClassLoader for loading from Jar files.&lt;br /&gt; *&lt;br /&gt; * @author david&lt;br /&gt; */&lt;br /&gt;public class JarClassLoader extends ClassLoader&lt;br /&gt;{&lt;br /&gt;    public JarClassLoader(ClassLoader parent) { super(parent); }    &lt;br /&gt;    public JarClassLoader()                   { super(); }&lt;br /&gt;    &lt;br /&gt;    private final List&lt;JarFile&gt;     files = new ArrayList&lt;JarFile&gt;();&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Provides an iterator over all pairs of (file, entry) of JarFiles associated&lt;br /&gt;     * with this class loader and entries of those jar files.&lt;br /&gt;     */&lt;br /&gt;    private Iterator&lt;Pair&lt;JarFile, JarEntry&gt;&gt; getResources(){&lt;br /&gt;        return new LinkingIterator&lt;Pair&lt;JarFile, JarEntry&gt;&gt;(&lt;br /&gt;            new IteratorTransformer&lt;JarFile, Iterator&lt;Pair&lt;JarFile,JarEntry&gt;&gt;&gt;(&lt;br /&gt;                new Transformer&lt;JarFile, Iterator&lt;Pair&lt;JarFile, JarEntry&gt;&gt;&gt;(){&lt;br /&gt;                public Iterator&lt;Pair&lt;JarFile, JarEntry&gt;&gt; transform(JarFile file){&lt;br /&gt;                    return FunctionalUtils.merge(file, new JarEntryIterator(file));}})&lt;br /&gt;           .transform(JarClassLoader.this.files.iterator()));}&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Searches through the available jar files to try and find a class with the given name.&lt;br /&gt;     */&lt;br /&gt;    @Override&lt;br /&gt;    protected Class&lt;?&gt; findClass(String name) throws ClassNotFoundException{&lt;br /&gt;        final Iterator&lt;Pair&lt;JarFile,JarEntry&gt;&gt; resources = this.getResources();&lt;br /&gt;        while (resources.hasNext()){&lt;br /&gt;            final Pair&lt;JarFile, JarEntry&gt; resource = resources.next();&lt;br /&gt;                if(resource.second.getName().equals(name)){&lt;br /&gt;                    try { return this.load(resource.first.getInputStream(resource.second));}&lt;br /&gt;                    catch (IOException e) { e.printStackTrace(); }}}&lt;br /&gt;        throw new ClassNotFoundException();}&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Loads the contents of the InputStream as a class. This doesn't validate&lt;br /&gt;     * the contents, so will fail messily if you pass it something that isn't a&lt;br /&gt;     * valid class file.&lt;br /&gt;     */&lt;br /&gt;    private Class&lt;?&gt; load(final InputStream input){&lt;br /&gt;        try {&lt;br /&gt;            final ByteArrayOutputStream output = new ByteArrayOutputStream();&lt;br /&gt;            final byte[] bytes = IOUtils.copy(input, new ByteArrayOutputStream()).toByteArray();&lt;br /&gt;            return this.defineClass(null, bytes, 0, bytes.length); }&lt;br /&gt;        catch (IOException e){ throw new RuntimeException(e); }&lt;br /&gt;        finally{&lt;br /&gt;            try{ input.close(); }&lt;br /&gt;            catch (IOException e){ throw new RuntimeException(e);}}}&lt;br /&gt;        &lt;br /&gt;    /**&lt;br /&gt;     * Provides an iterator which lazily loads the contents of the jar file as&lt;br /&gt;     * classes. Will also register the jar file with the class loader's file list.&lt;br /&gt;     * When the iterator is exhausted the jar file will be removed from the file list.&lt;br /&gt;     */&lt;br /&gt;    public Iterator&lt;Class&lt;?&gt;&gt; load(final JarFile file) throws IOException{&lt;br /&gt;        this.files.add(file);&lt;br /&gt;                &lt;br /&gt;        return  &lt;br /&gt;            new DelegatingIterator&lt;Class&lt;?&gt;&gt;(&lt;br /&gt;                new IteratorTransformer&lt;JarEntry, Class&lt;?&gt;&gt;(&lt;br /&gt;                    new Transformer&lt;JarEntry, Class&lt;?&gt;&gt;(){&lt;br /&gt;                        public Class transform(JarEntry stream){&lt;br /&gt;                            InputStream fileStream = null;&lt;br /&gt;&lt;br /&gt;                            try{return JarClassLoader.this.load(fileStream = file.getInputStream(stream)); }&lt;br /&gt;                            catch(IOException e){throw new RuntimeException(e);}&lt;br /&gt;                            finally{IOUtils.close(fileStream); }}})&lt;br /&gt;                .transform(&lt;br /&gt;                    new FilterIterator( &lt;br /&gt;                       new Predicate&lt;JarEntry&gt;(){ &lt;br /&gt;                           public boolean satisfies(JarEntry entry){&lt;br /&gt;                               return entry.getName().endsWith(".class");}},&lt;br /&gt;                       new JarEntryIterator(file)))){&lt;br /&gt;                @Override&lt;br /&gt;                public Class&lt;?&gt; next(){&lt;br /&gt;                    final Class&lt;?&gt; clazz = super.next();&lt;br /&gt;                    if (!this.hasNext()) JarClassLoader.this.files.remove(file);                    &lt;br /&gt;                    return clazz;}&lt;br /&gt;                &lt;br /&gt;                @Override&lt;br /&gt;                public boolean hasNext(){&lt;br /&gt;                    final boolean hasNext = super.hasNext();&lt;br /&gt;                    if (!hasNext) JarClassLoader.this.files.remove(file);&lt;br /&gt;                    return hasNext;}};}&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * Loads all class files from the jar and returns a set of them.&lt;br /&gt;     */&lt;br /&gt;    public Set&lt;Class&lt;?&gt;&gt; loadAll(final JarFile file) throws IOException&lt;br /&gt;    {&lt;br /&gt;        return IteratorUtils.fill(new HashSet&lt;Class&lt;?&gt;&gt;(), this.load(file));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 15 Feb 2007 09:55:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3527</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>#post method in tests with a different controller</title>
      <link>http://snippets.dzone.com/posts/show/2880</link>
      <description>I wanted a #login method in test_helper that would allow me to easily login from any of my functional tests.  However, the #post method won't allow you to set a different controller than the one in the @controller instance variable that's defined in your test's #setup.  Well, by looking at how the &lt;a href="http://api.rubyonrails.org/classes/ActionController/TestProcess.html#M000046"&gt;#process method&lt;/a&gt; works, you can see that it just grabs the controller from @controller.  Redefine that, and you're good to go:&lt;br /&gt;&lt;code&gt;old_controller = @controller&lt;br /&gt;@controller = LoginController.new&lt;br /&gt;post(&lt;br /&gt;  :attempt_login,&lt;br /&gt;  {:user =&gt; {:name =&gt; 'joe', :password =&gt; 'password'}}&lt;br /&gt;)&lt;br /&gt;@controller = old_controller&lt;/code&gt;&lt;br /&gt;If you have several login methods, such as a #login_admin and #login_regular, you could make a wrapper to reduce duplication:&lt;br /&gt;&lt;code&gt;def wrap_with_controller( new_controller = LoginController )&lt;br /&gt;  old_controller = @controller&lt;br /&gt;  @controller = new_controller.new&lt;br /&gt;  yield&lt;br /&gt;  @controller = old_controller&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def login_admin&lt;br /&gt;  wrap_with_controller do&lt;br /&gt;    post(&lt;br /&gt;      :attempt_login,&lt;br /&gt;      {:user =&gt; {:name =&gt; 'root', :password =&gt; 'password'}}&lt;br /&gt;    )&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def login_regular&lt;br /&gt;  wrap_with_controller do&lt;br /&gt;    post(&lt;br /&gt;      :attempt_login,&lt;br /&gt;      {:user =&gt; {:name =&gt; 'joe', :password =&gt; 'password'}}&lt;br /&gt;    )&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 24 Oct 2006 18:54:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2880</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>Python curry implementation</title>
      <link>http://snippets.dzone.com/posts/show/2375</link>
      <description># Generalized version of curry, works w/ keywords too...  snarfed original version&lt;br /&gt;# from somewhere else ages ago, much simpler / cleaner than other implementations&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;curry = lambda func, *args, **kw:\&lt;br /&gt;            lambda *p, **n:\&lt;br /&gt;                 func(*args + p, **dict(kw.items() + n.items()))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 Aug 2006 03:55:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2375</guid>
      <author>jbone (Jeff Bone)</author>
    </item>
    <item>
      <title>JavaScript versions of the functions described in Martin Fowler's article "CollectionClosureMethod"</title>
      <link>http://snippets.dzone.com/posts/show/575</link>
      <description>See the original article at &lt;a href="http://www.martinfowler.com/bliki/CollectionClosureMethod.html"&gt;http://www.martinfowler.com/bliki/CollectionClosureMethod.html&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;These implement all the methods except the sorting related ones.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Array.prototype.select = function(detect) {&lt;br /&gt;    var result = [];&lt;br /&gt;    for (var i = 0; i &lt; this.length; ++i) {&lt;br /&gt;        if (detect(this[i])) {&lt;br /&gt;            result.push(this[i]);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return result;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Array.prototype.reject = function(detect) {&lt;br /&gt;    var result = [];&lt;br /&gt;    for (var i = 0; i &lt; this.length; ++i) {&lt;br /&gt;        if (!detect(this[i])) {&lt;br /&gt;            result.push(this[i]);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return result;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Array.prototype.partition = function(detect) {&lt;br /&gt;    var yeses = [];&lt;br /&gt;    var nos   = [];&lt;br /&gt;    for (var i = 0; i &lt; this.length; ++i) {&lt;br /&gt;        if (detect(this[i])) {&lt;br /&gt;            yeses.push(this[i]);&lt;br /&gt;        } else {&lt;br /&gt;            nos.push(this[i]);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return [yeses, nos];&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Array.prototype.all = function(detect) {&lt;br /&gt;    for (var i = 0; i &lt; this.length; ++i) {&lt;br /&gt;        if (!detect(this[i])) {&lt;br /&gt;            return false;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return true;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Array.prototype.any = function(detect) {&lt;br /&gt;    for (var i = 0; i &lt; this.length; ++i) {&lt;br /&gt;        if (detect(this[i])) {&lt;br /&gt;            return true;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return false;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Array.prototype.find = function(detect, ifNone) {&lt;br /&gt;    for (var i = 0; i &lt; this.length; ++i) {&lt;br /&gt;        if (detect(this[i])) {&lt;br /&gt;            return this[i];&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    if (ifNone === null) {&lt;br /&gt;        return null;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    return ifNone();&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Array.prototype.map = function(mapper) {&lt;br /&gt;    var result = [];&lt;br /&gt;    for (var i = 0; i &lt; this.length; ++i) {&lt;br /&gt;        result.push(mapper(this[i]));&lt;br /&gt;    }&lt;br /&gt;    return result;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Array.prototype.inject = function(initial, reducer) {&lt;br /&gt;    var result = initial;&lt;br /&gt;    for (var i = 0; i &lt; this.length; ++i) {&lt;br /&gt;        result = reducer(result, this[i]);&lt;br /&gt;    }&lt;br /&gt;    return result;&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 11 Aug 2005 06:15:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/575</guid>
      <author>keith (Keith Gaughan)</author>
    </item>
  </channel>
</rss>
