We're growing into something new. Read More
adrusi

Adrian Sinclair


Discussion

Realtime HTML, CSS and Javascript environment

This tool might be useful to a decent number of people here on forrst. It allows for editing html , CSS and JavaScript with a realtime preview like jsfiddle and company, but it also supports realtime collab. While it seems like an obvious feature for such a tool, I can't think of anything else that supports it. Go try it out.

class Hash
  def has_all_keys key_list
    has_all_keys = true
    key_list.each do |key|
      has_all_keys = false if self[key].nil?
    end
    has_all_keys
  end
end

class ElectricalComponent
  def initialize
    
  end
end


class PowerSource < ElectricalComponent
  def initialize state, name
    @on = state == :on
    name ||= to_s
    @name = name
  end
  
  def connect opts
    # call like this:
    #     PowerSource#connect from_output: :a,
    #                         to_component: <ElectricalComponent>,
    #                         on_input: <input symbol>
    if opts.has_all_keys [:from_output, :to_component, :on_input]
      @receiver_component = opts[:to_component]
      @receiver_input = opts[:on_input]
      update
    else
      raise "invalid options given to PowerSource#connect"
    end
  end
  
  def off
    @on = false
    @receiver_component.unpower @receiver_input
  end
  
  def on
    @on = true
    @receiver_component.power_up @receiver_input
  end
  
  def toggle
    @on = !@on
    update
  end
  
  def state
    "PowerSource \"#{@name}\" is #{if @on then "on" else "off" end}"
  end
  
  private
  def update
    if @on
      @receiver_component.power_up @receiver_input
    else
      @receiver_component.unpower @receiver_input
    end
  end
end


class AndGate < ElectricalComponent
  def initialize
    @a_is_on = false
    @b_is_on = false
  end  
  
  def connect opts
    if opts.has_all_keys [:to_component, :on_input]
      @receiver_component = opts[:to_component]
      @receiver_input = opts[:on_input]
      update_output
    else
      raise "invalid options given to AndGate#connect"
    end
  end
  
  def power_up input
    if input == :a
      @a_is_on = true
    elsif input == :b
      @b_is_on = true
    else
      raise "AndGates only accept inputs `:a` and `:b`"
    end
    update_output
  end
  
  def unpower input
    if input == :a
      @a_is_on = false
    elsif input == :b
      @b_is_on = false
    else
      raise "AndGates only accept inputs `:a` and `:b`"
    end
    update_output
  end
  
  private
  def update_output
    if !@receiver_component.nil? && !@receiver_input.nil?
      if @a_is_on && @b_is_on
        @receiver_component.power_up @receiver_input
      else
        @receiver_component.unpower @receiver_input
      end
    end
  end
end


class OutputDevice < ElectricalComponent
  def initialize name
    @on = false
    @name = name
    puts "OutputDevice \"#{@name}\" is off"
  end
  
  def power_up input
    if input == :a
      on = @on
      @on = true
      alert if !on
    else
      raise "OutputDevices only accept input `:a`, got `#{}"
    end
  end
  
  def unpower input
    if input == :a
      on = @on
      @on = false
      alert if on
    else
      raise "OutputDevices only accept input `:a`, got `#{input}"
    end
  end
  
  private
  def alert
    if @on
      puts "OutputDevice \"#{@name}\" is now on"
    else
      puts "OutputDevice \"#{@name}\" is now off"
    end
  end
end


##### TEST CODE #####


puts "A and B are off."
source_a = PowerSource.new :off, "source a"
source_b = PowerSource.new :on, "source b"
gate = AndGate.new
alerter = OutputDevice.new "alerter"

source_a.connect from_output: :a,
                 to_component: gate,
                 on_input: :a
source_b.connect from_output: :a,
                 to_component: gate,
                 on_input: :b
    gate.connect from_output: :a,
                 to_component: alerter,
                 on_input: :a

puts "Turn A on."
source_a.on
puts "Turn B off."
source_b.off
puts "Turn A off."
source_a.off
puts "Turn B on."
source_b.on
puts "Turn A on."
source_a.on

Ruby Electronics Emulator

