update with macro syntax and some other stuff
This commit is contained in:
parent
7a732b4b05
commit
3cab34ad31
86
README.md
86
README.md
@ -23,80 +23,94 @@ This is also a string, it can contain anything:
|
||||
"! I'm the king of the world !"
|
||||
```
|
||||
|
||||
This is a list. It evaluates to a linked-list of its elements (not lisp-like!):
|
||||
This is a list. It evaluates to a linked-list of four strings:
|
||||
|
||||
```
|
||||
(a b c d)
|
||||
```
|
||||
|
||||
This is a string:
|
||||
This is a vector of those same elements. It's like a list, but has some slightly
|
||||
different properties. We'll mostly be using lists:
|
||||
|
||||
```
|
||||
[a b c d]
|
||||
```
|
||||
|
||||
This is a string
|
||||
|
||||
```
|
||||
+
|
||||
```
|
||||
|
||||
This is a string being evaluated, it will return a function:
|
||||
`:` is the evaluator. This evaluates to a function which adds its arguments:
|
||||
|
||||
```
|
||||
:+
|
||||
```
|
||||
|
||||
This is a list, with a string and two numbers:
|
||||
|
||||
```
|
||||
(+ 1 2)
|
||||
```
|
||||
|
||||
This is a list, with a function and two numbers:
|
||||
This evaluates to list whose elements are a function and two numbers:
|
||||
|
||||
```
|
||||
(:+ 1 2)
|
||||
```
|
||||
|
||||
This is a list being evalutated, the first item in the list must be a function:
|
||||
This evaluates to the number 5:
|
||||
|
||||
```
|
||||
:(:+ 1 2)
|
||||
```
|
||||
|
||||
This is a list in a list:
|
||||
The `fn` function can be used to define a new function. This evaluates to an
|
||||
anonymous function which adds one to its argument and returns it:
|
||||
|
||||
```
|
||||
((a b) foo bar)
|
||||
:(:fn [x]
|
||||
:(:+ :x 1))
|
||||
```
|
||||
|
||||
This is an anonymous function, it takes in the arguments `a` and `b`, and
|
||||
returns their sum:
|
||||
The `def` function can be used to bind some value to a new variable:
|
||||
|
||||
```
|
||||
#((a b)
|
||||
:(:+ :a :b))
|
||||
:(:def foo bar)
|
||||
# Now :foo will evaluate to the string bar
|
||||
|
||||
:(:def incr
|
||||
(:fn [x]
|
||||
:(:+ :x 1)))
|
||||
# Now :incr will evaulate to a function which adds 1 to its argument
|
||||
|
||||
#defn is a shortcut for the above
|
||||
:(:defn incr [x]
|
||||
:(:+ :x 1))
|
||||
```
|
||||
|
||||
`map` takes in a function and a list, and calls the function on each item in the
|
||||
list. The following will return `(1 2 3)`:
|
||||
There are also maps. A map's keys can be any value(?). A map's values can be any
|
||||
value. This evaluates to a map with 2 key/val pairs:
|
||||
|
||||
```
|
||||
:(:map ; This is a comment, it's ignored
|
||||
#((a) (:+ :a 1)) ; <- Increment function
|
||||
(0 1 2))
|
||||
{ foo :foo
|
||||
bar (:incr 4) }
|
||||
```
|
||||
|
||||
`name` names a value. After the following call, `:ted` will always evalutate to
|
||||
`5`:
|
||||
`.` is the half-evaluator. It only works on lists, and runs the function given
|
||||
in the first argument with the unevaluated arguments (even if they have `:` in
|
||||
front). You can generate new code to run on the fly (macros) using the normal
|
||||
`fn`. This evaluates to a `let`-like function, except it forces you to use the
|
||||
capitalized variable names in the body (utterly useless):
|
||||
|
||||
```
|
||||
:(:name ted 5)
|
||||
```
|
||||
|
||||
You can name any value, including functions:
|
||||
:(:defn caplet [mapping body...]
|
||||
# elem-map maps over every element in a list, embedded or otherwise
|
||||
::(:elem-map
|
||||
:(:fn [x]
|
||||
:(:if (mapping :(:slice :x 1))
|
||||
(capitalize :x)
|
||||
:x))
|
||||
:body))
|
||||
|
||||
```
|
||||
:(:name increment #((a) :(:+ a 1)))
|
||||
```
|
||||
|
||||
Now the `map` example above can be simplified down to:
|
||||
|
||||
```
|
||||
:(:map :increment (0 1 2))
|
||||
#Usage
|
||||
.(:caplet [foo "this is foo"
|
||||
dog "this is dog"]
|
||||
:(:println :Foo)
|
||||
:(:println :Dog))
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user