<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
  <title>Gideon de Kok on scriptogr.am</title>
  <link>http://gideondk.com</link>
  <description>Next-gen front- and backend development</description>
  <pubDate>2012</pubDate>
 
  <item>
    <title>Additions to Scaliak driver</title>
    <pubDate>Sat, 28 Apr 2012 01:06:00 -0400</pubDate>
    <link>http://gideondk.com/post/additions-to-scaliak-driver</link>
    <guid>http://gideondk.com/post/additions-to-scaliak-driver</guid>     
    <description><![CDATA[<p>I've started the merge of my (private) Scala -> Riak client into the Scaliak client made by the awesome guys at StackMob:</p>

<p><a href="https://github.com/gideondk/scaliak/commits/master">https://github.com/gideondk/scaliak/commits/master</a></p>

<p>The latest commits add preliminary support for MapReduce to the Scaliak client.</p>

<p>MapReduce jobs are modelled into a simple case class (<code>MapReduceJob</code>) which together with the MapReduceBuilder abstracts the Riak MapReduce job into a much simpler flow.</p>

<p>The MapReduceJob class takes a <code>MapReducePhasePipe</code> as a argument and has optional arguments for a <em>Search Query</em>, <em>Bucket</em>, <em>Object Map (Bucket => KeySet)</em>, <em>Binary Index</em> or <em>Int Index</em> with a <code>Int</code> or a <code>Range</code> as argument.</p>

<p>The MapReducePhasePipe makes adding phases easier: By using the <code>|*</code> or <code>|-</code> method, you can add a new Map or Reduce phase to your job. For instance:</p>

<p><code>MapReducePhasePipe(mapValuesToJson(false) |- sort(field, sortDESC))</code></p>

<p>Both <code>mapValuesToJson</code> as the <code>sort</code> function, are named or anonymous javascript functions stored in <code>MapReduceFunctions.scala</code>.  I will add more common MapReduce functions into this object for easy job building.</p>

<p>Once a job has been created, the job can be send to the <code>mapReduce</code> function in the <code>ScaliakBucket</code> class, which uses the <code>MapReduceBuilder</code> to create a JSON representation of the job and sends in to the client.</p>

<p>Next to the added MapReduce functionality, I've added a <code>DomainObjectWrapper</code>, which makes DomainObjects easier to use.</p>

<p>To use it, simply create a case class extending the <code>DomainObject</code> class and create a companion object:</p>

<p><code>object Photo extends DomainObjectWrapper[Photo](classOf[Photo])</code></p>

<p>Through this companion class, you can fetch, store and delete objects in several ways, classes are automatically (de)serialized to JSON and if you use annotations (RiakKey and RiakIndex for instance). Keys and indexes are automatically stored and retrieved.</p>

<p>The code is untested and still in progress, but hope to finish most parts and the end of next week!</p>
]]></description>
  </item>
 
  <item>
    <title>Stratifiedjs</title>
    <pubDate>Wed, 01 Feb 2012 01:09:00 -0500</pubDate>
    <link>http://gideondk.com/post/stratifiedjs</link>
    <guid>http://gideondk.com/post/stratifiedjs</guid>     
    <description><![CDATA[<blockquote>
  <p>JavaScript + structured concurrency.
  Bring structure to your JavaScript based asynchronous applications. 
  A small but powerful extension to the language of the web.</p>
</blockquote>

<p><a href="http://onilabs.com/stratifiedjs">http://onilabs.com/stratifiedjs</a></p>
]]></description>
  </item>
 
  <item>
    <title>First Conj 2011 Videos Available</title>
    <pubDate>Tue, 31 Jan 2012 03:59:00 -0500</pubDate>
    <link>http://gideondk.com/post/first-conj-2011-videos-available</link>
    <guid>http://gideondk.com/post/first-conj-2011-videos-available</guid>     
    <description><![CDATA[<p><a href="http://clojure.com/blog/2012/01/31/first-conj-2011-videos-available.html">http://clojure.com/blog/2012/01/31/first-conj-2011-videos-available.html</a></p>
]]></description>
  </item>
 
  <item>
    <title>Iteratees, Enumerators, Enumeratees and handling data streams reactively</title>
    <pubDate>Tue, 31 Jan 2012 03:49:00 -0500</pubDate>
    <link>http://gideondk.com/post/iteratees-enumerators-enumeratees-and-handling-data-streams-reactively</link>
    <guid>http://gideondk.com/post/iteratees-enumerators-enumeratees-and-handling-data-streams-reactively</guid>     
    <description><![CDATA[<p>As some of you may have noticed ;-) , the combination of Scala, Play and Akka is now my preferred platform for creating performant, realtime and scalable web architectures. Until now, only little was documented about one of the most powerful features of Play, but they just updated the Wiki!</p>

<blockquote>
  <p>Progressive Stream Processing and manipulation is an important task in modern Web Programming, starting from chunked upload/download to Live Data Streams consumption, creation, composition and publishing through different technologies including Comet and WebSockets.</p>
</blockquote>

<p>Read more about it: <a href="https://github.com/playframework/Play20/wiki/Iteratees">Wiki link</a></p>
]]></description>
  </item>
 
  <item>
    <title>Actors on iOS: Objective-C implementation of asynchronous inter-thread message passing</title>
    <pubDate>Tue, 24 Jan 2012 00:00:00 -0500</pubDate>
    <link>http://gideondk.com/post/actors-on-ios-objective-c-implementation-of-asynchronous-inter-thread-message-passing</link>
    <guid>http://gideondk.com/post/actors-on-ios-objective-c-implementation-of-asynchronous-inter-thread-message-passing</guid>     
    <description><![CDATA[<blockquote>
  <p>ActorKit provides an Objective-C implementation of asynchronous inter-thread message passing.</p>
  
  <p>The purpose of ActorKit is to facilitate the implementation of concurrent software on both the desktop (Mac OS X) and embedded devices (iPhone OS). On the iPhone, thread-based concurrency is a critical tool in achieving high interface responsiveness while implementing long-running and potentially computationally expensive background processing. On Mac OS X, thread-based concurrency opens the door to leveraging the power of increasingly prevalent multi-core desktop computers.</p>
</blockquote>

<p><a href="http://code.google.com/p/plactorkit/">Try it now!</a></p>
]]></description>
  </item>
 
  <item>
    <title>WAT!</title>
    <pubDate>Tue, 24 Jan 2012 00:00:00 -0500</pubDate>
    <link>http://gideondk.com/post/wat</link>
    <guid>http://gideondk.com/post/wat</guid>     
    <description><![CDATA[<p><a href="http://destroyallsoftware.com/talks/wat">Why programming languages are weird... WAT!</a></p>
]]></description>
  </item>
 
  <item>
    <title>Need a implementation of a Waveform generator in Java?</title>
    <pubDate>Mon, 23 Jan 2012 00:00:00 -0500</pubDate>
    <link>http://gideondk.com/post/need-a-implementation-of-a-waveform-generator-in-java</link>
    <guid>http://gideondk.com/post/need-a-implementation-of-a-waveform-generator-in-java</guid>     
    <description><![CDATA[<p>Most implementations of (Java/Scala) waveform generators you find on the web will have your server RAM for lunch.</p>

<p>Needed something leaner myself and if you need one too, here you go:</p>

<pre class="prettyprint"><code>import javax.sound.sampled.AudioInputStream

class Bucket {
    var min = 0;
    var max = 0;

    def add(x: Int) = {
      if (x &gt; max)
        max = x
      if (x &lt; min)
        min = x
    }

    def peak: Int = {
      List(Math.abs(min), Math.abs(max)).max
    }
}

object AudioStatter {

    val NUM_BITS_PER_BYTE = 8

    def numberOfChannels(audioInputStream: AudioInputStream): Int = 
      (audioInputStream.getFormat().getFrameSize() /  (audioInputStream.getFormat().getSampleSizeInBits() / NUM_BITS_PER_BYTE))

    def maximalPeakFromBucketArray(bucketArray: Array[Bucket]): Double = 
        bucketArray.par.foldLeft(0) {(peak, bucket) =&gt; if (bucket.peak &gt; peak) bucket.peak else peak} / 65535.0

    def normalizedSampleList(bucketArray: Array[Bucket], maxPeak: Double): List[List[Double]] =
        bucketArray.par.map(bucket =&gt; List(bucket.min, bucket.max).map(i =&gt; i / 65535.0 / maxPeak)).toList

    def sampleListToWaveFormCoordinateString(sampleList: List[List[Double]]): String =
        sampleList.flatten.map(f =&gt; "%.2f" format f).grouped(20).map(a =&gt; a.mkString(",")).mkString(",\n")

    def byteToSample(firstByte: Int, secondByte: Int): Int = (secondByte &lt;&lt; 8) + (firstByte &amp; 0x00ff)

    def waveformFromAudioInputStream(audioInputStream: AudioInputStream): String = {
      val resolution = 1600
      val bucketArray = Array.fill(resolution){new Bucket}
      val inputSize = audioInputStream.available
      val bucketSize = ((inputSize - 1).toFloat / resolution.toFloat + 0.5).toInt + 1
      var index = 0

      val bufferSize = audioInputStream.getFormat().getFrameSize() * 1024 * 1024 * 2 // With a frame size of 2 bytes, a buffer of around 4MB
      val buffer = new Array[Byte](bufferSize)

      var bytesRead = 1

      while (bytesRead &gt;= 0) {
        bytesRead = audioInputStream.read(buffer, 0, buffer.length)
        if ( bytesRead &gt;= 0)
        {
          for (doubleByte &lt;- buffer.grouped(2) if index / bucketSize &lt; resolution)
          {
              bucketArray(index / bucketSize).add(byteToSample(doubleByte(0),doubleByte(1)))
              index = index + 2
          }
        }
      }

      val maxPeak = maximalPeakFromBucketArray(bucketArray)
      val normalizedSamples = normalizedSampleList(bucketArray, maxPeak)
      sampleListToWaveFormCoordinateString(normalizedSamples)
    }
}
</code></pre>
]]></description>
  </item>
 
  <item>
    <title>Leaving CoreData behind…</title>
    <pubDate>Wed, 20 Jul 2011 00:00:00 -0400</pubDate>
    <link>http://gideondk.com/post/leaving-coredata-behind</link>
    <guid>http://gideondk.com/post/leaving-coredata-behind</guid>     
    <description><![CDATA[<p>For some reason, SQL databases always felt strange to me.</p>

<p>You try to create a optimal object-oriented structure, use attributes on objects to set and get data. And when you’re using less object-structured data like dictionaries, you use the (almost same) key-value coding as you do with dictionaries.</p>

<p>And then you have to store things… Using traditional relational databases, the next step is to create a ORM (object-relational mapper) to map those beautiful dynamic lists of objects into a table. A completely different approach of data representation: a two dimensional structure with tables, columns and rows.</p>

<h2>NoSQL</h2>

<p>Thank god for document and key-value stores like MongoDB and Redis! With the right tools, they enable you to create, store, change and extend your objects without thinking about database migrations.</p>

<p>Using such storage on you server side, makes you frustrated you have to revert to SQL for storage on the iPhone. Especially when you simply want to embed objects into other objects.</p>

<h2>Countries with cities</h2>

<p>For instance: you want to create multiple “Country” objects, which stores a ordered list of “City” objects (by alphabet). Perfectly fine in the object-oriented mind of a Objective-C programmer. But if you want to store these objects, and you want to retrieve a country with its cities. You’re out of luck…</p>

<p>First you have to retrieve the city you want to fetch, by name for instance, and you have to fetch all cities with a specific “city_id” matching the identifier of the city. Finishing the task by putting the list of city objects in the “cities” attribute of the country object.</p>

<p>Could be simpler right? SQL brings a lot of power when it comes to filtering and searching through your data. In this case, and many cases: SQL brings more pain then joy.</p>

<h2>BNRPersistence</h2>

<p>So let’s bring some noSQL goodness onto the iPhone by using <a href="https://github.com/hillegass/BNRPersistence">BNRPersistence</a>. BNRPersistence is a Objective-C wrapper for the key-value store “Tokyo Cabinet”.</p>

<p>With large data sets, BNRPersistence can be 10 to 17 times faster then the traditional CoreData. While sacrificing complex searching and filtering, you get a new storage layer which is not only faster but also much simpler!</p>

<p>Once you get past the (bit more difficult) installation, adding the storage functionality is more then easy. Start off by extending the <em>BNRStoredObject</em> class in your object.</p>

<pre class="prettyprint"><code>#import "BNRStoredObject.h"
@class City;

@interface Country : BNRStoredObject {
    NSString *name;
    NSMutableArray *cities;
}

- (void)setName:(NSString *)n;
- (NSString *)name;

- (NSArray *)cities;

@end
</code></pre>

<p>Next step is to add the data serialization and deserialization functions to your class, like this:</p>

<pre class="prettyprint"><code>- (void)readContentFromBuffer:(BNRDataBuffer *)d
{
    [name release];
    name = [[d readString] retain];

    [cities release];
    cities = [d readArrayOfClass:[City class] usingStore:[self store]];
    [cities retain];
}

- (void)writeContentToBuffer:(BNRDataBuffer *)d
{
    [d writeString:name];
    [d writeArray:cities ofClass:[city class]];
} 
</code></pre>

<p>And your up and running! For other functions you can use, just check the header file of <a href="https://github.com/hillegass/BNRPersistence/blob/master/BNRPersistence/BNRStoredObject.h">BNRStoredObject</a>.</p>

<p>At the moment, BNRPersistence shows some immaturity… So you can walk into some drawbacks:</p>

<ul>
<li>The framework isn’t thread safe currently, so lock your stuff or use one thread for storing data.</li>
<li>Stored data can be quite a bit larger then when using CoreData, so think things through what you want to store and what not.</li>
<li>Next to full-text searching, you don’t have a lot of other added functionalities. If you need things from CoreData, just use them side by side.</li>
</ul>

<p>All in all, if you are frustrated by the cumbersome managed object contexts and managed objects or looking a fast and simple storage method (with built in versioning). Definitely give BNRPersistence a try!</p>
]]></description>
  </item>
    
</channel>
</rss>
