zzz6519003 / blog

My blog about coding
4 stars 1 forks source link

clojure day-2 #101

Open zzz6519003 opened 7 years ago

zzz6519003 commented 7 years ago

Vectors are optimized for random access. You’ll surround vectors with square brackets, like this:

then we’ll look at that mystical language feature that is the Force for all Lisps, the macro.

As you’ve learned in other languages in this book, functional languages depend on recursion rather than iteration

Initially, given a vector, loop binds the variables in the even positions to the values in the odd positions. In fact, if you don’t specify a recur, loop works exactly like a let:

      user=> (loop [x 1] x)
      1

The list comprehension combines maps and filters, as you saw in Erlang and Scala.

Remember, in Clojure, we’re using immutable values. That means that turning will return a new, modified compass rather than changing the existing compass in place.

Object
(toString [this] (str "[" (direction this) "]")))

how to create mutable references of Clojure objects in ways that maintain transactional integrity, much like relational databases do.

In general, macro expansion will let you treat code like lists. If you don’t want a function to execute right away, quote it. Clojure will replace the arguments intact

user=> (defmacro unless [test body]
        (list 'if (list 'not test) body))
#'user/unless

We’re building a list of code in the exact form that Clojure will execute.

What we’ve done is change the base definition of the language. We are adding our own control structure, without requiring the language designers to add their own keywords. Macro expansion is perhaps the most powerful feature of Lisp, and few languages can do it. The secret sauce is the expression of data as code, not just a string. The code is already in a higher-order data structure.

Since the JVM doesn’t support tail- recursion optimization, we had to use loop and recur.

With lazy sequences, we were able to add another powerful layer to sequences. Lazy sequences simplified algorithms. They also offered de- layed execution, potentially significantly improving performance and loosening coupling.

there is a step, called macro expansion, that occurs before Clojure implements or interprets code.

zzz6519003 commented 7 years ago

https://mega.nz/#!nNxClIBL!OblCVBk5XgrN38YLveDODoc-MNoMyg-nZwC-4i-dAbI