Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## Appendix - Odds and ends | ||
|
||
Here are a few nuggest to chew on. | ||
|
||
### Compiling regex at parse | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
|
||
## Lisp iterations | ||
|
||
Common lisp's ITERATE library is a powerful and subtle looping facility. it allows you to write | ||
loops declaratively instead of imperatively and includes many types of subclauses for most or all | ||
of the common tasks that need to be done inside of loops. | ||
|
||
examples stolen from: http://common-lisp.net/project/iterate/doc/Introduction.html | ||
|
||
|
||
<pre class="brush:clojure"> | ||
(iter (for i from 1 to 10) | ||
(collect i)) | ||
</pre> | ||
|
||
This doesn't seem so exciting. Ruby has collect too, and some nice | ||
looping facilities. The ruby equivalent is: | ||
|
||
<pre class="brush:ruby"> | ||
(1..10).map{|i| i} # =>[1,2,3,4,5,6,7,8,9,10] | ||
</pre> | ||
|
||
But ITERATE has many more capabilities than the actually fairly limited | ||
set of things that ruby's each, map, inject, find, etc can do for you | ||
|
||
This one iterates over a list and collects the odd numbers in it: | ||
|
||
|
||
<pre class="brush:clojure"> | ||
(iter (for el in list) | ||
(if (and (numberp el) (oddp el)) | ||
(collect el))) | ||
</pre> | ||
|
||
That kind of thing can't be done with map in the general case without | ||
falling back to the imperative form of loop; a while or for loop. You | ||
can't do it with #collect. | ||
|
||
<pre class="brush:clojure"> | ||
(iter (for (key . item) in alist) | ||
(for i from 0) | ||
(declare (fixnum i)) | ||
(collect (cons i key))) | ||
</pre> | ||
|
||
This loop takes the keys of an alist and returns a new alist associating | ||
the keys with their positions in the original list. The compiler declaration | ||
for i will appear in the generated code in the appropriate place. | ||
|
||
|
||
To find the length of the shortest element in a list: | ||
|
||
<pre class="brush:clojure"> | ||
(iterate (for el in list) | ||
(minimize (length el))) | ||
</pre> | ||
|
||
To return t only if every other element of a list is odd: | ||
|
||
<pre class="brush:clojure"> | ||
(iterate (for els on list by #'cddr) | ||
(always (oddp (car els)))) | ||
</pre> | ||
|
||
* http://common-lisp.net/project/iterate/doc/index.html | ||
|
||
|
||
Iterate allows you to declare your loop at high level, rather than getting | ||
into the low level details of programming your loop. | ||
Iterate is written with lisp macros. at parse time, iterate loops are | ||
compiled down to the equivalent of efficient while loops, without | ||
iterating over data sets multiple times or a lot of extra bookkeeping. | ||
|
||
|
||
|
||
<pre class="brush:ruby"> | ||
with foo.bar { | ||
baz | ||
quux | ||
fleeble | ||
} | ||
=> | ||
t=foo.bar | ||
t.baz | ||
t.quux | ||
t.fleeble | ||
|
||
|
||
macro with arg | ||
:( t= ^arg ) | ||
yield.deep_copy{|node| | ||
if RedParse::CallNode===node and node.receiver.nil? | ||
node.receiver=:(t) | ||
end | ||
node | ||
} | ||
end | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters