Simply put, destructuring in Clojure is a way extract values from a datastructure and bind them to symbols, without having to explicitly traverse the datstructure. It allows for elegant and concise Clojure code.
| ;; warm up: balancing | |
| => (s/def ::balanced | |
| (s/* (s/cat :open #{'<} :children ::balanced :close #{'>}))) | |
| :user/balanced | |
| => (s/conform ::balanced '[< < > < > > < >]) | |
| [{:open <, :children [{:open <, :close >} {:open <, :close >}], :close >} {:open <, :close >}] | |
| => (s/conform ::balanced '[< < > < < > > > < >]) | |
| [{:open <, :children [{:open <, :close >} {:open <, :children [{:open <, :close >}], :close >}], :close >} {:open <, :close >}] | |
| ;; infix to prefix |
| { config, pkgs, ... }: | |
| let | |
| hostname = "luz3"; | |
| in { | |
| imports = | |
| [ # Include the results of the hardware scan. | |
| ./hardware-configuration.nix | |
| # I use VirtualBox to connect to Windows and Linux guests |
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| ;;; Connection | |
| (defprotocol DatomicConnection | |
| (as-conn [_])) | |
| (extend-protocol DatomicConnection | |
| datomic.Connection | |
| (as-conn [c] c) | |
| datomic.db.Db |
| ;; I wrote this in the Eurostar on my way back from the last lambdanext.eu clojure course. | |
| (ns comprehensions | |
| (:refer-clojure :exclude [for doseq]) | |
| (:require [clojure.core.reducers :as r])) | |
| ;; borrowed from clojure.core | |
| (defmacro ^{:private true} assert-args | |
| [& pairs] | |
| `(do (when-not ~(first pairs) |
I don't want anyone to think that I am not a big fan of ReactiveCocoa. I'm a HUGE FAN of it!
But look how LONG it took for something like this to come about. Cocoa is an OLD OLD system, and even though KVO/KVC wasn't there at the birth, it has been there at least a decade. I pretty much gave up on Obj-C in favor of RubyMotion, and look at our landscape: Futuristic, ProMotion, Formotion, Geomotion, Elevate, Teacup - all of these projects bring expressiveness to Cocoa, and RubyMotion is barely a year old.
So what I meant to say is that Obj-C suffers from a lack of expressiveness - this has nothing to do with ReactiveCocoa, that's just my example. To illustrate my point, I will translate the first example of the ReactiveCocoa README. I encourage you to read the ReactiveCocoa source and try and consolidate all the code that is used to handle this function. I will include all of the Ruby code that is necessary for my translation to work.
I do have one qualm with ReactiveCocoa: the names. I'm going to t
| class AppDelegate | |
| def application(application, didFinishLaunchingWithOptions:launchOptions) | |
| @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds).tap do |win| | |
| win.rootViewController = UINavigationController.alloc.initWithRootViewController(CountriesController.new) | |
| win.backgroundColor = UIColor.whiteColor | |
| win.makeKeyAndVisible | |
| end | |
| true |
| (ns graaaph.core | |
| (:import (org.jrubyparser Parser | |
| CompatVersion) | |
| (org.jrubyparser.parser ParserConfiguration) | |
| (org.jrubyparser.ast Node) | |
| (java.io.StringReader)) | |
| (:require [clojure.zip :as z])) | |
| (defn parse-ruby [ruby-string] | |
| (let [config (ParserConfiguration. 0 (CompatVersion/RUBY1_9)) |
| #!/usr/bin/env ruby | |
| mem = Array.new(30_000) | |
| iptr = dptr = 0 | |
| prog = File.read ARGV.first | |
| jmp = lambda { |op, a, b| | |
| (iptr = iptr.send(op, 1); jmp.call(op, a, b) if prog[iptr] == a) until prog[iptr] == b | |
| } | |
| nop = lambda {} | |
| cmds = { | |
| '>' => lambda { dptr += 1 }, |