Post-rsses on colindrake.mehttps://colindrake.me/post/index.xml
Recent content in Post-rsses on colindrake.meHugo -- gohugo.ioen-usMon, 21 Mar 2016 00:00:00 +0000You Could Have Invented Objectshttps://colindrake.me/post/you-could-have-invented-objects/
Mon, 21 Mar 2016 00:00:00 +0000https://colindrake.me/post/you-could-have-invented-objects/
<p>I&rsquo;ve always been interested in programming languages and paradigms.</p>
<p>Usually, this manifests itself in reading about functional programming, the Lisp
family of languages, compilers, and lately,
<a href="https://en.wikipedia.org/wiki/Protocol_(object-oriented_programming)">protocol-oriented programming</a>
with Swift and Clojure.</p>
<p>However, I recently found myself circling back to a paradigm that most of us take
for granted: object-oriented programming. After reading many articles on the
<a href="http://c2.com/cgi/wiki">c2.com</a> wiki, I ended up playing around with a
<a href="http://pharo.org/">Pharo</a> Smalltalk installation on my Mac. While I can&rsquo;t
say that I plan on using Smalltalk for my next project, my curiosity was piqued.</p>
<p>The sheer level of <a href="https://www.youtube.com/watch?v=HOuZyOKa91o">interaction and introspection</a>
supported by the Pharo object
system/environment was amazing. So naturally, I wondered if I could build a small
home-grown object system myself&hellip; The following is a short exploration in creating a (primitive) object system in Swift.</p>
<h2 id="overview">Overview</h2>
<p>Let&rsquo;s take a step back to CS 101: what are some of the basic, core facets of object oriented programming? <a href="https://en.wikipedia.org/wiki/Object-oriented_programming">Wikipedia</a>
lists a few:</p>
<ul>
<li>Message Passing</li>
<li>Encapsulation</li>
<li>Composition/Inheritance/Delegation</li>
<li>Open Recusion</li>
</ul>
<p>Now, pretend for a while that <code>class</code>, <code>struct</code>, <code>enum</code>, and friends don&rsquo;t
exist in Swift. What type of (more fundamental) structure is capable of expressing
the above features? How about functions, or closures?</p>
<ul>
<li>Message Passing: perform &ldquo;procedures&rdquo; dispatching based off of a passed-in message name parameter</li>
<li>Encapsulation: closures already capture lexical scope</li>
<li>Composition: isn&rsquo;t this what the functional paradigm is about? 😃</li>
<li>Inheritance/Delegation: simply call another closure!</li>
<li>Open Recusion: well&hellip; we&rsquo;ll get there&hellip;</li>
</ul>
<p>With the above in mind, let&rsquo;s jump in.</p>
<h2 id="message-passing">Message Passing</h2>
<p>For our purposes, we&rsquo;ll define a <code>Message</code> as a <code>String</code> and an object as some
type that interprets a <code>Message</code> to (possibly) return some value.</p>
<pre><code class="language-swift">typealias Message = String
typealias Object = (Message) -&gt; Any?
</code></pre>
<p>Unfortunately, as you can see, we&rsquo;ll be&hellip; sidestepping the type
system for this hacky experiment 😉</p>
<p>Let&rsquo;s start by defining our first &ldquo;object&rdquo; that can respond to messages.
Our convention will be as such: messages that are not known by the objects will
return <code>nil</code>, and all others will return some value.</p>
<pre><code class="language-swift">let obj1: Object = { (msg: Message) in
switch msg {
case &quot;do-something&quot;: return &quot;sure!&quot;
default: return nil
}
}
</code></pre>
<p>Great! Now we have an arbitrary object that can dispatch to code based off of messages.</p>
<pre><code class="language-swift">obj1(&quot;do-something&quot;) // =&gt; &quot;sure!&quot;
obj1(&quot;do-other-thing&quot;) // =&gt; nil
</code></pre>
<p>This buys us the simplest benefit of OOP: the ability to abstract out procedures
packaged with data based off of some known interface.</p>
<h2 id="encapsulation">Encapsulation</h2>
<p>The above code snippets are fine and dandy, but we don&rsquo;t really want to be
defining our own objects ad-hoc all the time: we all know the benefits of using
classes as reusable blueprints for creating objects. Luckily for us, this can
be represented as&hellip; you guess it &ndash; another closure!</p>
<p>For us, a &ldquo;class&rdquo; is really just comprised of a constructor function that returns
a new object/instance (another closure, as demonstrated above).</p>
<p>Additionally, closures can capture the lexical environment, so any parameters
passed into our constructor function can act as a sort of read-only instance
variable for our internal object. In a nutshell, we get encapsulation for free.</p>
<pre><code class="language-swift">// Constructors are uppercase by convention...
let Person = { (firstName fn: String, lastName ln: String) -&gt; Object in
return { (msg: Message) -&gt; Any? in
switch msg {
case &quot;full-name&quot;: return &quot;\(fn) \(ln)&quot;
case &quot;description&quot;: return &quot;&lt;Person&gt;&quot;
default: return nil
}
}
}
</code></pre>
<p>&hellip;and to use it&hellip;</p>
<pre><code class="language-swift">// Define a person and test out methods...
let p = Person(firstName: &quot;Colin&quot;, lastName: &quot;Drake&quot;)
p(&quot;full-name&quot;) // =&gt; &quot;Colin Drake&quot;
p(&quot;description&quot;) // =&gt; &quot;&lt;Person&gt;&quot;
p(&quot;address&quot;) // =&gt; nil
</code></pre>
<p>Immutability is nice, but we can also package up mutable state pretty easily.
All it really takes is copying the constructor parameters to a mutable local variable.
Take, for instance, this <code>Counter</code> example:</p>
<pre><code class="language-swift">let Counter = { (initial: Int) -&gt; Object in
var counter = initial
return { (msg: Message) in
switch msg {
case &quot;count&quot;: return counter
case &quot;increment&quot;:
counter += 1
return counter
case &quot;decrement&quot;:
counter -= 1
return counter
default: return nil
}
}
}
</code></pre>
<h2 id="composition-inheritance-and-delegation">Composition, Inheritance, and Delegation</h2>
<p>One of the biggest attractions to OOP is the ability for code reuse. Both object
composition and inheritance allow for this, but in two different ways. Composition
allows us to delegate messages out to another object, and inheritance allows us
to do the same, but specifically to some object we deem a &ldquo;parent&rdquo; to the current one.</p>
<p>We&rsquo;ll implement inheritance as a simple delegated call to our known parent, if
the object itself doesn&rsquo;t know how to interpret the message. Most languages and
object systems have this as a built-in to the syntax/model, but we&rsquo;ll keep things
simple and explicit.</p>
<pre><code class="language-swift">/// Represents possible speeds at which atheletes can run.
enum RunSpeed: String {
case Slow
case Fast
}
/// Represents an Athelete, a subclass of Person.
let Athelete = { (firstName fn: String,
lastName ln: String,
runningSpeed rs: RunSpeed) -&gt; Object in
// Create a person &quot;super&quot; object to delegate to.
let superInstance = Person(firstName: fn, lastName: ln)
return { (msg: Message) -&gt; Any? in
switch msg {
// Simple instance method.
case &quot;how-fast?&quot;: return rs
// Override behavior of a Person.
case &quot;description&quot;: return &quot;&lt;A \(rs.rawValue)-Moving Athelete&gt;&quot;
// Delegate unknown messages to superclass.
default: return superInstance(msg)
}
}
}
</code></pre>
<p>And example usage:</p>
<pre><code class="language-swift">let a = Athelete(firstName: &quot;Colin&quot;,
lastName: &quot;Drake&quot;,
runningSpeed: .Slow)
a(&quot;how-fast?&quot;) // =&gt; RunSpeed.Slow
a(&quot;description&quot;) // =&gt; &quot;&lt;A Slow-Moving Athelete&gt;&quot;
a(&quot;full-name&quot;) // =&gt; &quot;Colin Drake&quot; (a Person method)
</code></pre>
<h2 id="open-recursion">Open Recursion</h2>
<p>Within the context of OOP, open recursions refers to the ability to dispatch
methods on yourself. As an object, you want the ability to call one of your own
methods, but you don&rsquo;t want the responsibility of needing to know the
whereabouts of the raw implementation yourself.</p>
<p>This self-referential dispatch is pretty trivial (and IMHO more elegant) in a Lisp-like
language, but we&rsquo;re in the Swift world: a closure recursively calling itself will
raise a compiler error indicating that a value is attempting to use itself in its
own definition.</p>
<p>To get around this, we&rsquo;ll use the <a href="http://stackoverflow.com/a/94056">Y-combinator</a>,
a function that &ldquo;enables recursion, when you can&rsquo;t refer to the function from
within itself.&rdquo; It&rsquo;s a bit tricky to define, but easy to use.</p>
<pre><code class="language-swift">// h/t: http://bit.ly/1U2N0HP
func Y&lt;T, R&gt;( f: (T -&gt; R) -&gt; (T -&gt; R) ) -&gt; (T -&gt; R) {
return { t in f(Y(f))(t) }
}
</code></pre>
<p>With that mess out of the way, let&rsquo;s try to use it in the OOP context
to recursively call our dispatch function:</p>
<pre><code class="language-swift">let Cat = { (name n: String, age a: Int) -&gt; Object in
return Y {
dispatch in { (msg: Message) -&gt; Any? in
switch msg {
case &quot;name&quot;: return &quot;\(n) the Cat&quot;
case &quot;description&quot;:
// Dispatch on ourself.
let me = dispatch(&quot;name&quot;)!
return &quot;&lt;A Feline named \(me)&gt;&quot;
default: return nil
}
}
}
}
let leo = Cat(name: &quot;Leo&quot;, age: 2)
leo(&quot;name&quot;) // =&gt; &quot;Leo the Cat&quot;
leo(&quot;description&quot;) // =&gt; &quot;&lt;A Feline named Leo the Cat&gt;&quot;
</code></pre>
<p>The value of this is that we don&rsquo;t care if we implemented the method we&rsquo;re
calling, or if it is delegated to a super instance. All of that is abstracted
from us via the call to <code>dispatch</code>.</p>
<p>And with that, our simple homegrown object system is complete! However,
I&rsquo;ve got one more trick up my sleeve&hellip;</p>
<h2 id="goodies">Goodies</h2>
<p>Using metaprogramming to aid in code readability is one of my favorite features of
Ruby. An expressive, powerful example of this is the ability to call a
<code>find_by_&lt;column&gt;</code> method on ActiveRecord models in Rails without needing to
define said method. Rails achieves this via the <code>method_missing</code> feature in Ruby&rsquo;s
object system.</p>
<p>In Ruby, if a message is
sent to an object, and that object doesn&rsquo;t have an implementation for it, the
<code>method_missing</code> method is called instead with the original method name as a parameter. By
default, this raises an exception, but developers have the ability to override
and provide custom behavior. This is where Rails builds and caches customized
database query functions.</p>
<p>It turns out, we can emulate this dynamic nature
pretty easily within our system:</p>
<pre><code class="language-swift">let SQLBuilder = { (table: String) -&gt; Object in
return { (msg: Message) in
let field = msg.stringByReplacingOccurrencesOfString(
&quot;sort-by-&quot;,
withString: &quot;&quot;)
if field != msg {
return &quot;SELECT * FROM \(table) ORDER BY \(field)&quot;
}
return nil
}
}
let s = SQLBuilder(&quot;user&quot;)
s(&quot;sort-by-age&quot;) // =&gt; &quot;SELECT * FROM user ORDER BY age&quot;
s(&quot;sort-by-name&quot;) // =&gt; &quot;SELECT * FROM user ORDER BY name&quot;
s(&quot;group-by-something&quot;) // =&gt; nil
</code></pre>
<p>We&rsquo;re missing the robustness of a dynamic <code>Object</code> superclass to route things,
but hey, we&rsquo;ve got the same results 😉</p>
<h2 id="conclusion">Conclusion</h2>
<p>In the end, our object system is neither featureful nor expressive.</p>
<p>But to me, the most important takeaway is that given certain fundamental functional
language features, any developer could have come up with and implemented OOP.
At its core, the concepts and <em>patterns</em> of OOP are very simple &ndash; and it&rsquo;s just that.</p>
<p>OOP can be thought of as a set of (helpful!) <em>patterns</em> that can be implemented
on top of a more fundamental, functional paradigm.
We can truly <a href="http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html">say</a>
that &ldquo;objects are a poor man&rsquo;s closures&rdquo;.</p>
<p>The code in this article is available as a <a href="https://gist.github.com/cfdrake/2a20fe432547d92b50a8">gist</a>.</p>
Implementing a Small DSL in Swifthttps://colindrake.me/post/implementing-a-small-dsl-in-swift/
Wed, 28 Oct 2015 00:00:00 +0000https://colindrake.me/post/implementing-a-small-dsl-in-swift/
<p>I was working on an iOS project earlier today when I ended up running into an annoying bug in my Swift Core Data stack. My seemingly correct <code>NSFetchRequest</code> code was returning <code>nil</code> for all inputs, and I couldn&rsquo;t figure out why.</p>
<p>After inspecting and thinking through totally different parts of the codebase, I had an ah-hah moment! I went back to my fetch request code and sure enough I saw something resembling the following:</p>
<pre><code class="language-swift">let predicate = NSPredicate(format: &quot;%@ = %@&quot;, key, value)
</code></pre>
<p>Those acquainted with Core Data will probably notice my error pretty quickly: I accidentally formatted <code>key</code> as a value type! The <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html">format string</a> should actually be <code>%K = %@</code>, that way Core Data will know that <code>key</code> is in fact, a substitutable key. Otherwise, it will simply perform string comparison between the two <code>String</code> values.</p>
<p>I lamented that there was no error checking in format strings and quickly moved on. However, I had the idea in the back of my mind that a small, focused, problem-driven language, or <a href="https://en.wikipedia.org/wiki/Domain-specific_language">Domain Specific Language</a>, could easily be built in Swift to aid in basic validation/cleanliness of <code>NSPredicate</code> construction.</p>
<h2 id="overview">Overview</h2>
<p>I figured a limited proof of concept DSL for <code>NSPredicate</code> construction could be made pretty quickly for &ldquo;fun&rdquo; 😉 As a baseline, all I wanted to support was:</p>
<ul>
<li>Referencing keypaths and simple <code>String</code>-convertible constant values</li>
<li>Checking keys against values with comparison operators</li>
<li>Combining expressions with logical operators, such as <code>AND</code> and <code>OR</code></li>
</ul>
<p>My goal syntax was something resembling the following:</p>
<pre><code class="language-swift">let predicate = NSPredicate(&quot;foo&quot; == true &amp;&amp; !(&quot;bar&quot; == 4))
</code></pre>
<p>Luckily for me, this experiment turned out to be relatively straightforward thanks to many of Swift&rsquo;s powerful language features. Let&rsquo;s dive straight into the implementation!</p>
<p><strong>Note:</strong> This article is also available as an <a href="https://github.com/cfdrake/swift-dsl-example">Xcode Playground</a> on Github.</p>
<h2 id="keys-and-values">Keys and Values</h2>
<p>We&rsquo;ll start with the most simple object to represent: a keypath. A keypath is simply a string that represents a (potentially nested) property, for example <code>amountDeposited</code> or <code>parent.firstName</code>. To encode this in our DSL, we&rsquo;ll simply represent a <code>KeyPath</code> as a <code>String</code>:</p>
<pre><code class="language-swift">typealias KeyPath = String
</code></pre>
<p>The second most simple object type to represent is a constant value. Again, in our simplified DSL, we&rsquo;ll only be supporting values that can be directly represented as strings. Thus, we simply want <code>ValueType</code> to be those objects that are <code>CustomStringConvertible</code>:</p>
<pre><code class="language-swift">typealias ValueType = CustomStringConvertible
</code></pre>
<p>A more full-featured DSL would not be encoding values into strings, but this representation will suit our small experiment 👍🏼</p>
<h2 id="operators">Operators</h2>
<p>Now we can represent the names and values of things we want to query. However, we need a way to relate the two: this is where operators come into play.</p>
<p>We&rsquo;ll support a small subset of comparison and logical operators for querying data. These can be easily represented by Swift&rsquo;s ever-helpful <code>RawRepresentable</code>-backed <code>enum</code> type:</p>
<pre><code class="language-swift">enum Comparison: String {
case EqualTo = &quot;=&quot;
case GreaterThan = &quot;&gt;&quot;
case LessThan = &quot;&lt;&quot;
case And = &quot;&amp;&amp;&quot;
case Or = &quot;||&quot;
}
</code></pre>
<p>Given that we&rsquo;ll eventually want to convert these <code>Comparison</code> values into a <code>String</code> representation, we&rsquo;ll adhere to <code>CustomStringConvertible</code> ourselves. Because we&rsquo;ve already defined a nice string for the <code>rawValue</code>, we&rsquo;ll simply return that for our implementation.</p>
<pre><code class="language-swift">extension Comparison: CustomStringConvertible {
var description: String { return rawValue }
}
</code></pre>
<p><strong>Tip:</strong> We could have easily made calls to <code>rawValue</code> in our code later for <code>String</code> versions of our operators, but I consider implementing <code>CustomStringConvertible</code> a cleaner mechanism. For one, it gives you a free initializer: <code>let str = String(Comparison.EqualTo)</code>. But more importantly, it keeps the &ldquo;storage mechanism&rdquo; of <code>Comparison</code> abstracted out.</p>
<h2 id="expressions">Expressions</h2>
<p>The <code>Expression</code> type will represent a value or compound comparison of values that we can convert to an <code>NSPredicate</code>. In our DSL&rsquo;s simplified model, there are four types of values:</p>
<ul>
<li>A property</li>
<li>A constant</li>
<li>A negated expression</li>
<li>A compound expression connecting two subexpressions</li>
</ul>
<p>These concepts can be directly translated into Swift via yet another <code>enum</code>. However, this time we&rsquo;ll be creating a <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-ID145">recursive enumeration</a>:</p>
<pre><code class="language-swift">indirect enum Expression {
case Property(KeyPath)
case Constant(CustomStringConvertible)
case Negated(Expression)
case Binary(Expression, Comparison, Expression)
}
</code></pre>
<p>The complex cases to explain here are <code>Negated</code> and <code>Binary</code>.</p>
<p>An <code>Expression</code> of type <code>Negated</code> is simply a subexpression with an additional tag attached saying that it should be negated. Likewise, <code>Binary</code> is composed of two subexpressions related by a <code>Comparison</code> operator. In both of these cases, the subexpressions could be anything from a simple <code>Property</code> to another <code>Binary</code> relation.</p>
<p>Feel free to read up on <a href="https://en.wikipedia.org/wiki/Algebraic_data_type">Algebraic Data Types</a> if you want to learn more about structuring data like this!</p>
<p>At this point, we have enough ground-level types to <em>represent</em> what we want to eventually generate. Next, let&rsquo;s take a look at how we can build up an <code>NSPredicate</code>.</p>
<h2 id="nspredicate-creation">NSPredicate Creation</h2>
<p>Our DSL implementation will create <code>NSPredicates</code> by concatenating <code>String</code> representations of our query together. Keypaths, constants, and operators are already strings (or string convertible) but how do we stringify an <code>Expression</code>?</p>
<p>You might have guessed it: Recursion again!</p>
<p>By defining how to convert the simple cases of <code>Expression</code> to a <code>String</code> (the <code>Property</code> and <code>Constant</code> cases), we can implement the more complicated cases on top. Let&rsquo;s take a look at the code:</p>
<pre><code class="language-swift">extension Expression: CustomStringConvertible {
var description: String {
switch self {
case let .Property(key): return key
case let .Constant(value): return String(value)
case let .Negated(expression): return &quot;!(\(expression))&quot;
case let .Binary(lhs, op, rhs): return &quot;(\(lhs)) \(op) (\(rhs))&quot;
}
}
}
</code></pre>
<p>Now that we can fully convert an <code>Expression</code>, our simplified <em>representation</em> of an <code>NSPredicate</code>, to a <code>String</code>, we simply need a clean way to pass it to <code>NSPredicate</code> itself. To do this, we can define a convenience initializer:</p>
<pre><code class="language-swift">extension NSPredicate {
convenience init(_ expression: Expression) {
self.init(format: String(expression))
}
}
</code></pre>
<p>After all this work, let&rsquo;s stop, take a look, and verify that we&rsquo;ve got something that works:</p>
<pre><code class="language-swift">let expr1 = Expression.Property(&quot;amount&quot;)
let expr2 = Expression.Constant(300)
let expr3 = Expression.Binary(expr1, Comparison.EqualTo, expr2)
let pred = NSPredicate(expr3) // amount == 300
</code></pre>
<p>Cool, but it&rsquo;s still a lot of typing 😒 Now for the fun part&hellip;</p>
<h2 id="syntactic-sugar">Syntactic Sugar</h2>
<p>The main problem with the current state of our DSL is that we have no clean way to create and combine expressions.</p>
<p>Swift&rsquo;s <a href="http://nshipster.com/swift-literal-convertible/">literal convertible protocols</a> will aid in being able to construct values and keypaths easily, while <a href="http://nshipster.com/swift-operators/#overloading">operator overloading</a> will provide the basis of a sugary combination syntax.</p>
<p>Literal protocols will allow us to type a value such as <code>3</code> and have it automatically converted to <code>Expression.Constant(3)</code>, for example. Likewise, it&rsquo;d be great if strings were automatically interpreted as keypaths. We support this behavior by adhering to a number of different protocols and defining specialized initializers.</p>
<p>I&rsquo;ve implemented <code>Bool</code>, <code>String</code>, and <code>Integer</code> conversion in the snippet below - it&rsquo;s pretty much plug &lsquo;n&rsquo; chug mapping values into <code>Constant</code>, etc. cases:</p>
<pre><code class="language-swift">extension Expression: BooleanLiteralConvertible, IntegerLiteralConvertible, StringLiteralConvertible {
typealias ExtendedGraphemeClusterLiteralType = StringLiteralType
typealias UnicodeScalarLiteralType = StringLiteralType
init(booleanLiteral: Bool) {
self = .Constant(booleanLiteral)
}
init(integerLiteral: Int) {
self = .Constant(integerLiteral)
}
init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
self = .Property(value)
}
init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
self = .Property(value)
}
init(stringLiteral value: StringLiteralType) {
self = .Property(value)
}
}
</code></pre>
<p>Next, we&rsquo;ll use (abuse?) some operators for combining expressions. The most obvious way of implementing a simple, readable DSL for predicates is by overloading the relevant comparison and logical operators to work on <code>Expression</code> objects as well.</p>
<p>Here&rsquo;s a snippet of a couple of implementations as an example:</p>
<pre><code class="language-swift">func ==(lhs: Expression, rhs: Expression) -&gt; Expression {
return Expression.Binary(lhs, Comparison.EqualTo, rhs)
}
// ...other operators omitted for brevity...
prefix func !(expr: Expression) -&gt; Expression {
return Expression.Negated(expr)
}
</code></pre>
<p>Implementing the syntactic sugar for this DSL ends up being little more than tying an operator with the matching <code>Expression</code> case.</p>
<p>As a note, it&rsquo;s always worth thinking whether the above is a good idea, or if you should introduce your own operators instead. The second case should almost always be the preferred solution, but if the semantics of an existing operator fits 100% with your data structures (think a <code>Vector</code> type that could work with <code>+</code>, etc) then it might be okay to overload.</p>
<p>Now that we&rsquo;ve got some of the philosophy out of the way, let&rsquo;s give it a spin:</p>
<pre><code class="language-swift">let bourbonExpr: Expression = (&quot;proof&quot; &gt; 100 &amp;&amp; &quot;name&quot; == &quot;Wild Turkey&quot;)
let deletedExpr: Expression = (&quot;deleted&quot; == true)
</code></pre>
<h2 id="final-example">Final Example</h2>
<p>As a working final example, we&rsquo;ll define a <code>Person</code> structure, and filter an <code>NSArray</code> of people using an <code>NSPredicate</code> constructed in our DSL:</p>
<pre><code class="language-swift">class Person: NSObject {
let tweets: Int
let age: Int
init(tweets: Int, age: Int) {
self.tweets = tweets
self.age = age
}
}
// Setup some contrived, inaccurate data ;)
let people: NSArray = [Person(tweets: 250, age: 18),
Person(tweets: 80, age: 22)]
// --&gt; [{NSObject, tweets 250, age 18}, {NSObject, tweets 80, age 15}]
let pred = NSPredicate(&quot;tweets&quot; &gt; 200 &amp;&amp; &quot;age&quot; == 18)
// --&gt; &quot;tweets &gt; 200 AND age == 18&quot;
let filtered = people.filteredArrayUsingPredicate(pred)
// --&gt; [{NSObject, tweets 250, age 18}]
</code></pre>
<p>Looks neat, plus you get some <em>minor</em> validation wins done by the compiler. Note, however, it is still entirely possible to construct an invalid <code>NSPredicate</code> with the existing DSL - it&rsquo;s more demo/thought experiment than production-ready library!</p>
<p>Practically speaking, a similar, type-checked, more fleshed out DSL could perhaps end up being useful for Core Data development, where you are limited to the string parsing predicate API. For other applications, however, <a href="http://nshipster.com/nspredicate/#block-predicates">block predicates</a> or <code>SequenceType</code>&rsquo;s <code>filter</code> function will potentially solve the problem better.</p>
Wrapping a C Library in a Swift Frameworkhttps://colindrake.me/post/wrapping-a-c-library-in-a-swift-framework/
Mon, 05 Oct 2015 00:00:00 +0000https://colindrake.me/post/wrapping-a-c-library-in-a-swift-framework/
<p>I recently started work on a project where I wanted to wrap up a C library for OS X with a more palatable Swift framework interface. My first thought was to follow what I saw in Apple&rsquo;s <a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html">Mix and Match documentation</a>, a guide to integrating C/Objective-C with Swift code.</p>
<p>The guide tells you to create a <a href="http://www.learnswiftonline.com/getting-started/adding-swift-bridging-header/">Bridging Header</a>, a file where you can denote the needed non-Swift dependencies for the compiler to then expose to your Swift code. However, when following this advice with a framework target, Xcode gives you this friendly error:</p>
<p><img src="https://colindrake.me/images/c-library/error.png" alt="" /></p>
<p>After much searching, poking, and prodding, I came across the <a href="https://github.com/Zewo/SwiftGo">SwiftGo</a> project, which appeared to have the same setup as I needed. (Kudos to <a href="http://loganwright.io/">@loganwright</a> from the <a href="https://ios-developers.io/">iOS Developers Slack</a> for pointing this out!)</p>
<p>I ended up reading the source of SwiftGo and creating an example wrapper project for <a href="https://github.com/doches/progressbar">progressbar</a>, a simple C library to display a progress bar with <a href="https://en.wikipedia.org/wiki/Ncurses">ncurses</a>, as I went along. Check out the final project <a href="https://github.com/cfdrake/swift-framework-c-library-example">here</a>.</p>
<p>Now, let&rsquo;s take a look at how it&rsquo;s set up.</p>
<h2 id="creating-the-project">Creating the Project</h2>
<p>First, we&rsquo;ll start out by creating the project. Given that we want to target Mac OS X, we&rsquo;ll want to start out with a <strong>Cocoa Framework</strong> type:</p>
<p><img src="https://colindrake.me/images/c-library/fw.png" alt="" /></p>
<p>Name the project &ldquo;Progressbar&rdquo; and save it somewhere.</p>
<h2 id="the-wrapped-library">The Wrapped Library</h2>
<p>Next, we&rsquo;ll want to pull in the progressbar C library, our only dependency:</p>
<pre><code>$ cd Progressbar
$ mkdir Dependencies
$ git clone git@github.com:doches/progressbar.git
</code></pre>
<p>If you take a look at progressbar&rsquo;s code, you&rsquo;ll see it is actually comprised of only a few files:</p>
<pre><code>$ cd progressbar
$ ls include/
progressbar.h statusbar.h
$ ls lib/
progressbar.c statusbar.c
</code></pre>
<p>To include these in our wrapper library, open a Finder window and drag all of the above files into the existing Xcode project. I created a few Groups in the project to help organize things:</p>
<p><img src="https://colindrake.me/images/c-library/deps.png" alt="" /></p>
<h2 id="module-mapping">Module Mapping</h2>
<p>We&rsquo;ve now got our C library into our Swift project (and Xcode will even compile it all), but we have no way to <em>call</em> it from Swift. To remedy this, we use a <strong>Module Map</strong>.</p>
<p>The <a href="http://clang.llvm.org/docs/Modules.html#module-maps">module.map</a> file is a Clang-specific file that &ldquo;describes how a collection of existing headers maps on to the (logical) structure of a module.&rdquo; Luckily for us, these files are simple to create and will allow Clang to do most of the work for us!</p>
<p>Inside of the main source directory (<code>Progressbar</code>), create a file called <code>module.map</code> with the following contents:</p>
<pre><code>module Libprogressbar [system] {
header &quot;Dependencies/progressbar/include/progressbar.h&quot;
export *
}
</code></pre>
<p>Feel free to drag and drop it into Xcode for easy editing (but note this is not actually necessary).</p>
<p>Next, we need to inform Xcode of the whereabouts of this file. Open up the Project settings for Progressbar, select <strong>Build Settings</strong>, and look for the one titled <strong>Import Paths</strong> under <strong>Swift Compiler - Search Paths</strong>.</p>
<p>Change it the value of this setting to <code>$(SRCROOT)/Progressbar</code> and ensure that <code>non-recursive</code> is selected. Your <code>module.map</code> file should be in the folder displayed by Xcode.</p>
<p><img src="https://colindrake.me/images/c-library/searchpaths.png" alt="" /></p>
<p>Now we can now simply (in Swift parlance) <code>import Libprogressbar</code> and access all of the C functions, macros, etc. underneath it.</p>
<h2 id="the-wrapper">The Wrapper</h2>
<p>For this example project, I only ended up writing a minimal wrapper around the original C library for demonstrative purposes. In any case, wrapping a C library like this allows callers to only have to deal with native Swift types, keeping their application layer nice and clean.</p>
<p>The code for our <code>Progressbar</code> wrapper is below (any calls to the <code>Libprogressbar</code> module are interfacing directly with the C library we included):</p>
<pre><code class="language-swift">import Foundation
import Libprogressbar
/// Class outputting an animated terminal progress bar.
public final class Progressbar {
let bar: UnsafeMutablePointer&lt;progressbar&gt;
public init(text: String, max: UInt) {
let cstring = (text as NSString).UTF8String
bar = Libprogressbar.progressbar_new(cstring, max)
}
public func increment() {
Libprogressbar.progressbar_inc(bar)
}
public func finish() {
Libprogressbar.progressbar_finish(bar)
}
}
</code></pre>
<h2 id="example-usage">Example Usage</h2>
<p>With our wrapper defined, we can finally write some simple application code to test our framework:</p>
<pre><code class="language-swift">import Cocoa
import Progressbar
let max: UInt = 30
let bar = Progressbar(text: &quot;foo&quot;, max: max)
for i in 1...max {
bar.increment()
sleep(1)
}
bar.finish()
// Example Output:
// foo |================================== | ETA: 0h00m02s
</code></pre>
<p>If you&rsquo;re following along with the <a href="https://github.com/cfdrake/swift-framework-c-library-example">Xcode project</a>, check out the Playground file under the <code>Example</code> group. If you have the Console Window open (<strong>⌘-Shift-Y</strong>), you should see a progress bar animating over the course of 30 seconds.</p>
An Observable Pattern Implementation in Swifthttps://colindrake.me/post/an-observable-pattern-implementation-in-swift/
Thu, 01 Oct 2015 00:00:00 +0000https://colindrake.me/post/an-observable-pattern-implementation-in-swift/
<p>Over the past few days, I&rsquo;ve been working a new Mac app in Swift as a part of <a href="https://gumroad.com/smallproductlab">Gumroad&rsquo;s Small Product Lab</a> challenge. This app contains a simple <code>struct</code> type, <code>AppConfig</code>, representing the application&rsquo;s editable configuration. What I needed to build was a view controller for the user to edit and update said values, and this is where I ran into trouble.</p>
<p>The normal pattern in the Objective-C world to implement this is to use <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaBindings/CocoaBindings.html">Cocoa Bindings</a>, an awesome feature implemented on top of <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html">KVO</a> that allows you to automatically bind your (Mac) UI to instance variables in Interface Builder.</p>
<p>However, Cocoa Bindings will only work for types that are <a href="http://stackoverflow.com/q/24092285">subclasses of NSObject</a>. I felt that porting my <code>AppConfig</code> struct over to a class, let alone an <code>NSObject</code> subclass, conflicted with the fact that it was better represented as a <a href="https://www.mikeash.com/pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html">value type</a>, and I definitely didn&rsquo;t want to needlessly bring in the Objective-C runtime for this simple case.</p>
<p>Given that I&rsquo;ve been working on this app to both scratch an itch and to use Swift in a practical setting, I decided to write my own native Swift implementation of the <a href="https://en.wikipedia.org/wiki/Observer_pattern">Observable Pattern</a>.</p>
<h2 id="protocols-everywhere">Protocols Everywhere</h2>
<p>To begin with, I wanted to come up with a <code>protocol</code> that I could implement to satisfy my data binding woes. I decided on a <code>subscribe</code>/<code>unsubscribe</code> type model, passing in an owner/observer as the object to update a subscription off of.</p>
<pre><code class="language-swift">protocol ObservableProtocol {
typealias T
var value: T { get set }
func subscribe(observer: AnyObject,
block: (newValue: T, oldValue: T) -&gt; ())
func unsubscribe(observer: AnyObject)
}
</code></pre>
<p>Assuming we are in some type of object context with <code>self</code>, implementing the <code>Observable</code> protocol would allow code to be written somewhat like this:</p>
<pre><code class="language-swift">let initial = 3
var v = initial
var obs = Observable(initial)
obs.subscribe(self) { (newValue, oldValue) in
print(&quot;Object updated!&quot;)
v = newValue
}
obs.value = 4 // Trigger update.
print(v) // 4!
</code></pre>
<p>Perfect! Now to write it&hellip;</p>
<h2 id="implementation">Implementation</h2>
<p>Given that the <code>Observable</code> object has state/a lifecycle, I decided to make it a class:</p>
<pre><code class="language-swift">public final class Observable&lt;T&gt;: ObservableProtocol {
// ...
}
</code></pre>
<p>We&rsquo;ll start out by defining a variable and a couple of handy types.</p>
<p>Our model of subscribers is <code>observers</code>, a variable array of <code>ObserversEntry</code> entries. Each entry is a tuple composed of a listening object and the block it expects to run upon the <code>Observable</code> firing. By passing in this listening object and associating it with the block to execute, we can easily look for it in the <code>unsubscribe</code> method to remove it.</p>
<pre><code class="language-swift">typealias ObserverBlock = (newValue: T, oldValue: T) -&gt; ()
typealias ObserversEntry = (observer: AnyObject, block: ObserverBlock)
private var observers: Array&lt;ObserversEntry&gt;
</code></pre>
<p>Now we&rsquo;ll need to implement an <code>init</code> for our class. The default initializer will simply take an initial value for the observable (declaration forthcoming). Given that we&rsquo;re writing Swift here, we&rsquo;ll need to initialize our non-optional <code>observers</code> array as well.</p>
<pre><code class="language-swift">init(_ value: T) {
self.value = value
observers = []
}
</code></pre>
<p>At this point, when we construct an <code>Observable</code> we&rsquo;ll have an initial value. This value is even assignable, but currently we don&rsquo;t have any way of telling the objects in our <code>observers</code> array that the value changed. To do this, we&rsquo;ll implement <code>didSet</code> for our <code>value</code> variable. All we need to do is loop through our listeners and call their associated blocks. Simple!</p>
<pre><code class="language-swift">var value: T {
didSet {
observers.forEach { (entry: ObserversEntry) in
// oldValue is an implicit parameter to didSet in Swift!
let (_, block) = entry
block(newValue: value, oldValue: oldValue)
}
}
}
</code></pre>
<p>Last but not least, the mechanism to notify observers is in place, but we have no way to update the <code>observers</code> array. We&rsquo;ll implement <code>subscribe</code> and <code>unsubscribe</code> to package up and add/remove observer tuples into the internal array.</p>
<pre><code class="language-swift">func subscribe(observer: AnyObject, block: ObserverBlock) {
let entry: ObserversEntry = (observer: observer, block: block)
observers.append(entry)
}
func unsubscribe(observer: AnyObject) {
let filtered = observers.filter { entry in
let (owner, _) = entry
return owner !== observer
}
observers = filtered
}
</code></pre>
<p>That&rsquo;s all it takes!</p>
<p><strong>Note:</strong> Please keep in mind that this is a simple, naïve, implementation without any considerations for performance, etc.</p>
<h2 id="syntactic-sugar">Syntactic Sugar</h2>
<p>While this works, I figured I could throw in just a little syntactic sugar to reduce the repetition of writing <code>foo.value = &lt;value&gt;</code>. I decided to override the <code>&lt;&lt;</code> operator:</p>
<pre><code class="language-swift">func &lt;&lt;&lt;T&gt;(observable: Observable&lt;T&gt;, value: T) {
observable.value = value
}
</code></pre>
<p><strong>Update:</strong> It appears I&rsquo;ve been (fairly!) called out here by none other than <a href="http://nondot.org/sabre/">Chris Lattner</a> (the designer of the Swift language) himself for overriding and repurposing the bit shift operator 😉 I can&rsquo;t say I disagree:</p>
<div class="twitter-wrapper"><blockquote class="twitter-tweet" lang="en"><p lang="en" dir="ltr">Interesting approach to implementing a bindings like system, but not too enthused about repurposing shift operator! <a href="https://t.co/qiNqvx6BS2">https://t.co/qiNqvx6BS2</a></p>&mdash; Chris Lattner (@clattner_llvm) <a href="https://twitter.com/clattner_llvm/status/650354422430588928">October 3, 2015</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script></div>
<p>As such, if you use this code I&rsquo;d recommend defining the <code>&lt;~</code> operator instead, or something else similarly unique!</p>
<h2 id="example">Example</h2>
<p>With the above code, you can now wire up <code>Observable</code> objects to UI elements and instance variables. Just what I needed! Here&rsquo;s an example <code>NSViewController</code> implementation with an editable <code>port</code> value:</p>
<pre><code class="language-swift">/// A view controller supporting editing of the app's config.
class PreferencesViewController: NSViewController {
// The model layer.
var configuration: ApplicationConfiguration
// The view and object supporting the &quot;controller&quot;.
@IBOutlet var portTextField: NSTextField!
var port: Observable&lt;Int&gt;
// ...
// MARK: NSViewController
override func viewDidLoad() {
super.viewDidLoad()
port.subscribe(self) { (port, _) in
// Ignore the old value, but update config with the new.
self.configuration.port = port
// You can trigger anything from here! Save to disk, etc...
// Keeps action/UI code clean.
}
// ...
// Assume `portTextFieldDidUpdate` is wired to be
// called when portTextField's value updates.
// ...
}
// MARK: Helpers
func portTextFieldDidUpdate(value: Int) {
port &lt;&lt; value
}
}
</code></pre>
<p>Download this as an <a href="https://github.com/cfdrake/swift-observables-example">Xcode Playground</a>.</p>
Getting Started with Pure Datahttps://colindrake.me/post/getting-started-with-pure-data/
Mon, 03 Aug 2015 00:00:00 +0000https://colindrake.me/post/getting-started-with-pure-data/
<p><a href="http://puredata.info/">Pure Data</a> is a cross platform and open source visual programming language allowing you to work with audio, video, and more in a simple <a href="https://en.wikipedia.org/wiki/Dataflow#Software_architecture">dataflow</a> programming model. Over the past week or so, I&rsquo;ve been exploring Pd for music and audio use and figured that a series of blog posts documenting my path of learning would be fun, helpful for others, and a good way to reinforce what I&rsquo;ve covered.</p>
<p>This is the first entry in this potential series: we&rsquo;ll be covering installation for Mac OS X, some basic dataflow programming concepts, and a few simple audio object types. In the end, we&rsquo;ll build a simple Hi-Hat patch with a few tweakable parameters.</p>
<h2 id="installation">Installation</h2>
<p>Pure Data comes in two flavors:</p>
<ul>
<li><strong>Pure Data Vanilla</strong>: A basic setup, but will cover everything you need to get started.</li>
<li><strong>Pure Data Extended</strong>: Vanilla plus some very helpful documentation, libraries, and packages.</li>
</ul>
<p>I&rsquo;d recommend installing <a href="http://puredata.info/downloads/pd-extended">Pure Data Extended</a>, as it comes with a variety of useful objects types that you would otherwise be missing. Additionally, I&rsquo;ve noticed that many times Cycling74/Pure Data forum threads will reference objects built-in to only the Extended version (the helpful <code>[sfv~]</code> for instance).</p>
<p>On Mac OS X, you can either <a href="http://puredata.info/downloads">manually download</a> these or install them with the <code>brew</code> command, if you are familiar.</p>
<pre><code>$ brew search pd
Caskroom/cask/pd-extended
Caskroom/cask/pd
</code></pre>
<p><em>Note: I haven&rsquo;t tested the Homebrew versions to see how up-to-date or functional they are. YMMV.</em></p>
<p>Before continuing, I&rsquo;ll assume you have a little bit of familiarity with the Pd <a href="http://en.flossmanuals.net/pure-data/the-interface/the-interface/">interface</a>, i.e. that you have at least explored around it before reading this article. Additionally, some experience with sound synthesis will help out, but I&rsquo;ll attempt to link out for further reading when needed.</p>
<h2 id="dataflow-model">Dataflow Model</h2>
<p>The dataflow programming model of Pure Data allows you to abstract out your ideas into sequences of composable functions that immediately react to new values produced by inputs. In the audio world, this allows you to build out your instruments with a DSP-centric model of the world.</p>
<p>Within Pure Data, this is graphically represented by objects with wires connecting their inputs to the outputs of other objects. For example, the following patch will print out the number sent as input to the print function. Upon, changing the value of the number in Play mode (Command-E to toggle between Edit/Play modes), <code>[print]</code> will automatically react and print the value to the console window.</p>
<p class="smaller-img"><img src="https://colindrake.me/images/pure-data/pd-print.png" title="WIP Hi-Hat Patch" /></p>
<p>This automatic reaction to inputs is what lets us easily build interactive synthesizers, instruments, etc. Note that these inputs may come from a variety of sources: manual user input, MIDI messages, <a href="https://en.wikipedia.org/wiki/Open_Sound_Control">OSC</a>, serial port data from an <a href="http://playground.arduino.cc/Interfacing/PD">Arduino</a>, and more. We&rsquo;ll stick with manual user input from the GUI for now.</p>
<p>Next, we&rsquo;ll cover some of the object and data types that are available for us to use and start building with.</p>
<h2 id="object-types">Object Types</h2>
<p>The following are the most basic forms of data and types that Pure Data supports:</p>
<ul>
<li><strong>Object</strong>: Think of this as similar to a function. <code>[print]</code>, <code>[+]</code>, <code>[*]</code> are examples. They may take input via pins at the top, and will produce output via pins at the bottom.</li>
<li><strong>Number Box</strong>: This data type allows you to input and edit a numerical (integer or float) value. This may be sent to an input pin of an object and will re-send whenever the value changes.</li>
<li><strong>Message Box</strong>: List of data that may be sent to an object upon click from the user. For example, you may describe a sequence of sampled amplitudes to send to an audio object. In a way, these are similar to function arguments.</li>
<li><strong>Bang</strong>: Upon clicking, this will &ldquo;trigger&rdquo; another object in realtime. May be used to begin a sound&rsquo;s audio envelope, etc.</li>
</ul>
<p>Additionally, many UI components such as sliders, VU meters, etc. are supported to make your patch more usable to other people.</p>
<p>When it comes to the audio domain, most of what we&rsquo;ll work with will be simple Object types, or functions. Sound generators, audio filters, and more are all provided as objects in Pure Data that generate potentially constantly changing signals over time. It&rsquo;s worthy of note that audio objects typically have names ending in <code>~</code>, such as <code>[phasor~]</code>.</p>
<h2 id="audio-patches">Audio Patches</h2>
<p>Let&rsquo;s start out by exploring the simplest generator available to us.</p>
<h4 id="generating-sound">Generating Sound</h4>
<p>The <code>[osc~]</code> object outputs a <a href="https://en.wikipedia.org/wiki/Pure_tone">constant (co)sine wave</a> at the specified frequency. You can provide a frequency to this object via a couple of methods:</p>
<ol>
<li><code>[osc~ 440]</code> will generate a constant 440-Hz sine wave.</li>
<li>A plain <code>[osc~]</code> with a Number Box connected to the top left pin will allow you to adjust the frequency of the wave at will. You may also use the first form with a Number Box attached, providing a default value that is user changeable.</li>
</ol>
<p><code>[dac~]</code>, the digital to analog convertor object, allows you to output a signal to the left and right output stereo channels of your computer. When you create this object, you&rsquo;ll note it has two pins: the left and right pins (obviously) map to the left and right sound output channels.</p>
<p>Try to set up the two prior configurations (one at a time) with output connected to the DAC in Pure Data. Press Command-/ to enable audio and Command-. to disable it. You should hear a pure, constantly sounding tone. Your patches should look something like these:</p>
<p class="small-img"><img src="https://colindrake.me/images/pure-data/osc.png" title="Osc Patch" /></p>
<p>Next, we&rsquo;ll try to modify (process) this audio signal before sending it to the speaker.</p>
<h4 id="processing-sound">Processing Sound</h4>
<p>The simplest case for modifying a signal is by affecting the the amplitude. The <code>[*~]</code> object will multiply N audio signals together over time. By multiplying a signal by zero, you silence it, and by multiplying it by one, you leave it unaffected. Less than one is negative gain, and greater than one is positive gain. Lets build a user-editable volume control for our oscillator by using a Number box:</p>
<p class="smaller-img"><img src="https://colindrake.me/images/pure-data/amplitude.png" title="Volume Control Patch" /></p>
<p>Be careful when entering gain values greater than one for this patch. Given that <code>[osc~]</code> produces a waveform with a peak of one for amplitude, increasing this value will cause the signal to hard-clip.</p>
<p>By connecting up some additional machinery, we can verify that the signal is being affected as we&rsquo;re expecting. Here&rsquo;s a graph showing the output of the oscillator (you can see that the amplitude looks like one-quarter of the height of the graph, per the 0.25 amplitude factor):</p>
<p class="small-img"><img src="https://colindrake.me/images/pure-data/volgraph.png" title="Volume Control Patch with Graph" /></p>
<p>It&rsquo;s actually possible for the <code>[*~]</code> object to multiply two changing audio signals as well, creating a sort of amplitude/volume modulation. Instead of passing in a constant number, let&rsquo;s replace it with another sine wave so that the volume changes periodically over time. You&rsquo;re able to see how the amplitude of the wave changes from the graph:</p>
<p class="small-img"><img src="https://colindrake.me/images/pure-data/volmod.png" title="Volume Control Patch with Graph" /></p>
<p>Feel free to experiment with the frequency at which the volume is modulating. By setting the frequency of the modulation to a frequency in the audible spectrum (for example, 880), you can achieve what&rsquo;s known to musicians as a <a href="https://en.wikipedia.org/wiki/Tremolo">tremelo</a>.</p>
<p>Here&rsquo;s a fun exercise: Create a <a href="https://en.wikipedia.org/wiki/Vibrato">vibrato</a> effect by <em>ever so slightly</em> modulating the frequency, not amplitude, of the original oscillator over time at an audible speed.</p>
<h2 id="sample-patch">Sample Patch</h2>
<p>Next, we&rsquo;re going to take what we now know and turn it into a playable patch. This section is going to cover how to create a classic white-noise based <a href="https://en.wikipedia.org/wiki/Hi-hat">Hi-Hat</a> instrument.</p>
<p>We&rsquo;re going to start with the <code>[noise~]</code> object, an audio generator that outputs <a href="https://en.wikipedia.org/wiki/White_noise">white noise</a>, a random signal with each area of the audible spectrum equally represented. This harsh sound actually can form the basis of many other percussion instruments, such as a snare drum. If you connect <code>[noise~]</code> directly to <code>[dac~]</code>, you&rsquo;ll hear a sound that&rsquo;s quite harsh, so we&rsquo;re going to filter out some frequencies using a <a href="https://en.wikipedia.org/wiki/Band-pass_filter">bandpass filter</a>.</p>
<p class="smaller-img"><img src="https://colindrake.me/images/pure-data/noise.png" title="WIP Hi-Hat Patch" /></p>
<p>The <code>[vcf~]</code> (voltage-controlled filter) object takes three parameters: an input audio signal to filter, the base frequency at which the filtering will occur, and a Q (resonance) value that determines how &ldquo;far out&rdquo; the filtering will reach. Go ahead and connect the output of <code>[noise~]</code> to the input of a new <code>[vcf~]</code>, with number boxes inputting 1.1kHZ (11000) and 5 for frequency and Q, respectively. Connecting this to <code>[dac~]</code> will produce something a <em>little</em> less irritating, however it&rsquo;s still just a constantly playing sound, not an instrument. What we&rsquo;ve done here is setup a tight <a href="http://en.flossmanuals.net/pure-data/ch027_filters/">filter</a> that only allows a specific range of high frequencies from the original signal to come through, emulating the high pitch of a real hi-hat.</p>
<p class="smaller-img"><img src="https://colindrake.me/images/pure-data/noise-filtered.png" title="WIP Hi-Hat Patch" /></p>
<p>An <a href="https://en.wikipedia.org/wiki/Synthesizer#ADSR_envelope">ASDR envelope</a> can shape a waveform&rsquo;s amplitude over a period of time, which is what we&rsquo;ll want to be able to trigger. By triggering changes in the amplitude (which should be at zero normally) on a button press, we can get the distinctive short, percussive &ldquo;blip&rdquo; sound of a Hi-Hat from this filtered noise. The <code>[line~]</code> object is a simple way to accomplish this in Pure Data.</p>
<p><code>[line~]</code> takes a list of amplitudes and times and generates a linearly interpolated audio waveform according to the values passed in. For example, if you use a message box to feed in <code>1, 0.7 40, 0 30</code> to <code>[line~]</code>, when triggered it will generate a waveform that initially has an amplitude of 1, moves down to 0.7 after 40ms, and falls back down to 0 after 30ms. By multiplying (<code>*~</code>) this with our white noise audio signal, we can create a new, snappy signal that varies over time when the envelope is triggered (via a click in Play Mode), and otherwise stays quiet. This is the basis of creating &ldquo;playable&rdquo; instruments within Pure Data.</p>
<p>For bonus points, you can hook up a Button to the ASDR message box, providing a more user-friendly (in a way) mechanism to toggle the sound. Clicking the button in Play mode will send a <code>bang</code> to the message box, triggering the envelope and temporarily lifting the amplitude.</p>
<p>If you&rsquo;ve followed the above, you&rsquo;ll hopefully have something similar to the following:</p>
<p class="small"><img src="https://colindrake.me/images/pure-data/hihat.png" title="Hi-Hat Patch" /></p>
<p><audio controls>
<source src="https://colindrake.me/audio/pure-data/HiHat.wav" type="audio/wav">
Your browser does not support the audio element.
</audio></p>
<p>It turns out that many classic drum synthesizers create sounds in similar (but perhaps more complex) ways. Bass drums can be synthesized using low-frequency sine waves, and snares can be also be created by combining a low-frequency sine wave mixed with some white noise to simulate the &ldquo;snap&rdquo;. Once you get started here, the possibilities are pretty much endless, only limited by your imagination.</p>
<h2 id="further-tips-and-notes">Further Tips and Notes</h2>
<h4 id="midimock">MidiMock</h4>
<p>I&rsquo;ve found it helpful to download <a href="https://itunes.apple.com/us/app/midi-mock/id438240325?mt=12">MidiMock</a> from the App Store, and create a setup similar to the Input Stage of the demo synthesizer from the Pd tutorial on <a href="http://en.flossmanuals.net/pure-data/ch032_4-stage-sequencer/">FLOSS Manuals</a> to explore your patch more naturally with an onscreen piano.</p>
<p>Check the &ldquo;Mac OS X&rdquo; section of the Pure Data documentation on <a href="https://puredata.info/docs/faq/midiinput">MIDI Input</a> to get your computer wired properly.</p>
<h4 id="max-msp-and-max4live">Max/MSP and Max4Live</h4>
<p><a href="https://cycling74.com/">Max/MSP</a> and <a href="https://www.ableton.com/en/live/max-for-live/">Max4Live</a> are two excellent products by Cycling74 and Ableton. The former is a commercial Pure Data-like programming environment with an excellent GUI, documentation, and more, and the latter is a collaboration between C74 and Ableton allowing you to seamlessly interact with Max patches as an Ableton device in your tracks. I would highly recommend both.</p>
<h4 id="other-resources">Other Resources</h4>
<p><a href="http://en.flossmanuals.net/pure-data/index/">FLOSS Manuals</a> provides an excellent, deep guide to getting started using Pure Data. Additionally, they also maintain a glossary of available <a href="http://en.flossmanuals.net/pure-data/list-of-objects/introduction/">objects</a>.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Overall, I&rsquo;ve found Pure Data/Max wonderful to work with, and it&rsquo;s nice to have the ability to open up some M4L patches and see what&rsquo;s going on under the covers. Additionally, just having the ability to quickly mock up an instrument idea without having to learn a VST library, etc. is phenomenal. Hopefully this guide was able to transfer some of my excitement over to you! My plan for the next article in this series is to build a MIDI-capable, monophonic, two-oscillator synthesizer.</p>
Vim for iOS Development: Setting up ctagshttps://colindrake.me/post/vim-for-ios-development-setting-up-ctags/
Thu, 25 Jun 2015 00:00:00 +0000https://colindrake.me/post/vim-for-ios-development-setting-up-ctags/
<p>With projects like <a href="https://github.com/msanders/cocoa.vim">cocoa.vim</a> and <a href="https://github.com/eraserhd/vim-ios">vim-ios</a>, iOS developers have a variety of helpful tools when deciding to build an app in Vim. However, with implementation files, header files, and the numerous set of frameworks we use to build apps, I&rsquo;ve always found auto-completion and code navigation to be particularly difficult when writing Objective-C, especially when dealing with larger projects.</p>
<p>To remedy this, I&rsquo;ve started using a very old tool, called <a href="https://en.wikipedia.org/wiki/Ctags">ctags</a>. ctags is able to parse source code and index methods, functions, classes, etc. for quick access later. Modern versions of Vim are built with ctags support by default, so this makes for a very easy integration.</p>
<p>Let&rsquo;s get started.</p>
<h2 id="setup">Setup</h2>
<p>Luckily for us, Mac OS X comes with ctags installed by default &hellip;but unfortunately for us, this version (despite what the documentation says) doesn&rsquo;t support Objective-C. We&rsquo;ll have to use <a href="http://brew.sh">Homebrew</a> to install a newer version. Start by executing the following to install the latest and greatest version of ctags:</p>
<pre><code>$ brew install ctags --HEAD
</code></pre>
<p>Next, let&rsquo;s define a few default flags to always use when running ctags. These can be specified in a <code>.ctags</code> file in your home directory:</p>
<pre><code>$ cat ~/.ctags
--recurse=yes
--tag-relative=yes
--exclude=.git
</code></pre>
<p>Finally, we&rsquo;ll set up a bash alias to make our lives easier when running ctags. Unfortunately, ctags assumes all <code>.m</code> files are Matlab files, not Objective-C implementation files, so we&rsquo;ll create a new command we can use that will ensure <code>.m</code> files are treated in the way that we need. Go ahead and add the following line of code to the <code>.bash_profile</code> file in your home directory:</p>
<pre><code>alias ctags-objc=&quot;ctags --languages=objectivec --langmap=objectivec:.h.m&quot;
</code></pre>
<p>At this point, we&rsquo;re ready to start processing our code.</p>
<h2 id="indexing-your-project">Indexing Your Project</h2>
<p>If you&rsquo;ve been following along word-for-word so far, you can simply run the following command from your project&rsquo;s root directory to create a local tag index:</p>
<pre><code>$ ctags-objc
</code></pre>
<p>This will create a file called <code>tags</code> that Vim is smart enough to read. Now, if you open Vim in this directory, you should be able to start jumping through your project&rsquo;s source code already. Try the following command from within Vim:</p>
<pre><code>:tag &lt;ClassFromYourProject&gt;
</code></pre>
<p>You should be taken directly to the <code>@interface</code> declaration for your class.</p>
<p>This is a great start, but oftentimes as iOS developers we find that we need to take a deeper look and check out the headers or documentation for an Apple Framework instead of our own code.</p>
<h2 id="indexing-system-headers">Indexing System Headers</h2>
<p>Let&rsquo;s begin by generating tags for some of the more commonly-used Apple Frameworks. We&rsquo;ll start by opening up the folder containing all of the iOS and Mac OS X frameworks and peering inside:</p>
<pre><code>$ cd /System/Library/Frameworks
$ ls
AGL.framework
AVFoundation.framework
AVKit.framework
Accelerate.framework
Accounts.framework
AddressBook.framework
AppKit.framework
AppKitScripting.framework
AppleScriptKit.framework
AppleScriptObjC.framework
ApplicationServices.framework
AudioToolbox.framework
...
</code></pre>
<p>Here, every Framework that we can include and link to has a corresponding <code>*.framework</code> folder with parseable header files inside. We can use these files to generate a global tag list for all of the Frameworks we use on a daily basis. I&rsquo;d start with just a few Frameworks - a tag file containing all of them gets pretty slow to search, at least on my system.</p>
<p>By using the <code>--append</code> and <code>-f</code> flags in ctags, we can output and append tags to a file of our choice. I like to keep the <a href="https://developer.apple.com/library/ios/documentation/MapKit/Reference/MapKit_Framework_Reference/">MapKit</a>, <a href="https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CoreLocation_Framework/">CoreLocation</a>, and <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/ObjC_classic/index.html#//apple_ref/doc/uid/20001091">Foundation</a> frameworks in my database. Anytime you want to add a Framework to your tags database, a command like the following will work:</p>
<pre><code>$ cd CoreData.framework/
$ ctags-objc --append -f ~/Documents/global-objc-tags
</code></pre>
<p>Do this for a couple of your most often-used Frameworks to get a good base.</p>
<p>Next, we&rsquo;ll add tags for <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKit_Framework/">UIKit</a>. The UIKit frameworks are stored on a per-iOS SDK basis deep inside of the <code>Xcode.app</code> folder, so we&rsquo;ll need to start by navigating there (make sure to substitute the <code>8.3</code> in the next command for the iOS version you want):</p>
<pre><code>cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.3.sdk/System/Library/Frameworks/UIKit.framework/Headers
</code></pre>
<p>Now, we can just run the same command as above to append to our global tag list:</p>
<pre><code>$ ctags-objc --append -f ~/Documents/global-objc-tags
</code></pre>
<p>At this point we&rsquo;ve got two tag databases ready &mdash; one for our project, and one for the Frameworks we intend to use. Let&rsquo;s jump back over to Vim.</p>
<h2 id="vim-configurations">Vim Configurations</h2>
<p>As we saw above, Vim was smart enough to find and automatically take a look at the local <code>tags</code> file to process our codebase. However, we need to tell Vim about the global Objective-C tags we just generated. Using an <a href="http://vimdoc.sourceforge.net/htmldoc/autocmd.html">autocmd</a> will be a great solution to make this database searchable only when we&rsquo;re in an Objective-C file.</p>
<p>Open your <code>.vimrc</code> file and add the following:</p>
<pre><code>if has(&quot;autocmd&quot;)
autocmd BufNewFile,BufRead *.h,*.m set tags+=~/Documents/global-objc-tags
endif
</code></pre>
<p>Now, we&rsquo;ll be able to automatically search our local and global tag databases for any project that we open.</p>
<h2 id="basic-usage">Basic Usage</h2>
<p>Now that we&rsquo;ve built our full tag archive, we can start querying and jumping around our (and Apple&rsquo;s) codebases. The following commands and key combinations will form a good start for poking around.</p>
<pre><code>:tag &lt;Class, Method, Protocol, Type...&gt;
</code></pre>
<p>This command will let you go to the definition of the given tag (try it with an Apple Framework class this time &mdash; <code>UITableViewDataSource</code> always has details to the method signatures that I forget). Once you&rsquo;re done, you may use <code>&lt;c-t&gt;</code> or <code>:bd</code> to leave.</p>
<pre><code>:tselect &lt;...&gt;
</code></pre>
<p>This form is the same as the above <code>tag</code> except that if more than one match is found, a menu is presented where you may choose which definition you want to jump to. Additionally, this is a good command to use the <code>/</code> search modifier with. For example, to find tags beginning with <code>NSAss</code> the <code>:tselect /NSAss</code> command may be issued.</p>
<pre><code>:tags
</code></pre>
<p>This command provides a menu of previously-found tags from your current session. Simply type the number of the tag you want to view and press Enter to jump to it again.</p>
<pre><code>&lt;c-]&gt;
</code></pre>
<p>This normal mode command will perform a <code>:tag</code> command for the currently selected word. Try putting your cursor within a class name and executing this.</p>
<pre><code>&lt;c-x&gt; &lt;c-p&gt;
</code></pre>
<p>In insert mode, this will provide an autocompletion menu based on tag file contents. The arrow keys navigate the list, <code>&lt;c-y&gt;</code> accepts and inserts, and <code>&lt;c-e&gt;</code> cancels the prompt.</p>
<pre><code>:CtrlPTag
</code></pre>
<p>Finally, if you&rsquo;re a <a href="https://github.com/kien/ctrlp.vim">ctrlp.vim</a> user (which you should be!), you can use this command to search tags in realtime. I use this often enough that I&rsquo;ve bound it to <code>&lt;leader&gt;t</code>.</p>
<h2 id="conclusion">Conclusion</h2>
<p>You should now have a basic working knowledge of how to generate and view tags for Objective-C projects in Vim. However, there are many, many more commands available to aid in navigating your source with ctags. Give the <code>:help tags</code> page a read for more information.</p>
<p>Additionally, it&rsquo;s worth noting that there are a wide variety of user-contributed plugins that can aid your usage of ctags. Check out <a href="https://github.com/majutsushi/tagbar">TagBar</a> and <a href="https://github.com/xolox/vim-easytags">EasyTags</a> for viewing and maintaining your tags, respectively.</p>
<p>Shameless plug: If you&rsquo;re a Vim user and iOS developer, you also may enjoy my <a href="https://github.com/cfdrake/vim-carthage">vim-carthage</a> plugin. Give it a shot!</p>
k-means Clustering in Rubyhttps://colindrake.me/post/k-means-clustering-in-ruby/
Sat, 28 May 2011 00:00:00 +0000https://colindrake.me/post/k-means-clustering-in-ruby/
<p>Clustering algorithms play a very important role in many modern web applications that feature machine learning. This article will introduce you to one of the simplest techniques for the unsupervised grouping of related objects, or clustering.</p>
<p>If you&rsquo;re at all interested in automated data grouping or sorting you should at least be familiar with one or two types of <a href="http://en.wikipedia.org/wiki/Cluster_analysis">these algorithms</a> (in addition to more advanced Machine Learning topics, but this serves a good start). In this article, I&rsquo;m going to go through the process of implementing a very simple Ruby program that can group a set of 2D coordinates into clusters, where each group is composed of a center point, and all of the data points closest to it.</p>
<p>The algorithm that we&rsquo;ll be using to accomplish this task is a simple one; <a href="http://en.wikipedia.org/wiki/K-means_clustering">k-means clustering</a> will give us the behavior that we want with little fuss.</p>
<h2 id="overview">Overview</h2>
<p>Our k-means clustering algorithm takes in, as input, a set of points in the 2-dimensional plane. As output, the points will be grouped into k clusters, where k is an integer specified by the user. Unfortunately, the algorithm can&rsquo;t decide how many groups there are by itself without more complication, so k must be given. Nonetheless, I&rsquo;ll describe the algorithm below:</p>
<ol>
<li>Start by choosing k random points within the dataset&rsquo;s range as an initial guess for the positions of all the clusters. These points form the centroid point of all the clusters. All distances to other points will be measured from here.</li>
<li>For each point in the input data, assign it to the cluster that it is nearest to. After this step, each cluster will somehow be associated with a set of nearby points.</li>
<li>For each cluster, go through the set of associated datapoints and calculate the average among them. This will give a new centroid point that is directly in the center of all of the member points.</li>
<li>If the clusters didn&rsquo;t move from their previous locations after recentering, or if they all move less than a certain delta value, return the k clusters and their associated points. Otherwise, go back to Step 2 after deassociating all of the associated points with their cluster. This lets the algorithm start fresh, but with more accurate centroid points.</li>
</ol>
<h2 id="implementation">Implementation</h2>
<p>To begin with, we&rsquo;ll need a class to store the points to be clustered by the algorithm. Essentially, we just need a <code>Point</code> class to hold <code>x</code> and <code>y</code> values. This is implemented below (I won&rsquo;t insult your intelligence trying to explain it):</p>
<pre><code class="language-ruby">class Point
attr_accessor :x, :y
# Constructor that takes in an x,y coordinate
def initialize(x,y)
@x = x
@y = y
end
# Calculates the distance to Point p
def dist_to(p)
xs = (@x - p.x)**2
ys = (@y - p.y)**2
return Math::sqrt(xs + ys)
end
# Return a String representation of the object
def to_s
return &quot;(#{@x}, #{@y})&quot;
end
end
</code></pre>
<p>Next, there has to be a class to hold clusters of data. As the algorithm described, clusters have groups of member points and a center point (not necessarily in the dataset) associated with them. This corresponds to two instance variables: <code>@points</code>, a list of <code>Point</code>s, and <code>@center</code>, a single <code>Point</code>. Additionally, there needs to be a way for <code>Cluster</code>s to update by averaging their member points. This is implemented in <code>recenter!</code>.</p>
<pre><code class="language-ruby">class Cluster
attr_accessor :center, :points
# Constructor with a starting centerpoint
def initialize(center)
@center = center
@points = []
end
# Recenters the centroid and removes associated points
def recenter!
xa = ya = 0
old_center = @center
# Sum up all x/y coords
@points.each do |point|
xa += point.x
ya += point.y
end
# Average out data
xa /= points.length
ya /= points.length
# Reset center and return distance moved
@center = Point.new(xa, ya)
return old_center.dist_to(center)
end
end
</code></pre>
<p>Finally, the algorithm itself needs to be implemented.</p>
<p>The parameters to the <code>kmeans</code> function are a dataset (list of <code>Point</code>s), data, number of clusters to find, k, and an optional halting delta, delta. The algorithm will halt when all of the clusters are updated by a value less than delta on an iteration.</p>
<pre><code class="language-ruby">def kmeans(data, k, delta=0.001)
</code></pre>
<p>Initially, the algorithm needs to choose the starting guesses for cluster centers. It does this by generating <code>k</code> <code>Cluster</code> objects, and assigning them a center from a randomly selected <code>Point</code> from data.</p>
<pre><code class="language-ruby">clusters = []
# Assign intial values for all clusters
(1..k).each do |point|
index = (data.length * rand).to_i
rand_point = data[index]
c = Cluster.new(rand_point)
clusters.push c
end
# ... code to follow below ...
</code></pre>
<p>Next is the main meat of the algorithm. The code loops indefinitely and assigns points to clusters by finding, for each point, which cluster center is the closest. This assignment will be updated, and become more accurate, each iteration of the loop while the clusters recenter.</p>
<pre><code class="language-ruby"># Loop
while true
# Assign points to clusters
data.each do |point|
min_dist = +INFINITY
min_cluster = nil
# Find the closest cluster
clusters.each do |cluster|
dist = point.dist_to(cluster.center)
if dist &lt; min_dist
min_dist = dist
min_cluster = cluster
end
end
# Add to closest cluster
min_cluster.points.push point
end
# ... code from below ...
end # end of while loop
</code></pre>
<p>Finally, in the code at the bottom of the while loop, we recalculate the centers of the clusters for the next iteration. This is done by calling <code>recenter!</code> on all of the <code>Cluster</code> objects. Additionally, we do some delta checking because we need to leave the loop eventually. By keeping track of the most that any <code>Cluster</code> was updated, we can compare it against delta to see if all of the <code>Cluster</code>s were below the input <code>delta</code>. If the delta was hit, the algorithm terminates, returning a list of all of the <code>Cluster</code>s found in the dataset.</p>
<pre><code class="language-ruby"> # Loop
while true
# ... code from above ...
# Check deltas
max_delta = -INFINITY
clusters.each do |cluster|
dist_moved = cluster.recenter!
# Get largest delta
if dist_moved &gt; max_delta
max_delta = dist_moved
end
end
# Check exit condition
if max_delta &lt; delta
return clusters
end
# Reset points for the next iteration
clusters.each do |cluster|
cluster.points = []
end
end # end of while
end # end of kmeans()
</code></pre>
<p>Overall, k-means clustering is a pretty simple algorithm, as you can see from above. The entire source file, along with glue/integration code, is available <a href="https://gist.github.com/cfdrake/995804#file-kmeans-rb">here</a> to download.</p>
<h2 id="conclusion">Conclusion</h2>
<p>While this is a very simple example, note that the x and y axis can be whatever you want them to be (latitude/longitude of households, baseball stats, etc). You could even (easily) extend the program to support 3 or more parameters. Thus, k-means clustering can actually be a powerful tool for grouping real-world datasets, despite the apparent simplicity.</p>
A Genetic Algorithm in Pythonhttps://colindrake.me/post/a-genetic-algorithm-in-python/
Sun, 01 May 2011 00:00:00 +0000https://colindrake.me/post/a-genetic-algorithm-in-python/
<p>In the spirit of taking biology and artificial intelligence this quarter, I decided to try to throw together a program combining the concepts of the two. I ended up writing a python script that performs &ldquo;natural selection&rdquo; of a population of initially random strings towards an ideal &ldquo;Hello, World&rdquo; string, implementing a really basic genetic algorithm.</p>
<h2 id="the-algorithm-of-evolution">The Algorithm of Evolution</h2>
<p>Before we look at the code, I&rsquo;m going to go over the basics of how the &ldquo;algorithms&rdquo; of <a href="http://en.wikipedia.org/wiki/Evolution">evolution</a> and genetics work (or at least the simple model of it that we&rsquo;ll use).</p>
<p>To begin, we generate a random initial population of organisms. This will serve as the set of organisms from which we will evolve better, more-fit forms. Next, we implement natural selection by iterating over the following steps for each generation that we would like to simulate:</p>
<ol>
<li>Assign a fitness value to each organism in the population. This should reflect how well the individual is suited to survival in the current environment.</li>
<li>Map the fitness value to a probability of being picked to survive into the next generation. Individuals with more fitness should be more likely to be selected to survive.</li>
<li>Create a &ldquo;working population&rdquo; by choosing individuals from the population with the chance of each being chosen based on their calculated probabilities. This means that individuals can be chosen many times, once, or even not at all.</li>
<li>Create &ldquo;offspring&rdquo; between each pair of two organisms from the working population by combining DNA from each parent. In our program, each set of parents will produce 2 offspring to drive the new generation, keeping the population levels constant.</li>
<li>Randomly mutate each individual&rsquo;s genes slightly. This ensures genetic diversity remains within the population.</li>
</ol>
<p>At the end of this process (once a certain fitness or generation cap has been hit), the resulting population should be significantly more fit to the environment than in the initial population.</p>
<p><strong>However:</strong> while this process can produce optimized solutions difficult for humans to design from the start, it&rsquo;s important to note that nothing in this algorithm guarantees a <em>perfectly</em> fit organism in the end.</p>
<h2 id="implementation-in-python">Implementation in Python</h2>
<p>Now that we know how the algorithm itself will work, we can get to the code. You can either interpret this as an experiment in literate programming or as me being too lazy to write any explanation at all! Here&rsquo;s the (at least highly annotated) source:</p>
<pre><code class="language-python">&quot;&quot;&quot;
helloevolve.py implements a genetic algorithm that starts with a base
population of randomly generated strings, iterates over a certain number of
generations while implementing 'natural selection', and prints out the most fit
string.
The parameters of the simulation can be changed by modifying one of the many
global variables. To change the &quot;most fit&quot; string, modify OPTIMAL. POP_SIZE
controls the size of each generation, and GENERATIONS is the amount of
generations that the simulation will loop through before returning the fittest
string.
This program subject to the terms of the BSD license listed below.
Copyright (c) 2011 Colin Drake
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the &quot;Software&quot;), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
&quot;&quot;&quot;
import random
#
# Global variables
# Setup optimal string and GA input variables.
#
OPTIMAL = &quot;Hello, World&quot;
DNA_SIZE = len(OPTIMAL)
POP_SIZE = 20
GENERATIONS = 5000
#
# Helper functions
# These are used as support, but aren't direct GA-specific functions.
#
def weighted_choice(items):
&quot;&quot;&quot;
Chooses a random element from items, where items is a list of tuples in
the form (item, weight). weight determines the probability of choosing its
respective item. Note: this function is borrowed from ActiveState Recipes.
&quot;&quot;&quot;
weight_total = sum((item[1] for item in items))
n = random.uniform(0, weight_total)
for item, weight in items:
if n &lt; weight:
return item
n = n - weight
return item
def random_char():
&quot;&quot;&quot;
Return a random character between ASCII 32 and 126 (i.e. spaces, symbols,
letters, and digits). All characters returned will be nicely printable.
&quot;&quot;&quot;
return chr(int(random.randrange(32, 126, 1)))
def random_population():
&quot;&quot;&quot;
Return a list of POP_SIZE individuals, each randomly generated via iterating
DNA_SIZE times to generate a string of random characters with random_char().
&quot;&quot;&quot;
pop = []
for i in xrange(POP_SIZE):
dna = &quot;&quot;
for c in xrange(DNA_SIZE):
dna += random_char()
pop.append(dna)
return pop
#
# GA functions
# These make up the bulk of the actual GA algorithm.
#
def fitness(dna):
&quot;&quot;&quot;
For each gene in the DNA, this function calculates the difference between
it and the character in the same position in the OPTIMAL string. These values
are summed and then returned.
&quot;&quot;&quot;
fitness = 0
for c in xrange(DNA_SIZE):
fitness += abs(ord(dna[c]) - ord(OPTIMAL[c]))
return fitness
def mutate(dna):
&quot;&quot;&quot;
For each gene in the DNA, there is a 1/mutation_chance chance that it will be
switched out with a random character. This ensures diversity in the
population, and ensures that is difficult to get stuck in local minima.
&quot;&quot;&quot;
dna_out = &quot;&quot;
mutation_chance = 100
for c in xrange(DNA_SIZE):
if int(random.random()*mutation_chance) == 1:
dna_out += random_char()
else:
dna_out += dna[c]
return dna_out
def crossover(dna1, dna2):
&quot;&quot;&quot;
Slices both dna1 and dna2 into two parts at a random index within their
length and merges them. Both keep their initial sublist up to the crossover
index, but their ends are swapped.
&quot;&quot;&quot;
pos = int(random.random()*DNA_SIZE)
return (dna1[:pos]+dna2[pos:], dna2[:pos]+dna1[pos:])
#
# Main driver
# Generate a population and simulate GENERATIONS generations.
#
if __name__ == &quot;__main__&quot;:
# Generate initial population. This will create a list of POP_SIZE strings,
# each initialized to a sequence of random characters.
population = random_population()
# Simulate all of the generations.
for generation in xrange(GENERATIONS):
print &quot;Generation %s... Random sample: '%s'&quot; % (generation, population[0])
weighted_population = []
# Add individuals and their respective fitness levels to the weighted
# population list. This will be used to pull out individuals via certain
# probabilities during the selection phase. Then, reset the population list
# so we can repopulate it after selection.
for individual in population:
fitness_val = fitness(individual)
# Generate the (individual,fitness) pair, taking in account whether or
# not we will accidently divide by zero.
if fitness_val == 0:
pair = (individual, 1.0)
else:
pair = (individual, 1.0/fitness_val)
weighted_population.append(pair)
population = []
# Select two random individuals, based on their fitness probabilites, cross
# their genes over at a random point, mutate them, and add them back to the
# population for the next iteration.
for _ in xrange(POP_SIZE/2):
# Selection
ind1 = weighted_choice(weighted_population)
ind2 = weighted_choice(weighted_population)
# Crossover
ind1, ind2 = crossover(ind1, ind2)
# Mutate and add back into the population.
population.append(mutate(ind1))
population.append(mutate(ind2))
# Display the highest-ranked string after all generations have been iterated
# over. This will be the closest string to the OPTIMAL string, meaning it
# will have the smallest fitness value. Finally, exit the program.
fittest_string = population[0]
minimum_fitness = fitness(population[0])
for individual in population:
ind_fitness = fitness(individual)
if ind_fitness &lt;= minimum_fitness:
fittest_string = individual
minimum_fitness = ind_fitness
print &quot;Fittest String: %s&quot; % fittest_string
exit(0)
</code></pre>
<h2 id="result">Result</h2>
<p>Here&rsquo;s an example of output generated by the script:</p>
<pre><code>Generation 0... Random sample: 'RE}36#qP'_u%'
Generation 1... Random sample: '{z?.;7CEYy#g'
Generation 2... Random sample: '0/5^aGk]yx1='
Generation 3... Random sample: 'lP6]`HBUS|1='
Generation 4... Random sample: 'l,iK%6{;&lt;|Lk'
Generation 5... Random sample: '0/5^aGk]y][j'
...
...
Generation 4999... Random sample: 'HeClo, World'
Fittest String: Hello, World
</code></pre>