Just to brush up on some OOP strategies (since I've been doing more function programming lately) I started this hardware emulator in ruby. I know it's kind of long, uncommented and probably not too great since I wrote it in less than an hour while recovering from pneumonia, but I kind of like the system I came up with. Here's what it looks like (without debugging messages): source_a = PowerSource.new :off, "source a" source_b = PowerSource.new :on, "source b" gate = AndGate.new alerter = OutputDevice.new "alerter" source_a.connect from_output: :a, to_component: gate, on_input: :a source_b.connect from_output: :a, to_component: gate, on_input: :b gate.connect from_output: :a, to_component: alerter, on_input: :a source_a.on source_b.off source_a.off source_b.on source_a.on Raw Code » It turns out that the physics work more like redstone from minecraft than real electricity, but oh well. I'll probably add some more gates to make it more complete, but since the system already established is meant to be flexible, it shouldn't be too painful to add more cool components.

var fs = require("fs");
(function() {
  var done = 0;
  var file1, file2;
  fs.readFile("path/to/file_1.json", "utf8", function(error, data) {
    // do stuff with data
    file1 = data;
    cont();
  });
  fs.readFile("path/to/file_2.json", "utf8", function(error, data) {
    // do stuff with data
    file2 = data;
    cont();
  });
  function cont() {
    if (++done !== 2) return; // 2 = number of files to load
    // stuff to do after both files have loaded (using variables file1 and file 2)
  }
}());

Quick tip for easier and more efficient Async code

This is just a quick tip for dealing with executing multiple asynchronous operations at once, and then calling a function when both are done. A lot of the time, I see stuff like this: fs.readFile("path/to/file_1.json", "utf8", function(error, data) { // do stuff with data fs.readFile("path/to/file_2.json", "utf8", function(error, data) { // do stuff with data, that is not dependent on file_1 // do stuff with data that is dependent on file_1 and file_2 }); }); Raw Code » Although this code uses callbacks and therefore could be asynchronous, in practice, it's the exact same as synchronous code, just less readable. I suggest the code at the top as a better alternative.

Lambda Poster

For the Lambda geeks on forrst, I've put together this ver minimal poster, featuring a giant lambda, and the y combinator in the untyped lambda calculus. I linked to a 16x20" download. License: Creative Commons 0 (Public Domain)

Discussion

What is elegant code

Blog post I wrote about what qualifies elegant code. I'm interested to see if people have anything to add, and what people disagree with.

Discussion

cl_GTD

I was just looking through my old github repos and I found this. It's kind of humbling to remember how little I knew in just November 2010. I wrote the whole program (I'll describe what it is below) in PHP because I wasn't yet comfortable with any other language, and looking at the source, I wasn't comfortable with PHP either. It's almost an ego-boost to realize that I was able to code such a thing without being comfortable with the language and still being a novice programmer. The name stands for command line Getting Things Done It's really more of a feature-rich todo app, but you can use it for the GTD method. It has a relatively polished interface, though it's not good for dealing with tasks on the fly because it takes over the entire terminal screen, so it's really best to keep a terminal window open just for cl_GTD. It has quite a few interesting features. You see a full list on the wiki, but here are some of the cool ones: themeability a simple query language for setting bookmarks and the startup page posts filters are incremental. You can filter to show all tasks tagged "homework" and then add a filter to only display tasks due in the next week. a useful date filter language that lets you select tasks due from the current day until n days in the future, due on a specific date, etc. when you delete tasks they go to an archive (like the trash) and are only permanently deleted when you empty the archive. You can also un-delete archived tasks, which is useful if you accidentally delete the wrong task. I'm not saying that it's all great though. When you edit a task, you either leave the tags as they are or you have to overwrite all the tags, which is awful. Another issue is that it's implemented in PHP, and the code is messy. It also lacks batch operations, so if you want to delete 3 old tasks, you need to do it one by one. Go ahead and try it out, the installation is very simple: git clone github.com/adrusi/… cd cl_GTD ./INSTALL Raw Code » Then you have to select install for the current user, because the other option is not yet implemented. Hopefully this will be useful to someone some day, but it can't compete with apps like Things or The Hit List, because although it can be synced over dropbox with some symlinks, you can't add tasks on a mobile phone unless you have some ssh app :|

Discussion

The difference between scripting languages and programming languages

I blog post I wrote about the difference between scripting languages and programming languages (hint: there is none). Please leave comments on the post if you think I got something wrong or want to make some other point :)

class ObjLit
  def initialize hash
    @hash = hash
  end
  
  def method_missing method, *args, &block
    val = @hash[method]
    if val.is_a? Proc
      val.call *args, &block
    else
      val
    end
  end
  
  def [] method
    @hash[method]
  end
  
  def []= method, val
    @hash[method] = val
  end
end

def objlit *args
  ObjLit.new *args
end

obj = objlit\
  prop: "foo",
  meth: lambda { |x|
    x * 2
  }
  

puts "#{obj.prop}. 2 * 2 = #{obj.meth 2}" # -> "foo. 2 * 2 = 4"

obj[:property] = "bar"

puts obj.property # -> "bar"

javascript-style object literals in ruby

I love both javascript and ruby, but javascript's object literals are really much better than ruby's singleton object creation. Normally in ruby, to make an object, you have to do stuff like this: obj = Object.new def obj.foo "bar" end Raw Code » which is really verbose, especially when compared to javascript's way: var obj = { foo: "bar" }; Raw Code » but using method_missing we can do better, as shown in the example. Currently, code like obj.foo = "foo" Raw Code » doesn't work, but I'm sure you could convert the symbol to a string and see if its last character is a = to reassign.