Posts Tagged Refactoring
Clojure function has-factors-in?
Posted by Proctor in Clojure, Declarative Programming, Deliberate Practice, Functional Programming, Lisp, Project Euler, Refactoring on August 27, 2012
Just another quick post this evening to share a new function I created as part of cleaning up my solution to Problem 1 of Project Euler.
Was just responding to a comment on Google+ on my update sharing the post Project Euler in Clojure – Problem 16, and I saw the commenter had his own solution to problem 1. In sharing my solution I realized that I could clean up my results even further, and added a function has-factors-in?
. These updates have also been pushed to my Project Euler in Clojure Github repository for those interested.
(defn has-factors-in? [n coll] (some #(factor-of? % n) coll))
Where before I had:
(defn problem1 ([] (problem1 1000)) ([n] (sum (filter #(or (factor-of? 3 %) (factor-of? 5 %))) (range n))))
It now becomes:
(defn problem1 ([] (problem1 1000)) ([n] (sum (filter #(has-factors-in? % [3 5]) (range n)))))
This change makes my solution read even more like the problem statement given.
Your thoughts?
–Proctor
Support for Arrays in Constructors using Castle Windsor
Posted by Proctor in .NET, C#, Castle Windsor, Refactoring, Software Design on August 27, 2012
As I mentioned in my post Aspect Oriented Timing in C# with Castle Windsor, we are using Castle Windsor as our Inversion of Container on our current project.
In the process of adding a couple of new features, there were places where the new code had to fit in to existing places where there were a longer (more than simply just a single if/else) set of conditionals. To address the changes that I needed to make, I added another conditional to go with the existing ones. This change was in the mood of make it work, knowing I would come back after it worked to make it right, and that this change would allow me to confirm the smell I was “sure” I was smelling.
One I had it working, I came back to make it right. The route I wanted to go was to emulate the refactoring Replace Conditional with Polymorphism by creating a “handler” abstraction, and pulling the logic for each of the conditionals into a separate implementation of the handler.
My new interface was defined as:
public interface INewHandler { bool CanHandle(ISomeObject obj); void Handle(ISomeObject obj); }
I then moved out the logic in each of the conditionals, as well as the conditionals themselves into new handlers. The conditionals were moved into the CanHandle
method, and the body of the conditional was moved to the HandleMethod
of the implementation.
With these defined, it became just a matter of using a LINQ query to find either the first, or all items that could handle the object, and then just call the Handle method on the ones that matched the filter.
injectedHandlers. Where(h => h.CanHandle(someObj)). ForEach(h => h.Handle(someObj));
After having this, all we need now is the ability to have all of the implementations injected into the constructor. This gets us to the title of this post, about having arrays injected into constructors. First we have to configure the container to add a new ArrayResolver as a subresolver.
container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
After this, I want to pick up all implementations of the INewHandler
in all assemblies.
container.Register( AllTypes.FromAssemblyInDirectory(new AssemblyFilter("bin")). BasedOn>INewHandler>(). WithService.Base(). Configure(c => c.LifeStyle.PerWebRequest) );
After adding these two to the registration of Castle Windsor, I now get all implementations injected into my constructor when I declare a constructor parameter that is a type of INewHandler[]
.
Hope someone else can find this helpful.
–Proctor