« »

Nu cheat sheet

Nu was dropped today. I immediately jumped on it but I’m not altogether useful with it. There’s a lot of syntax and whatnot that either isn’t document, isn’t obvious, or both. I’m putting all of my discoveries in the extended section. Expect it to grow over the next few days.

List enumeration

A bit long-winded, but still simpler than Cocoa:

(set a '(1 2 3)) ; The single quote indicates a list in Nu.
(a each: (do (n) ; `each` starts the enumeration and calls the `do` block with each item in the list.
    (puts n)))

Using NuTemplate

I like templating functions in programming languages. Nu’s is fairly easy to use.

(load "Nu:template") ; Load the templating class
(set a '(1 2 3)) ; Again with the simple list
(set tmp (NuTemplate codeForString:< <-END ; I really like that I can use multi-line strings
    Here is the first number in the list: <%= (head a) %>. ; The "=" means "print"
    Here is the last number in the list: < %= (tail (tail a)) %>.
    Here are all of the numbers: < % (a each: (do (n) %> ; Here's a block of code.
        < %= (n) %>< %))%> ; Here we print and then we end the block
    END)) ; End the text block
(puts (eval tmp)) ; The template object is converted to a string using the "eval" command

String templating

Simple string templating is also really easy:

(set a '(1 2 3))
(puts "Item 1 of a is: #{(head a)}")

A word on lists and arrays from Mr. Tim Burks

“I just wanted to point out that ‘(1 2 3) is creating a list and not an array. In Nu, arrays are represented with NSArray and its subclasses whereas lists are linked lists of NuCells. Both kinds of containers support iteration with the each: method.”

Leave a Reply