Skip to content

Instantly share code, notes, and snippets.

@RubenSandwich
Last active January 18, 2017 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RubenSandwich/a8da66cfc87bfb982a1f1d30f129e255 to your computer and use it in GitHub Desktop.
Save RubenSandwich/a8da66cfc87bfb982a1f1d30f129e255 to your computer and use it in GitHub Desktop.
Code & Supply Eve intro
# Eve Intro
Welcome to Eve.
We are going to be going through this document as a group. But if you feel like I'm going too slow, feel free to zip ahead. After this short intro to the basic Eve concepts there will be some exercise problems, those can be done as an individual or as a group.
If you ever have questions about one of the concepts in here feel free to raise your hand and ask. The one thing I ask about questions is that wait until I introduce a concept before asking questions about it. If I haven't introduced a concept your curious about then please wait till the workshop problem time begins and call me over or find me after the talk.
With pleasantries out of the way let's learn some Eve!
## Records
Anything between square brackets, for example
`[name: “Ruben”]`
is a Record.
Records are the facts or variables of Eve, they are how you store data that is important to you.
Records store data in a attribute value pair:
`[string: “Value”, num: 2017, list: (1, 2, 3), boolean: false, record: [data: 23]]`
Records can store numbers, strings, booleans, lists, or even other records.
Records are usually given tags, which begin with a hashtag: #, to help find query or find them:
`[#me #hash-tags-are-so-2007 lame-joke: true, name: “Ruben”]`
Which is just a shorthand way of doing:
`[tag: ”me”, tag: ”hash-tags-are-so-2007”, lame-joke: true, name: “Ruben”]`
## Actions
To do things in Eve we perform actions on Records.
There are three types of Actions
**search** - Find Records
**commit** - Update or Create a Record
**bind** - Define relationships between Records. For example: "When this record updates, update this record too".
These actions can be mixed and matched into Blocks. They usually come in the form of Search and then Commit or Search and then Bind. But they can be mixed and matched in anyway you want. Commit then Search and Bind? Sure! Remember order doesn't matter in Eve.
Each action can perform their action on a **Database**.
Databases are just a collection of records. You do that by specifying the Database after the Action with an at sign, @.
`search @browser`
Certain Databases have special meaning such as: @browser, @event, @http. But we are only going to use the @browser Database today.
### Example of Eve code
Great now that we know how things look let's look at and play with some Eve code.
The code below is disabled. *(You can tell because the checkbox in the upper right of the box is unchecked.)*
Let's check it and run the code.
~~~eve disabled
commit @browser
[#div text:"Hello world!"]
~~~
Which is a block that commits to the browser database a record with a tag of div and an attribute and value pair of text and "Hello World!".
The div tag in the browser db has a special meaning. That meaning is: "render this as an HTML div". Feel free to change the text of it.
### Help I broke something!
No worries just use this link:
[Reset](https://goo.gl/VqBtuF)
Don't actually just reload the page because Eve saves things aggressively and sometimes it gets it wrong and reloading will just load back up that bad save data.
### Searching
Let look commit a few things and then look at searching.
```eve
commit @example
[#person #me name:"Ruben"]
[#person name:"John"]
[#person name:"Jerry"]
```
Great now let's search for them and display them.
*(Remember to enable this code and run it.)*
```eve disabled
search @example
ruben = [#person #me name:"Ruben"]
commit @browser
[#div text:ruben.name]
```
What we did above is search for the record `[#person #me name:"Ruben"]` put that in a variable named `ruben` and then commited that varible's name attribute to the text of a div record in the browser.
When searching we don't have to be super exact, we could of dropped the `#me` tag.
```eve disabled
search @example
ruben = [#person name:"Ruben"]
commit @browser
[#div text:ruben.name]
```
We could of also just used the `#me` tag because we know that I'm the only record with that tag.
```eve disabled
search @example
ruben = [#me]
commit @browser
[#div text:ruben.name]
```
What happens if our search matches multiple records?
```eve disabled
search @example
person = [#person]
commit @browser
[#div text:person.name]
```
When then all of those people will be listed. Because we found 3 records with the `#person` tag the `[#div text:person.name]` is run 3 times.
See no loops. One item? Run once. Multiple items? Run on all of them.
We can even simplify our code by listing empty attributes and Eve will fill those in when it finds it:
```eve disabled
search @example
[#person name]
commit @browser
[#div text:name]
```
Let's exclude Ruben from this party. We can do this with `not`.
```eve disabled
search @example
[#person name]
not(name = "Ruben")
commit @browser
[#div text:name]
```
What happens when we search for something that doesn't exist:
```eve disabled
search @example
[#you name]
commit @browser
[#div text:name]
```
Nothing happens. Because Eve tries to find `#you` and can't *(Stop. Breath. Don't have an existential crisis you still exist. I hope. Also only you can see this.)* so it stops executing this block. (Other blocks that it can find things it will still run however, so you always just write what you want to happen and if it can't happen then it just won't run.)
### Commiting
Great now I have something how do I change it? Remember you do that with the commit action.
When updating a record you can change in four different ways:
**Add: +=** Add another value to an attribute. So if you had a value on an attribute before, you now have two values on it.
**Set: :=** Update a value on an attribute.
**Remove: -=** Remove a value from an attribute.
**Merge: <-** Merge two records together
(Use this block to see the changes below. It'll be explained in a bit.)
```eve disabled
search @example
[#person name]
bind @browser
[#div text:name]
```
Behold. What you believed to be Ruben is now Jim!
```eve disabled
search @example
me = [#me]
commit @example
me.name := "Jim"
```
And who you believed to be Jim now has two names!
```eve disabled
search @example
person = [#me]
person.name = "Jim"
commit @example
person.name += "Tim"
```
### Binding
Remember that binding defines a relationship between records.
How does this differ from commit?
A commit will happen every time a block can run, a bind will only update if the value has changed. For an example of this compare and contrast the two blocks below.
**Commit**
```eve disabled
search
[#time seconds]
commit @browser
[#div text: seconds]
```
**Bind**
```eve disabled
search
[#time seconds]
bind @browser
[#div text: seconds]
```
## Exercise Problems
You now know enough Eve to solve some problems:
[Problems](https://goo.gl/dNniCN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment