bolusmjak’s posterous

 

Shame on you jQuery (or when is "foo" not "foo")

As of 1.4, jQuery has changed ajax parameter serialization as follows:

jQuery 1.4 adds support for nested param serialization in jQuery.param, using the approach popularized by PHP, and supported by Ruby on Rails. For instance, {foo: ["bar", "baz"]} will be serialized as “foo[]=bar&foo[]=baz”.

Did you catch that?

You call your parameter foo. jQuery now renames that to foo[] behind your back if foo's value is an array. The reason for this is because some PHP-ers and Rubyists expect 3rd party APIs to rename things for them.

Call me old fashioned, but when I put something into a map, with key x, I expect to find the value under x. Or at least have this the default behaviour with an optional override.

Even the documentation agrees with me:

If value is an Array, jQuery serializes multiple values with same key i.e. {foo:["bar1", "bar2"]} becomes '&foo=bar1&foo=bar2'.

In any case, if you want the old (traditional) behaviour, you can still get it:

// Enables for all serialization
jQuery.ajaxSettings.traditional = true;
// Enables for a single serialization
jQuery.param( stuff, true );
// Enables for a single Ajax requeset
$.ajax({ data: stuff, traditional: true });

Comments [0]

iPhone App Fail

Today, all of my 3rd part iPhone apps stopped working. (This is a few days after a 3.0 upgrade, but not immediately after any significant event.) Said apps would crash immediately. No built-in Apple app was affected. Downloaded apple apps (Remote) were affected. An application that I wrote and manually installed was not affected.

Some searching revealed I was not the only one with this problem.

Below is a simple fix that seems to be working (for the time being):

1. Uninstall *any* app.

2. Install *any* app (e.g. the one you just removed, or any other).

3. Every app will now be working.

 

I suspect there is some common "housekeeping" file on the iPhone that becomes corrupted affecting all the apps. And the uninstall-reinstall process fixes it up.

 

Good Luck if this happens to you.

Filed under  //   Apple   iPhone  

Comments [0]

Applicative-Order Y Combinator (Clojure Version)


y.clj

;                    Stumbling towards Y (Clojure Version) 
; 
; (this tutorial can be cut & pasted into your IDE / editor)  
; 
; The applicative-order Y combinator is a function that allows one to create a 
; recursive function without using define. 
; This may seem strange, because usually a recursive function has to call 
; itself, and thus relies on itself having been defined. 
; 
; Regardless, here we will stumble towards the implementation of the Y combinator. 
 
; This was largely influenced by material in "The Little Schemer". 
; http://www.ccs.neu.edu/home/matthias/BTLS/ 
; 
; I wrote this because I needed the excersise of arriving at Y on my own terms. 
; As such, it is likely not the best resource to learn this. 
; 
; Mark Bolusmjak 2009 
 
(def else true) 
 
; Let's start with the Factorial function. 
(def fact 
  (fn [n] 
    (cond 
      (= 0 n) 1 
      else (* n (fact (- n 1)))))) 
 
