aboutsummaryrefslogtreecommitdiff
path: root/README.org
blob: dceb7531d0eef555257f3001e7ece14d3579f5dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
* lambda-riffs 

ALPHA QUALITY -- USE AT YOUR OWN RISK 

Lambda riffs provides

- a reader macro for building lambdas on the fly
- a few utilities for combining functions

Simply loading the system will introduce the reader macro.

** Examples and Use

To use the system you only need to load it. It adds a adds a single
reader macro to your Lisp system.

If this system has any actual users, and if those users are
dissatisfied with the alteration of the global read table, let me know
and I can try adapting this for use with =named-readtables=. 

*** Normal  Use

As normally used, =lambda-riffs= lets you make quick anonymous
functions.  There is a special syntax for making variables that start
with =$=, for example =$x= or =$my-var=.

Here is a basic example

 #+begin_src lisp :results verbatim

 (let ((xs (list 1 2 3 4 5 6)))
   (remove-if-not (lambda (x) (member x xs))
                  (loop repeat 20 collect (random 10))))

 ;; could be written

 (let ((xs (list 1 2 3 4 5 6))) 
   (remove-if-not #$(member $x xs)
                  (loop repeat 20 collect (random 10))))

 #+end_src

Outputs
 : (3 1 6 1 4 2 4 6 4 3 5 1 5 3)

The =#$= syntax is a reader macro dispatch sequence. Any symbol that
begins with =$= inside a form that prepended by =#$= will be treated
as a parameter of the anonymous function being defined.

#+begin_src lisp :results verbatim

(list  (funcall #$(list $x $y) 1 2)     ;; two arguments
       (funcall #$(list $x $x $x) 10))  ;; one argument used in three places
 
#+end_src

Outputs
: ((1 2) (10 10 10))

*** Numbered Parameters 

Examples of numbered parameters:

- =$1= , =$2= 
- =$1-with-a-name= , =$2another-name= 

If your form uses numbered parameters, then all of the parameters in
that form should be numbered.  

I.e. numbered parameters begin =$= and are followed first by an
integer and then any normal variable name characters.

The effect of numbered parameters is to explicitly specify the order
of the parameters in the anonymouse function being defined.  Here is
ane example:

#+begin_src lisp :results verbatim

;; flip the order 
(funcall #$(list $2-symb $1-symb) 'second 'first)

#+end_src

Outputs
: (FIRST SECOND)

In the above ='second= is passed in as the first argument, and
='first= is passed as the second argument.  

Interestingly the numbers do not have to be sequential, they are
merely sorted in ascending order:

#+begin_src lisp :results verbatim 
;; $2 < $4 < $10
(funcall #$(list $10 $2 $4) :two :four :ten)

#+end_src

Outputs
: (:TEN :TWO :FOUR)

*** Nested Forms

You can nest forms by appending an additional $ to the dispatch
sequence. Variables of nested forms must include the same number of $
characters as there are in their surrounding dispatch sequence.

This is easer to understand through example:

#+begin_src lisp :results verbatim

;; map over a list of lists, subtracting 9 from any member of a list
;; that is greater than 9

;; without lambda-riffs,  you might do:

(mapcar (lambda (digit-list)
          (mapcar (lambda (x) (if (> x 9) (- x 9) x))
                  digit-list))
        '((1 2 3 4 5 6 7 8 9)
          (10 11 12 12 14 15 16 17 18)))

;; with lambda-riffs, you can do

(mapcar #$(mapcar #$$(if (> $$x 9) (- $$x 9) $$x) $digit-list)                     
        '((1 2 3 4 5 6 7 8 9)
          (10 11 12 12 14 15 16 17 18)))
#+end_src

Outputs
: ((1 2 3 4 5 6 7 8 9) (1 2 3 3 5 6 7 8 9))

** Other tools

*** rev

#+begin_src lisp :results verbatim

(funcall (riff:rev #'cons) 1 2)


#+end_src

: (2 . 1)

*** closure

#+begin_src lisp :results verbatim

(let ((x 0))
  (let ((inc
          (riff:closure
            (incf x))))
    (funcall inc :these :will :all :be :ignored)
    (funcall inc)))


#+end_src

: 2

*** mapped

#+begin_src lisp :results verbatim

(funcall (riff:mapped #$(+ 10 $x) #$(format nil "--~a--" $x))
         100)


#+end_src

: (110 "--100--")

*** lambda-if

#+begin_src lisp :results verbatim

(let ((decide
        (riff:lambda-if #'evenp
                        #$(format nil "It's even: ~a~%" $x)
                        #$(format nil "It's odd: ~a~%" $x))))
  (funcall decide 10))


#+end_src

: It's even: 10

*** lambda-cond

#+begin_src lisp :results verbatim
(let ((decider
        (riff:lambda-cond
         #$(< (length $x) 10) (constantly "It's short.")
         #$(<= 10 (length $x) 20) #$(format nil "Medium length: ~a" (length $x))
         (constantly t) (constantly "Longish"))))

  (mapcar decider (list "short" "this is a bit longer" "and this one is too long probably")))

#+end_src


: ("It's short." "Medium length: 20" "Longish")