Permalink
Browse files

Attempting to remove all the references to forgeable names.

  • Loading branch information...
JoshOrndorff committed Oct 27, 2018
1 parent d7439ed commit 234ca5c575040982e91c7702a2a54e43bbb230a5
@@ -31,18 +31,18 @@ We always send ____ and receive ____.
- [ ] names, processes
- [ ] no such restriction

TODO: Another standing at a mailbox drawing. Maybe mix it up with the kind of mailbox that mounts on the front of your house or a mail slot or something. "Awww man, bills again? I wanted love letters."
<!-- TODO: Another standing at a mailbox drawing. Maybe mix it up with the kind of mailbox that mounts on the front of your house or a mail slot or something. "Awww man, bills again? I wanted love letters." -->

That was just a nice refresher from last unit. Hope your memory is holding up so far. If you've been writing your own rholang code, you may have found yourself really wishing you could receive processes directly so you didn't have to type all those `*`s. This is a common situation, and luckily rholang has a nice solution. We do always have to receive names, but we can bind them to name syntax like `@myValue`. Since `@myValue` is a name, `myValue` must be a process.

This syntax allows us to do things like
`for (@number <- @"someChan"){@"double"!(2 * number)}``
`for (@number <- someChan){double!(2 * number)}``

What code could be parred with the previous code to leave the number `24` on `@"double"`?
What code could be parred with the previous code to leave the number `24` on `double`?
- [ ] @number!(12)
- [x] @"someChan"!(12)
- [x] someChan!(12)
- [ ] @number!(24)
- [ ] @"double"!(48)
- [ ] double!(48)

### Exercise
Revisit the telephone game from lesson 3 and show that we could have used the `@message` pattern so `message` would be a process.
@@ -117,7 +117,7 @@ What would `stdout!((not not true) or false)` output?
- [ ] neither; that's invalid syntax

### Exercise
Write a contract that tells a caller whether they are eligible to vote. In order to vote you must be a certain age and of a certain country. You can pick the age and country. To use the contract, I would par in `@"canIVote!("Nigeria", 30)"`.
Write a contract that tells a caller whether they are eligible to vote. In order to vote you must be a certain age and of a certain country. You can pick the age and country. To use the contract, I would par in `canIVote!("Nigeria", 30)"`.