; Since we can't use define, let's strip that off. 
; For the recursive call, let's just put in a placeholder 
; to see how things look. (commented out because it won't compile) 
(comment 
  (fn [n] 
    (cond 
      (= 0 n) 1 
      else (* n (myself (- n 1)))))) 
   
; Ok, we need a way for the function to call itself via "myself". 
; How will we get a handle on that if we can't use define? 
; Maybe something can pass it in for us. Let's hope so and 
; keep going. 
(fn [myself] 
  (fn [n] 
    (cond 
      (= 0 n) 1 
      else (* n (myself (- n 1)))))) 
 
; That looks a lot like our original factorial function. 
; Let's replace "myself" with fact and take a look (below). 
; 
; From the outside, it looks like something that makes the factorial function. 
; Strangely, this function that creates the factorial function, relies on the 
; function "fact" to be supplied to itself. 
; Where will that come from? Does't that bring us back to square one? 
(fn [fact] 
  (fn [n] 
    (cond 
      (= 0 n) 1 
      else (* n (fact (- n 1)))))) 
 
; Well, we have a function that builds the factorial function. That seems 
; nearly as useful as a factorial function. Maybe we could pass that to itself 
; and try to work with that. 
; It's pretty easy to pass a function to itself if we have it defined. 
; e.g. (f f) 
; What about an anonymous function? 
; No problem, just write a helper function that takes any function and applies 
; it to itself. 
(fn [f] (f f)) 
 
; Ok, let's throw that in and take a look. 
; We'll pass fact to itself 
((fn [f] (f f)) 
  (fn [fact] 
    (fn [n] 
      (cond 
        (= 0 n) 1 
        else (* n (fact (- n 1))))))) 
 
; What happens if we try this out? 
(((fn [f] (f f)) 
  (fn [fact] 
    (fn [n] 
      (cond 
        (= 0 n) 1 
        else (* n (fact (- n 1))))))) 0) 
 
; Hey it works for 0, but not for anything else. 
; What's going on? 
; 
; With 0, (= 0 n) is true, and we get 1 back. 
; 
; If we try any other number we get a ClassCastException. Huh? 
; We got a bit ahead of ourselves. 
; Recall, fact is not the factorial funtion anymore. It now *builds* the 
; factorial function. 
; Also, we set it up to expecting a function as an argument before it returns 
; the actual function we want. 
; 
; This may be getting strange, but let's try to make a useful function out of 
; fact by calling (fact fact). This is good for one more step into our 
; function, at which point (fact fact) can be used again to get the next step. 
(((fn [f] (f f)) 
  (fn [fact] 
    (fn [n] 
      (cond 
        (= 0 n) 1 
        else (* n ((fact fact) (- n 1))))))) 5) 
 
; Wow! That actually worked. 
; Take a breather and think about this for a second. We took a few shots in the 
; dark and just implemented a recursive function without using define. 
; Pretty cool, but it's kind of messy. 
; 
; We have something that kind of looks like our original factorial definition 
; in there. Let's try to refactor that out and see what we're left with. 
; 
; Our original fact function didn't have anything looking like a (fact fact) in 
; it, so let's try to fix that first. 
; 
; As the next step, we'll pull out the (fact fact) and give it a name. 
; What should we call it? 
; fact is kind of like factorial builder at this point, so (fact fact) is more 
; like the factorial function. 
; Hmm ... let's just call (fact fact) fact2, see how things look and go from 
; there. 
; 
; We're going to be clever and instead of simply passing (fact fact) in as fact2, 
; were going to wrap it up as a closure to prevent (fact fact) from getting 
; evaluated before it gets passed in. 
; 
; So we'll pass in (fn [x] ((fact fact) x)) instead. 
((fn [f] (f f)) 
  (fn [fact] 
    ((fn [fact2] 
      (fn [n] 
        (cond 
          (= 0 n) 1 
          else (* n (fact2 (- n 1)))))) (fn [x] ((fact fact) x))))) 
 
; If we try this out, we see it still works. 
(((fn [f] (f f)) 
  (fn [fact] 
    ((fn [fact2] 
      (fn [n] 
        (cond 
          (= 0 n) 1 
          else (* n (fact2 (- n 1)))))) (fn [x] ((fact fact) x))))) 12) 
 
; Taking a closer look, we see that (fn [fact2] ... ) looks exactly like 
; our original (fn (fact) ... ). 
; Let's simply rename fact to y, and fact2 to fact to make this more clear. 
((fn [f] (f f)) 
  (fn [y] 
    ((fn [fact] 
      (fn [n] 
        (cond 
          (= 0 n) 1 
          else (* n (fact (- n 1)))))) (fn [x] ((y y) x))))) 
 
; Let's pull out (fn [fact] ... ) and pass it in as a parameter r (for 
; recursive function). 
((fn [r] 
  ((fn [f] (f f)) 
    (fn [y] 
      (r (fn [x] ((y y) x)))))) 
  (fn [fact] 
    (fn [n] 
      (cond 
        (= 0 n) 1 
        else (* n (fact (- n 1))))))) 
 
; Still works! Try it. 
(((fn [r] 
  ((fn [f] (f f)) 
    (fn [y] 
      (r (fn [x] ((y y) x)))))) 
  (fn [fact] 
    (fn [n] 
      (cond 
        (= 0 n) 1 
        else (* n (fact (- n 1))))))) 4) 
 
