Primitive Types and Data Structures - blancas/eisen GitHub Wiki
The following sections illustrate Eisen data types and their correspondence to Clojure objects, which are marked in bold. For the most part, Eisen types are Clojure types, except for some minor changes mainly because Eisen uses infix notation, as in everyday arithmetic.
We first load the Eisen core namespace, initialize the API and launch the Eisen repl.
(use 'blancas.eisen.core)
(init-eisen)
(eisen-repl)After the last command we get the Eisen repl prompt. The repl will keep reading after hitting Enter. It will evaluate the code after it gets an empty line. To evaluate 3 + 4:
user: 3 + 4 <Enter>
> <Enter>
7
user: _
The following code snippets won't show the Eisen prompts for the sake of clarity. Copy and paste a snippet into the Eisen repl and hit Enter. The result of the evaluation is shown with the double-dash line comment.
Boolean literals are true and false.
true || false
-- true
A character literal goes in single quotes, including the usual escape codes.
'a'
-- a
'\n'
A String literal goes in double quotes, including the usual escape codes.
"Now is the time"
-- Now is the time
"Col1\tCol2\Col3\n"
-- Col1 Col2 Col3
--
A regular-expression literal is a string with a leading hash or pound character.
#"^ABC\\\\."
-- #"^ABC\."
Keywords start with a colon and evaluate to themselves.
:address
-- :address
Integers can be entered in decimal, octal, and hexadecimal as long. A trailing N indicates BigInt.
4500800N
-- 4500800N
0666
-- 438
0xCAFEBABE
-- 3405691582
Floating point numbers can be double or BigDecimal. At least one digit must follow the decimal point; thus 1. is not a legal number.
3.141615927
-- 3.141615927
8.9876543210123456789M
-- 8.9876543210123456789M
The null value can be written nil or null.
As in Clojure, any element in a data structure may be any simple object or an expression, including another data structure, so they may be combined and nested.
A list literal has zero, one, or more elements separated by commas between square brackets.
[1, 2, 3, 4, 5]
-- (1 2 3 4 5)
[]
-- ()
A colon is the list construct operator, just like cons or conj.
1:2:3:[]
-- (1 2 3)
A shortcut to list consecutive numbers is to write the low and high values of a range, without a comma.
[1 15]
-- (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
Operator ++ concatenates sequences, including lists.
[2,4,6,8] ++ [1,3,5,7]
-- (2 4 6 8 1 3 5 7)
A vector literal is denoted as a list with a leading hash or pound character.
#[]
-- []
#[1, 2, 3, 4 , 5]
-- [1 2 3 4 5]
A colon is the vector construct operator; like conj it adds to the end.
1:2:3:#[]
-- [3 2 1]
Operator ++ concatenates sequences, including vectors.
#[2,4,6,8] ++ #[1,3,5,7]
-- (2 4 6 8 1 3 5 7)
#[-10, -20] ++ [100, 200]
-- (-10 -20 100 200)
To add or replace elements to a vector there's also assoc.
assoc #[15, 30] 2 40
-- [15 30 40]
assoc #[15, 30] 0 40
-- [40 30]
A shortcut for a vector with consecutive numbers is to write the low and high values of a range, without a comma.
#[1 12]
-- [1 2 3 4 5 6 7 8 9 10 11 12]
A set literal has a leading dash or pound character, then braces around a comma-separated elements.
#{}
-- {}
#{1, 2, 3, 4 , 5}
-- #{1 2 3 4 5}
A colon is the set construct operator; like conj it adds to the start.
10 : #{ 20 }
-- #{10 20}
Operator ++ concatenates sequences, including sets.
#{2,4,6,8} ++ #{1,3,5,7}
-- (2 4 6 8 1 3 5 7)
#{-10, -20} ++ [100, 200]
-- (-10 -20 100 200)
[1,2] ++ #[30,40] ++ #{500, 600}
-- (1 2 30 40 500 600)
A map literal is denoted by key-value pairs separated by commas between braces.
{}
-- {}
{ :one 1, :two 2, :ten 10 }
-- {:one 1, :ten 10, :two 2}
A colon works as a construct operator, like using conj. A new entry must be a vector with the key followed by the value.
#[:zero, 0] : { :one 1, :two 2, :ten 10 }
-- {:zero 0, :one 1, :ten 10, :two 2}
Operator ++ will also concatenate maps, but as collection of vector entries.
{:k 100, :k1 50} ++ {:k2 200}
-- ([:k 100] [:k1 50] [:k2 200])
To add or replace entries to a map there's also assoc. The receiving map need not be a literal.
assoc { :one 1, :ten 10 } :zero 0
-- {:zero 0, :one 1, :ten 10}
assoc { :one 1, :ten 10 } :one "one"
-- {:one one, :ten 10}
Use dissoc to return a map without the entry for the given key.
dissoc { :one 1, :two 2, :ten 10 } :ten
-- {:one 1, :two 2}
The following example illustrates a nested data structure built with expressions.
val top = 99
val bottom = 0
-- #'user/bottom
{ :key1 #{ [ (top - bottom) / 2.0, "second", #[top * 5, top * 10, top * 15] ], 'a', 9999 } }
-- {:key1 #{a (49.5 second [495 990 1485]) 9999}}