### Exercise
The contract above only works for one specific country. Use what we learned about factories to enable creating many of these eligibility checkers. To create a new checker for Canada, where the voting age is 18 par in `@"checkerFactory"!(canadaChecker, "Canada", 18)`. Then a 41-year-old Estonian would check whether he can vote in Canada with `canadaChecker!("Estonia", 41)`. Spoiler alert: He cannot vote in Canada.
The contract above only works for one specific country. Use what we learned about factories to enable creating many of these eligibility checkers. To create a new checker for Canada, where the voting age is 18 par in `checkerFactory!(canadaChecker, "Canada", 18)`. Then a 41-year-old Estonian would check whether he can vote in Canada with `canadaChecker!("Estonia", 41)`. Spoiler alert: He cannot vote in Canada.
@@ -1,7 +1,7 @@
new log(`rho:io:stdout`), register in {
new print(`rho:io:stdout`), register in {

for (@{{@"name"!(_) | _} /\ {@"age"!(_) | _}} <= register){
log!("Both name and age were in the data")
print!("Both name and age were in the data")
}
|

@@ -1,5 +1,5 @@
new ack in {
@"getInspectionChannel"!(*ack)|
new getInspectionChannel, ack in {
getInspectionChannel!(*ack)|
for(@(missile, "inspect") <- ack){
@(missile, "launch")!(Nil)
}
@@ -1,4 +1,4 @@
new log(`rho:io:stdout`), missile in {
new getInspectionChannel, log(`rho:io:stdout`), missile in {

contract @(*missile, "launch")(_) = {
log!("launching...")
@@ -9,7 +9,7 @@ new log(`rho:io:stdout`), missile in {
}
|

contract @"getInspectionChannel"(return) = {
contract getInspectionChannel(return) = {
return!(bundle+{(*missile, "inspect")})
}
}
@@ -62,12 +62,12 @@ Lists are a lot like tuples, but they are made with square brackets instead of p
Implement the body of the following running log contract. The user will call the contract every time they go for a run passing in the distance that they ran. The contract will keep track of all the runs in a list. You may also write methods to get all the run data, or get the total distance the user has run.

```rholang
new runsCh in {
new logRun, runsCh in {
// No runs to start with
runsCh!([])|
contract @"running log"(distance) = {
contract logRun(distance) = {
// Your code here
}
}
@@ -160,6 +160,6 @@ new alice, bob, key1, key2, stdout(`rho:io:stdout`) in {
What tuple is used to build the compound name in `contract @(*self, "getVal") = { Nil }`?
- [ ] `self`
- [ ] `"getval"`
- [x] `(*self, getVal)`
- [x] `(*self, "getVal")`
- [ ] `@(*self, "getVal")`
- [ ] `@"getVal"`
@@ -95,4 +95,4 @@ Which of these things is not like the other?
Which send will produce a comm event with `for (message <- grandmasSnapChat){Nil}`?
- [ ] `for(grandmasSnapChat)!("Hi Grandma")`
- [x] `grandmasSnapChat!("Glad you're snapping Grandma")`
- [ ] `for("Here's a snap for you g'ma" <- @"grandmasSnapChat")`
- [ ] `for("Here's a snap for you g'ma" <- grandmasSnapChat)`
@@ -42,10 +42,10 @@ State whether `x` is bound or free in each of the following code snippets.
- [ ] Free
- [ ] Neither

`for (y <- @"x"){Nil}`
`for (y <- x){Nil}`
- [ ] Bound
- [ ] Free
- [x] Neither
- [x] Free
- [ ] Neither

## The `new` Operator
`for` and `contract` are perfect for binding variables inside of continuations. It turns out that the `new` operator also binds variables. What does it bind them to? Brand new channels that we can use to send messages on.
@@ -1,3 +1,3 @@
for (order <= coffeeShop) {
@"stdout"!("Coffee Order Received")
stdout!("Coffee Order Received")
}
@@ -1,3 +1,3 @@
contract coffeeShop(order) = {
@"stdout"!("Coffee Order Received")
stdout!("Coffee Order Received")
}
@@ -1,11 +1,11 @@
new boxCh in {
new stdout(`rho:io:stdout`), boxCh in {
// To save data we just put it in the box
boxCh!(42)
|

// Then to get data back out
for (data <- boxCh) {
// Do whatever you want with the data here.
@"somePublicChannel"!(*data)
stdout!(*data)
}
}
@@ -47,10 +47,10 @@ Our current savings account allows negative balances, but probably it shouldn't.
Try to write the code Eve would need to par in to steal Sarah's funds. I bet you can't think of any. That's because only Sarah has access to the unforgeable names that control the account.


If Sarah wanted to allow anyone to deposit into her bank account, but not check or withdraw, how should she create her account?
- [ ] `openAccount!(10, *"sarahDeposit", *sarahWithdraw, *sarahCheck)`
- [x] `openAccount!(10, @"sarahDeposit", *sarahWithdraw, *sarahCheck)`
- [ ] `openAccount!(10, @"sarahDeposit", @"sarahWithdraw", @"sarahCheck")`
If Sarah wanted to allow her friend Stephanie to deposit into her bank account, but not check or withdraw, what code should she run?
- [ ] `stephanie!(*sarahWithdraw)`
- [x] `stephanie!(*sarahDeposit)`
- [ ] `sarahWithdraw!("enable", *stephanie)`
- [ ] `openAccount!(10, *sarahDeposit, @"sarahWithdraw", @"sarahCheck")`


@@ -28,10 +28,10 @@ new stationFactory, stdout(`rho:io:stdout`) in {

// Controllers create new station with private set capability
// and public get capability
new set in {
stationFactory!("Weather is nice", "airportInfo", *set)
new airportInfo, set in {
stationFactory!("Weather is nice", *airportInfo, *set)
}
|
// Listener tunes in to receive latest message
@"airportInfo"!(*stdout)
airportInfo!(*stdout)
}

0 comments on commit 234ca5c

Please sign in to comment.