; Now we've isolated the scaffolding we've built to allow the creation of a 
; recursive function without define. 
; 
; Finally, we've lived long enough without define, and that thing we built 
; looks pretty useful. 
; Let's use define and call it Y. 
(def Y 
  (fn [r] 
    ((fn [f] (f f)) 
      (fn [y] 
        (r (fn [x] ((y y) x))))))) 
 
; That's it. We've built the applicative-order Y combinator in Clojure. 
 
; Try it with another recursive function. Here it is with the Fibonacci 
; number generator. 
; (which, incidentally, makes 2 recursive calls to itself). 
((Y 
  (fn [fib] 
    (fn [n] 
      (cond 
        (< n 2) n 
        else (+ (fib (- n 1)) (fib (- n 2))))))) 8)

Filed under  //   Clojure   Y Combinator  

Comments [1]

Applicative Order Y Combinator


;                          Stumbling towards Y
;
; The applicative-order Y combinator is a function that allows one
; to create a recursive function without using define.
; This may seem strange. Usually a recursive function has to call
; itself, and thus relies on itself having been defined.
;
; Regardless, here we will stumble towards the implementation of the
; Y combinator (in Scheme).
;
; This was largely influenced by material in "The Little Schemer".
; http://www.ccs.neu.edu/home/matthias/BTLS/
;
; I wrote this because I needed the excersise of arriving at Y on my
; own terms.
;
; Mark Bolusmjak, 2009
; Disclaimer: This document and code are without warrenty.



; Let's start with the Factorial function as our example.
(define fact
  (lambda (n)
    (cond
      ((= 0 n) 1)
      (else (* n (fact (- n 1)))))))

; Since we can't use define, let's strip that off.
; For the recursive call, let's just put in a placeholder
; to see how things look. We'll call the placeholder "myself".
(lambda (n)
  (cond
    ((= 0 n) 1)
    (else (* n (myself (- n 1))))))

; Ok, we need a way for the function to call itself via "myself".
; How will we get a handle on that if we can't use define?
; Maybe something can pass it in for us. Let's hope so and
; keep going.
(lambda (myself)
  (lambda (n)
    (cond
      ((= 0 n) 1)
      (else (* n (myself (- n 1)))))))

; That looks a lot like our original define-ed factorial function.
; Let's replace "myself" with fact and take a look (below).
;
; From the outside, it looks like something that makes the factorial
; function.
; Strangely, this function that creates the factorial function,
; relies on the function "fact" to be supplied to itself.
; Where will that come from?
; Doesn't that bring us back to square one?
(lambda (fact)
  (lambda (n)
    (cond
      ((= 0 n) 1)
      (else (* n (fact (- n 1)))))))

; Well, here we have a function that builds the factorial function.
; That seems nearly as useful as a factorial function. Maybe we
; could pass that to itself and call on it when we need it.
;
; First we need to pass that whole (lambda (fact) ... ) to itself.
; It's pretty easy to pass a function to itself if we have it
; defined.
; e.g. (f f)
; What about an anonymous function?
; No problem, just write a helper function that takes any function
; and applies it to itself.
(lambda (f) (f f))

; Ok, let's throw that in and take a look.
; We'll pass (lambda (fact) ... ) to itself via this helper.
((lambda (f) (f f))
 (lambda (fact)
   (lambda (n)
     (cond
       ((= 0 n) 1)
       (else (* n (fact (- n 1))))))))

; What happens if we try this out?
(((lambda (f) (f f))
  (lambda (fact)
    (lambda (n)
      (cond
        ((= 0 n) 1)
        (else (* n (fact (- n 1)))))))) 0)

; Hey it works for 0, but not for anything else.
; What's going on?
;
; With 0, (= 0 n) is true, and we get 1 back.
;
; If we try any other number we get a complaint that * is expecting a
; numbervas it's 2nd argument, but is getting a procedure. Huh?
; We got a bit ahead of ourselves.
; Recall, fact is not the factorial funtion anymore. It now *builds*
; the factorial function. We can't just pass it a number.
; As the fact builder function, it expects a function as an argument
; before it returns the actual function we want.
;
; This may be getting strange, but let's try to make a useful
; function out of fact by calling (fact fact). At least (fact fact)
; will work for the next step into our recursive function.
((lambda (f) (f f))
  (lambda (fact)
    (lambda (n)
      (cond
        ((= 0 n) 1)
        (else (* n ((fact fact) (- n 1))))))))

; Hmm ... recursion works by "always working for the next step",
; which we seem to have just covered. So could it work for all the
; "next steps"?
; Let's try to call that with a value other than 0.
(((lambda (f) (f f))
  (lambda (fact)
    (lambda (n)
      (cond
        ((= 0 n) 1)
        (else (* n ((fact fact) (- n 1)))))))) 5)

; Wow! That actually worked.
; Take a breather and think about this for a second. We just
; implemented a recursive function without using define.
; Pretty cool, but it's kind of messy.
;
; We have something that kind of looks like our original factorial
; definition in there. Let's try to refactor that out and see what
; we're left with.
;
; Our original fact function didn't have anything looking like a
; (fact fact) in it, so let's try to fix that first.
;
; As the next step, we'll pull out the (fact fact) and give it a
; name.
; What should we call it?
; fact is kind of like factorial builder at this point, so
; (fact fact) is more like the factorial function.
; Hmm ... let's just call (fact fact) fact2, see how things look and
; go from there.
;
; We're going to be clever and instead of simply passing (fact fact)
; in as fact2, we're going to wrap it up as a closure to prevent
; (fact fact) from getting evaluated before it gets passed in. Cool?
; We'll pass in (lambda (x) ((fact fact) x)) instead.
((lambda (f) (f f))
 (lambda (fact)
   ((lambda (fact2)
      (lambda (n)
        (cond
          ((= 0 n) 1)
          (else (* n (fact2 (- n 1)))))))(lambda (x) ((fact fact) x)))))

; If we try this out, we see it still works.
(((lambda (f) (f f))
  (lambda (fact)
    ((lambda (fact2)
       (lambda (n)
         (cond
           ((= 0 n) 1)
           (else (* n (fact2 (- n 1)))))))(lambda (x) ((fact fact) x))))) 12)

; Taking a closer look, we see that (lambda (fact2) ... ) looks
; exactly like our original (lambda (fact) ... ).
; Let's simply rename fact to y, and fact2 to fact to make this
; clear.
((lambda (f) (f f))
 (lambda (y)
   ((lambda (fact)
      (lambda (n)
        (cond
          ((= 0 n) 1)
          (else (* n (fact (- n 1)))))))(lambda (x) ((y y) x)))))

; Let's pull out (lambda (fact) ... ) and pass it in as a parameter
; r (for recursive function).
((lambda (r)
   ((lambda (f) (f f))
    (lambda (y)
      ((lambda (x) ((y y) x))))))
 (lambda (fact)
   (lambda (n)
     (cond
       ((= 0 n) 1)
       (else (* n (fact (- n 1))))))))

; Still works! Try it.
(((lambda (r)
    ((lambda (f) (f f))
     (lambda (y)
       ((lambda (x) ((y y) x))))))
  (lambda (fact)
    (lambda (n)
      (cond
        ((= 0 n) 1)
        (else (* n (fact (- n 1)))))))) 4)

; Now we've isolated the scaffolding we've built to allow the
; creation of a recursive function without define.
;
; Finally, we've lived long enough without define, and that thing we
; built seems pretty useful.
; Let's use define and call it Y.
(define Y
  (lambda (r)
    ((lambda (f) (f f))
     (lambda (y)
       ((lambda (x) ((y y) x)))))))

; That's it. We've built the applicative-order Y combinator.

; Try it with another recursive function. Here it is with the
; Fibonacci number generator, taking a value of 8 as a parameter.
((Y
  (lambda (fib)
    (lambda (n)
      (cond 
        ((< n 2) n)
        (else (+ (fib (- n 1)) (fib (- n 2)))))))) 8)

Filed under  //   Scheme   Y Combinator  

Comments [0]

First Post

//Sent from iPhone//

Comments [0]